hdu4910(米勒罗宾)

Problem about GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 522    Accepted Submission(s): 86


Problem Description
Given integer m. Find multiplication of all 1<=a<=m such gcd(a, m)=1 (coprime with m) modulo m.
 

Input
Input contains multiple tests, one test per line.
Last line contains -1, it should be skipped.

[Technical Specification]
m <= 10^18
 

Output
For each test please output result. One case per line. Less than 160 test cases.
 

Sample Input
   
   
1 2 3 4 5 -1
 

Sample Output
   
   
0 1 2 3 4

题意:给定一个数n,求所有小于等于n且与n互素的数的乘积再mod n

思路:打表可以发现如下规律,只有当n是1,2,4或者只有一种质因子的时候答案为n-1,其它都为1,如果n是偶数,就把n除以2以后再判断,如果n小于1e6则可以直接判断,如

果n很大,就先用1e6以下的素数去整除,看能不能判断出来,如果还不能,就用米勒罗宾判断n是否为素数,如果是,那么n满足,如果不是就将n开方以后再平方看是否等于

n,因为n是小于1e18的,所以n不可能有三个大于1e6的素因子,如果等于就满足,否则不满足

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          using namespace std; typedef __int64 ll; int isprime[1000001]; int prime[100000]; int cnt; ll n; const int S=20; ll mult_mod(ll a,ll b,ll c) { a%=c; b%=c; ll ret=0; while(b) { if(b&1){ret+=a;ret%=c;} a<<=1; if(a>=c)a%=c; b>>=1; } return ret; } ll pow_mod(ll x,ll n,ll mod) { if(n==1)return x%mod; x%=mod; ll tmp=x; ll ret=1; while(n) { if(n&1) ret=mult_mod(ret,tmp,mod); tmp=mult_mod(tmp,tmp,mod); n>>=1; } return ret; } bool check(ll a,ll n,ll x,ll t) { ll ret=pow_mod(a,x,n); ll last=ret; for(int i=1;i<=t;i++) { ret=mult_mod(ret,ret,n); if(ret==1&&last!=1&&last!=n-1) return true; last=ret; } if(ret!=1) return true; return false; } bool Miller_Rabin(ll n) { if(n<2)return false; if(n==2)return true; if((n&1)==0) return false; ll x=n-1; ll t=0; while((x&1)==0){x>>=1;t++;} for(int i=0;i 
         
           >1; else nn=n; ll nnn=nn; int flag=0; for(i=0;i 
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值