HDU 4861 Couple doubi (number theory)
HDU 4861 Couple doubi
Question Link
Given k and p, there are k balls. The value of each ball is 1 ^ I + 2 ^ I +... + (p-1) ^ I (mod p) (1 <= I <= k). Now, the two take turns to take the ball. The final sum of the values of the Ball wins, and ask if the first player can win.
Idea: first-hand cannot lose, non-win is equal, so you only need to consider the value of each ball,
By using the ferma's theorem or Euler's theorem, we can easily obtain that the cycle of this function is p-1,
If I is a multiple of p-1, that is, the position of the cyclic section, then each value is 1 and the sum is p-1.
If I is not in the cyclic node position, take an original root g. According to the nature of the original root,GIContains 1 to p-1, so the original formula is equivalentG1M+G2M+G3M...MOD P, This is an proportional series, obtained using the first n terms and formulasGM? (1?GMP? 1)/(1?GM)MOD PUsing the Fermat TheoremGMP? 1MOD P= 1 then the original formula is 0
It is proved that the original cycle is p-1 and only has a value on the cycle section. Therefore, you only need to determine whether the number of the original cycle sections is an odd or even number.
Code:
#include
#include
#include
long long k, p;int main() { while (~scanf("%lld%lld", &k, &p)) {if (k / (p - 1) % 2) printf("YES\n");else printf("NO\n"); } return 0;}