对应HDU题目:点击打开链接
Couple doubi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1010 Accepted Submission(s): 705
Problem 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 1^i+2^i+...+(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).
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
费马小定理:
费马小定理是数论中的一个重要定理,其内容为: 假如p是质数,且(a,p)=1,那么 a(p-1) ≡1(mod p)。即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。
对于val(i)=1i+2i+⋯+(p−1)i%p来说,只有当i=ϕ(p)=p−1(p为素数)时,val(i)=p−1,其他情况下val(i)=0,那么只要确定说有多少个i是非0的即可,如果是偶数则输出NO,奇数输出YES。
证明,假设p有原根g,那么1i,2i,…,(p−1)i即是g1∗i,g2∗i,…,g(p−1)∗i的一个排序,因为对于gk来说,k从1到p-1,gk均不相同,并且为1到p-1。
于是val(i)=gi∗(1−gi∗(p−1))1−gi
根据费马小定理,gi∗(p−1)%p=1
所以有val(i)=gi∗(1−1)1−gi=0
- p为质数,所以一定有原根
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的
欧拉函数)
假设一个数g对于P来说是原根,那么g^i mod P的结果两两不同,且有 1<g<P, 0<i<P,那么g可以称为是P的一个原根,归根到底就是g^(P-1) = 1 (mod P)当且仅当指数为P-1的时候成立.(这里P是素数).
简单来说,g^i mod p ≠ g^j mod p (p为素数)
其中i≠j且i, j介於1至(p-1)之间
则g为p的原根。
求原根目前的做法只能是从2开始枚举,然后暴力判断g^(P-1) = 1 (mod P)是否当且当指数为P-1的时候成立
而由于原根一般都不大,所以可以暴力得到.
知道这个就会做了。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int k,p;
while(scanf("%d%d",&k,&p)!=EOF)
{
if((k/(p-1))%2==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}