我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2014年4月26日 星期六

[Codeforce Round #242]C. Magic Formulas

題目網址: http://codeforces.com/contest/424/problem/C

題意: 題目給定公式,計算答案。



解法: 利用xor的交換律,及mod的規律,即可快速計算答案。

TAG: math

程式碼:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int q;
int xx[1000005];
int cal( int val )
{
static int i, ret;
ret = 0;
if( (n/val)&1 ) {
ret ^= xx[val-1];
}
ret ^= xx[n%val];
return ret;
}
int main( void )
{
int i, x;
xx[0] = 0;
for( i = 1; i <= 1000000; ++i ) xx[i] = xx[i-1]^i;
while( scanf( "%d", &n ) == 1 ) {
q = 0;
for( i = 1; i <= n; ++i ) {
scanf( "%d", &x );
q ^= x;
q ^= cal(i);
}
printf( "%d\n", q );
}
return 0;
}

沒有留言:

張貼留言

任何意見都樂意傾聽