題意: 題目給定公式,計算答案。
解法: 利用xor的交換律,及mod的規律,即可快速計算答案。
TAG: math
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽