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
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