Skip to content

Commit

Permalink
Codeforces Round #305 (Div. 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
vawait committed May 28, 2015
1 parent 2b8af2f commit fe50a6a
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 0 deletions.
89 changes: 89 additions & 0 deletions CodeForces/Codeforces547A_MikeandFrog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Author: vawait
* Created Time: 2015/5/27 0:29:03
* Problem: Codeforces547A Mike and Frog
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define clr( x , y ) memset(x,y,sizeof(x))
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
typedef long long lint;
lint m , h1 , a1 , x1 , y1 , h2 , a2 , x2 , y2;

void init()
{
cin >> h1 >> a1 >> x1 >> y1;
cin >> h2 >> a2 >> x2 >> y2;
}

void work()
{
lint sum1 = 0 , sum2 = 0;
lint g1 = 0 , g2 = 0;
red(i,2e6,0) {
if ( h1 == a1 ) break;
h1 = ( x1 * h1 + y1 ) % m;
sum1 ++;
}
red(i,2e6,0) {
if ( h2 == a2 ) break;
h2 = ( h2 * x2 + y2 ) % m;
sum2 ++;
}
if ( h1 != a1 || h2 != a2 ) {
puts("-1");
return;
}
red(i,2e6,0) {
h1 = ( x1 * h1 + y1 ) % m;
g1 ++;
if ( h1 == a1 ) break;
}
red(i,2e6,0) {
h2 = ( h2 * x2 + y2 ) % m;
g2 ++;
if ( h2 == a2 ) break;
}
if ( sum1 == sum2 ) {
cout << sum1 << endl;
return;
}
if ( h1 == a1 && sum2 >= sum1 && sum2 % g1 == sum1 % g1 ) {
cout << sum2 << endl;
return;
}
if ( h2 == a2 && sum1 >= sum2 && sum1 % g2 == sum2 % g2 ) {
cout << sum1 << endl;
return;
}
if ( h1 != a1 || h2 != a2 ) {
puts("-1");
return;
}
red(i,2e6,0) {
if ( sum1 >= sum2 && sum1 % g2 == sum2 % g2 ) {
cout << sum1 << endl;
return;
}
sum1 += g1;
}
puts("-1");
}

int main()
{
while ( cin >> m ) {
init();
work();
}
return 0;
}
77 changes: 77 additions & 0 deletions CodeForces/Codeforces547B_MikeandFeet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Author: vawait
* Created Time: 2015/5/27 1:10:20
* Problem: Codeforces547B Mike and Feet
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define clr( x , y ) memset(x,y,sizeof(x))
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
typedef long long lint;
const int maxn = 301000;
int n , f[maxn] , L[maxn] , R[maxn] , ans[maxn] , use[maxn];
pair < int , int > a[maxn];

void init()
{
rep(i,1,n+1) L[i] = R[i] = f[i] = i , ans[i] = 0 , use[i] = 0;
rep(i,1,n) scanf("%d",&a[i].x) , a[i].y = i;
sort( a + 1 , a + n + 1 );
}

int find(int t)
{
return f[t] == t ? t : f[t] = find( f[t] );
}

void deal(int x,int y)
{
x = find( x );
y = find( y );
if ( x != y ) {
L[x] = min( L[x] , L[y] );
R[x] = max( R[x] , R[y] );
f[y] = x;
}
}

void work()
{
red(i,n,1) {
int id = a[i].y;
use[id] = 1;
if ( use[id-1] ) deal( id , id - 1 );
if ( use[id+1] ) deal( id , id + 1 );
id = find( id );
ans[R[id]-L[id]+1] = max( ans[R[id]-L[id]+1] , a[i].x );
}
red(i,n-1,1) ans[i] = max( ans[i] , ans[i+1] );
rep(i,1,n) printf("%d ",ans[i]);
}

int main()
{
while ( cin >> n ) {
init();
work();
}
return 0;
}
99 changes: 99 additions & 0 deletions CodeForces/Codeforces547C_MikeandFoam.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Author: vawait
* Created Time: 2015/5/27 1:24:08
* Problem: Codeforces547C Mike and Foam
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define clr( x , y ) memset(x,y,sizeof(x))
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
typedef long long lint;
const int maxn = 500000;
int n , q , num , a[maxn+10] , sum[maxn+10] , use[maxn+10];
int sum_prime , visit[maxn+10] , prime[maxn+10] , mobius[maxn+10];
lint ans = 0;
vector < int > g[maxn+10];

void pri()
{
sum_prime = 0;
mobius[1] = 1;
rep(i,2,maxn) {
if ( !visit[i] ) prime[++sum_prime] = i , mobius[i] = -1;
for ( int j = 1; j <= sum_prime && i * prime[j] <= maxn; j++ ) {
visit[i*prime[j]] = 1;
if ( i % prime[j] ) mobius[i*prime[j]] = -mobius[i]; else break;
}
}
rep(i,2,maxn) if ( mobius[i] != 0 )
for ( int j = i; j <= maxn; j += i ) g[j].pb( i );
}

void init()
{
pri();
scanf("%d%d",&n,&q);
rep(i,1,n) scanf("%d",&a[i]);
}

void add(int x)
{
int y;
ans += num;
red(i,g[x].size()-1,0) {
y = g[x][i];
ans += mobius[y] * sum[y];
sum[y] ++;
}
num ++;
}

void del(int x)
{
int y;
num --;
ans -= num;
red(i,g[x].size()-1,0) {
y = g[x][i];
sum[y] --;
ans -= mobius[y] * sum[y];
}
}

void work()
{
int x;
while ( q -- ) {
scanf("%d",&x);
if ( use[x] )
del( a[x] ) , use[x] = 0;
else
add( a[x] ) , use[x] = 1;
printf("%I64d\n",ans);
}
}

int main()
{
init();
work();
return 0;
}
74 changes: 74 additions & 0 deletions CodeForces/Codeforces547D_MikeandFish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Author: vawait
* Created Time: 2015/5/28 20:48:21
* Problem: Codeforces547D Mike and Fish
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define clr( x , y ) memset(x,y,sizeof(x))
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x) * (x))
typedef long long lint;
const int maxn = 201000;
int n , a[maxn] , b[maxn] , ans[maxn];
vector < int > g[maxn];

void init()
{
int x , y;
clr( a , 0 );
clr( b , 0 );
clr( ans , 0 );
rep(i,0,n) g[i].clear();
rep(i,1,n) {
scanf("%d%d",&x,&y);
if ( a[x] )
g[i].pb( a[x] ) , g[a[x]].pb( i ) , a[x] = 0;
else
a[x] = i;
if ( b[y] )
g[i].pb( b[y] ) , g[b[y]].pb( i ) , b[y] = 0;
else
b[y] = i;
}
}

void dfs(int t,int m)
{
ans[t] = m;
red(i,g[t].size()-1,0) if ( !ans[g[t][i]] )
dfs( g[t][i] , 3 - m );
}

void work()
{
rep(i,1,n) if ( !ans[i] ) dfs( i , 1 );
rep(i,1,n) printf("%c",ans[i]==1?'b':'r');
puts("");
}

int main()
{
while ( cin >> n ) {
init();
work();
}
return 0;
}

0 comments on commit fe50a6a

Please sign in to comment.