费马小定理——HDU4861

对应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).
 


 

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++(p1)i%p来说,只有当i=ϕ(p)=p1p时,val(i)=p1,其他情况下val(i)=0,那么只要确定说有多少个i是非0的即可,如果是偶数则输出NO,奇数输出YES。

证明,假设p有原根g,那么1i,2i,,(p1)i即是g1i,g2i,,g(p1)i的一个排序,因为对于gk来说,k从1到p-1,gk均不相同,并且为1到p-1。
于是val(i)=gi(1gi(p1))1gi
根据费马小定理,gi(p1)%p=1
所以有val(i)=gi(11)1gi=0

  1. 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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值