Couple doubi
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121334#problem/D
Description
DouBiXp has a girlfriend named DouBiNan.One day they felt very boring and decided to play some games. The rule of this game is as following. There are k balls on the desk. Every ball has a value and the value of ith (i=1,2,...,k) ball is 1i+2i+...+(p-1)^i (mod p). Number p is a prime number that is chosen by DouBiXp and his girlfriend. And then they take balls in turn and DouBiNan first. After all the balls are token, they compare the sum of values with the other ,and the person who get larger sum will win the game. You should print “YES” if DouBiNan will win the game. Otherwise you should print “NO”.
Input
Multiply Test Cases.
In the first line there are two Integers k and p(1<k,p<2^31).
Output
For each line, output an integer, as described above.
Sample Input
2 3
20 3
Sample Output
YES
NO
题意:
有n个数字定义如下:
num[i] = 1i+2i+...+(p-1)^i (mod p); (p是素数)
两人以最优策略轮流取其中的数字,总和大者获胜.
题解:
题目要求的数据规模非常大,下意识先打表找规律(注意p是素数).
很容易看出规律:每隔p-1个数才有一个非零数(易证该数为p-1);
由于是博弈的过程,所以每次都会取非零数; 判断非零数数目的奇偶即可.
数论解法:(费马小定理)
结论: (a^b)%p == ((a(p-1))(b/p-1))%p; 前提:b|p-1时才能用;
官方题解(2014 Multi-University Training Contest 1):
#
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 2100
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int main(int argc, char const *argv[])
{
//IN;
LL k,p;
while(scanf("%I64d %I64d", &k, &p) != EOF)
{
LL ans = k / (p-1);
if(ans & 1) puts("YES");
else puts("NO");
}
return 0;
}