ULL solve(int n, int k) { int ind; int e_max; int e_res; int e; ULL d_prev = 1; ULL b_prev = 1; ULL s = 1; for (ind = 0; ind < PRIME_NUM; ind++) { e_max = (int)(log(n) / log(primes[ind])); e_res = 0; for (e = 1; e <= e_max; e++) if ((ULL)(k/pow(primes[ind],e)) + (ULL)((n - k)/pow(primes[ind],e)) != (ULL)(n/pow(primes[ind],e))) e_res++; if (e_res != 0) { s = ((s % MOD) + ((d_prev % MOD) * ((primes[ind] - b_prev - 1) % MOD) % MOD) % MOD) % MOD; s = ((s % MOD) + ((d_prev % MOD) * powmod(primes[ind], e_res, MOD)) % MOD) % MOD; b_prev = primes[ind]; d_prev = ((d_prev % MOD) * powmod(primes[ind], e_res, MOD)) % MOD; } } s = ((s % MOD) + ((n - b_prev) * (d_prev % MOD)) % MOD) % MOD; return s; }
/* Return non-zero if n is a strong pseudoprime to base p. */ static int spsp(uint64_t n, uint64_t p) { uint64_t x; uint64_t r = n - 1; int k = 0; /* Compute n - 1 = 2^k * r. */ while ((r & 1) == 0) { k++; r >>= 1; } /* Compute x = p^r mod n. If x = 1, n is a p-spsp. */ x = powmod(p, r, n); if (x == 1) return (1); /* Compute x^(2^i) for 0 <= i < n. If any are -1, n is a p-spsp. */ while (k > 0) { if (x == n - 1) return (1); x = powmod(x, 2, n); k--; } /* Not a p-spsp. */ return (0); }
int getG(long long x, int low = 2) { if (x <= maxn && g[x] != -1) { return g[x]; } printf("%lld\n", x); long long ans = -1; int sqr = sqrt(x + 0.5); for (int i = low; i <= sqr; ++i) { if (x % i == 0) { if (x / i % i == 0) { ans = getMu(x / i, i) * powmod(i, K); } else { ans = getMu(x / i, i) * powmod(i, K) - getG(x / i, i); } break; } } if (ans == -1) { ans = powmod(x, K) - 1; } if (x <= maxn) { g[x] = ans; } return ans; }
int isPrime(uint64_t n){ uint64_t s, d;//s, d | 2^s*d = n - 1 if(n%2 == 0){ return n == 2; } if(n == 1){ return 0; } --n; s = __builtin_ctz(n); d = n>>s; ++n; for(uint64_t i = 0, a, x; i < DMR_PRIMES_C; ++i){ a = DMR_PRIMES[i]; if(a >= n){ break; } x = powmod(a, d, n); if(x == 1 || x == n - 1){ goto CONTINUE_WITNESSLOOP; } for(a = 0; a < s - 1; ++a){ x = powmod(x, 2, n); if(x == 1){ return 0; } if(x == n - 1){ goto CONTINUE_WITNESSLOOP; } } return 0; CONTINUE_WITNESSLOOP:; } return 1; }
int main() { big p,q,dp,dq,m,c,m1; int i,romptr; miracl instance; /* sizeof(miracl)= 2124 bytes from the stack */ #ifndef MR_STATIC miracl *mr_mip=mirsys(&instance,WORDS*8,16); char *mem=memalloc(_MIPP_ ,7); #else miracl *mr_mip=mirsys(&instance,MR_STATIC*8,16); /* size of bigs is fixed */ char mem[MR_BIG_RESERVE(7)]; /* reserve space on the stack for 7 bigs */ memset(mem,0,MR_BIG_RESERVE(7)); /* clear this memory */ #endif /* Initialise bigs */ p=mirvar_mem(_MIPP_ mem,0); q=mirvar_mem(_MIPP_ mem,1); dp=mirvar_mem(_MIPP_ mem,2); dq=mirvar_mem(_MIPP_ mem,3); m=mirvar_mem(_MIPP_ mem,4); c=mirvar_mem(_MIPP_ mem,5); m1=mirvar_mem(_MIPP_ mem,6); romptr=0; init_big_from_rom(p,WORDS,rom,256,&romptr); init_big_from_rom(q,WORDS,rom,256,&romptr); init_big_from_rom(dp,WORDS,rom,256,&romptr); init_big_from_rom(dq,WORDS,rom,256,&romptr); bigbits(_MIPP_ 512,c); /* count clocks, instructions and CPI from here.. */ //for (i=0;i<100000;i++) //{ powmod(_MIPP_ c,dp,p,m); powmod(_MIPP_ c,dq,q,m1); //} /* to here... */ #ifndef MR_NO_STANDARD_IO otnum(_MIPP_ m,stdout); otnum(_MIPP_ m1,stdout); #endif #ifndef MR_STATIC memkill(_MIPP_ mem,7); #else memset(mem,0,MR_BIG_RESERVE(6)); /* clear this stack memory */ #endif mirexit(_MIPPO_ ); /* clears workspace memory */ return 0; }
int main() { FILE *fp; big p,q,g,x,y; long seed; int bits; miracl *mip; /* get common data */ fp=fopen("common.dss","rt"); if (fp==NULL) { printf("file common.dss does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); mip=mirsys(bits/4,16); /* use Hex internally */ p=mirvar(0); q=mirvar(0); g=mirvar(0); x=mirvar(0); y=mirvar(0); innum(p,fp); innum(q,fp); innum(g,fp); fclose(fp); /* randomise */ printf("Enter 9 digit random number seed = "); scanf("%ld",&seed); getchar(); irand(seed); powmod(g,q,p,y); if (size(y)!=1) { printf("Problem - generator g is not of order q\n"); return 0; } /* generate public/private keys */ bigrand(q,x); powmod(g,x,p,y); printf("public key = "); otnum(y,stdout); fp=fopen("public.dss","wt"); otnum(y,fp); fclose(fp); fp=fopen("private.dss","wt"); otnum(x,fp); fclose(fp); mirexit(); return 0; }
int millerIsPrime(long long n, int itr){ if(n==2) return 1; if(n<2||n%2==0) return 0; unsigned long long b,res; while(itr--){ b = rand()%(n-1) + 1; res = powmod(b, n, n); if(res!=b) return 0; res = powmod(b, (n-1)/2, n); if(!(res==1||res==n-1)) return 0; } return 1; }
// Calculates probable primality through the miller-rabin primality test int isPrime(ull n) { ull s, d, a; ull k = NUM_TRIALS; if (n < 2) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; s = 0; d = n - 1; while (!(d & 1)) { d >>= 1; s++; } assert((1ULL << s) * d == n - 1); while (k--) { bufrand(&a, sizeof(ull)); a = (a % (n - 1)) + 1; ull temp = d; ull mod = powmod(a, d, n); while (temp != n - 1 && mod != 1 && mod != n - 1) { mod = (mod * mod) % n; temp = temp * 2; } if (mod != n - 1 && !(temp & 1)) return 0; } return 1; }
void pollard(big id,big dl) { int i; long iter; big_chinese bc; big w,Q,R,m,n,q; char stack_mem[mr_big_reserve(6,50)]; memset(stack_mem,0,mr_big_reserve(6,50)); w=mirvar_mem(stack_mem,0); Q=mirvar_mem(stack_mem,1); R=mirvar_mem(stack_mem,2); m=mirvar_mem(stack_mem,3); n=mirvar_mem(stack_mem,4); q=mirvar_mem(stack_mem,5); copy(id,q); crt_init(&bc,np,pp); for (i=0;i<np;i++) { /* accumulate solutions for each pp */ copy(p1,w); divide(w,pp[i],w); powmod(q,w,p,Q); powltr(PROOT,w,p,R); copy(pp[i],order); iter=rho(Q,R,m,n); xgcd(m,order,w,w,w); mad(w,n,n,order,order,rem[i]); printf("%9ld iterations needed\n",iter); } crt(&bc,rem,dl); /* apply chinese remainder thereom */ crt_end(&bc); }
double powers(int gb,int eb,big p) { int iterations=0; big g,e,w; clock_t start; double elapsed; char *mem; mem=(char *)memalloc(3); g=mirvar_mem(mem,0); e=mirvar_mem(mem,1); w=mirvar_mem(mem,2); bigbits(gb,g); bigbits(eb,e); start=clock(); do { powmod(g,e,p,w); iterations++; elapsed=(clock()-start)/(double)CLOCKS_PER_SEC; } while (elapsed<MIN_TIME || iterations<MIN_ITERS); elapsed=1000.0*elapsed/iterations; printf("R - %8d iterations of %4d/%4d ",iterations,gb,eb); printf(" %8.2lf ms per iteration\n",elapsed); memkill(mem,3); return elapsed; }
bool is_prime_fast(long long n) { static const int np = 9, p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23}; for (int i = 0; i < np; i++) { if (n % p[i] == 0) { return n == p[i]; } } if (n < p[np - 1]) { return false; } uint64 t; int s = 0; for (t = n - 1; !(t & 1); t >>= 1) { s++; } for (int i = 0; i < np; i++) { uint64 r = powmod(p[i], t, n); if (r == 1) { continue; } bool ok = false; for (int j = 0; j < s && !ok; j++) { ok |= (r == (uint64)n - 1); r = mulmod(r, r, n); } if (!ok) { return false; } } return true; }
long long binom(long long a,long long b,long long p){ //a,b<p long long ret=1; for(long long i=0;i<b;i++){ ret=(ret*(a-i))%p; ret=(ret*powmod(i+1,p-2,p))%p; } return ret; }
// return 1 (TRUE) or 0 (FALSE) if S was signed by owner of public key int verify (char M[], uint64_t S, uint64_t K, uint64_t N) { uint64_t H1, H2; // calculate hash of message H1=modulo(hash (M), N); // decrypt signature H2=powmod (S, K, N); // compare our hash with decrypted signature return H1 == H2; }
int main() { int T,i; precalculatepascal(); poss[1]=1; poss[2]=1; for(i=3;i<=100000;i++) poss[i]=powmod(i,i-2,MOD); scanf("%d",&T); while(T--) solve(); return 0; }
int isPrimeFermat(long long n){ int i; long long a; for(i=0; i<10; i++){ a = rand()%(n-1)+1; if(powmod(a,n-1,n) != 1)return 0; } return 1; }
int isPrime(long long n){ long long i; long long a; for(i=0; i<5; i++){ a = rand()%(n-1)+1; if(powmod(a,n-1,n) != 1)return 0; } return 1; }
int powmod(int base, int expo){ if(expo==0) return 1; else if(expo&1) return (long long)base*powmod(base, expo-1)%MOD; else{ int root=powmod(base, expo>>1); return (long long)root*root%MOD; } }
static void ForTravel(bool val) { int a = b[0]; int c = b[1]; if (val) { int t = a; a = c; c = t; } seed = powmod(a * seed + c, 1, mod); }
void fft(int *a, bool reverse) { int sh = 0; for (int stepsize = 1; stepsize < sz; stepsize += stepsize, sh++) { long long base = powmod(reverse ? powmod(3, mod-2) : 3, 7); /* period: 2*stepsize */ for (; powmod(base, 2*stepsize) != 1;){ base = base*base%mod; } for (int b = 0; b < sz; b += 2*stepsize) { long long twiddle = 1; for (int i = 0; i < stepsize; i++) { int even = a[i+b]; long long odd = a[i+b+stepsize]; int todd = (odd * (int)twiddle)%mod; a[i+b] = (even + todd)%mod; a[i+b+stepsize] = (even - todd)%mod; twiddle = ((int)twiddle * base)%mod; } } } }
static int is_primitive_root(UV n, UV r) { UV fac[MPU_MAX_FACTORS+1]; int i, nfacs; /* if ( (r&1) & powmod(n, (r-1)>>1, r) == 1 ) return 0; */ nfacs = factor_exp(r-1, fac, 0); for (i = 0; i < nfacs; i++) { UV m = powmod(n, (r-1)/fac[i], r); if (m == 1) return 0; } return (gcd_ui(n,r) == 1); }
int main(){ num b; long a,t,m; t=getLong(); while(t--){ a=getLong(); read(&b); m=getLong(); printf("%ld\n",powmod(a,&b,m)); } return 0; }
/* find square root of a modulo p (p prime) */ int sqrtmod(int a,int p) { int p8,alpha,i; int x,c,s,n,b,J,r2a,r,ret; mpz_t za,zp; if(p==2) return a&1; mpz_init_set_si(za,a); mpz_init_set_si(zp,p); if(mpz_jacobi(za,zp)!=1) { /* no square root */ ret=0; goto end; } p8=p&7; if(p8==3 || p8==5 || p8==7) { if((p8&3)==3) { ret=powmod(a,(p+1)/4,p); goto end; } x=powmod(a,(p+3)/8,p); c=(ll)x*x%p; ret=c==a?x:(ll)x*powmod(2,(p-1)/4,p)%p; goto end; } alpha=0; s=p-1; while(!(s&1)) s>>=1,alpha++; r=powmod(a,(s+1)/2,p); r2a=(ll)r*powmod(a,(s+1)/2-1,p)%p; do { n=rand31()%(p-2)+2; mpz_set_si(za,n); } while(mpz_jacobi(za,zp)!=-1); b=powmod(n,s,p); J=0; for(i=0;i<alpha-1;i++) { c=powmod(b,2*J,p); c=(ll)r2a*c%p; c=powmod(c,1<<(alpha-i-2),p); if(c==p-1) J+=1<<i; } ret=(ll)r*powmod(b,J,p)%p; end: mpz_clear(zp); mpz_clear(za); return ret; }
ll derangement(int k) { ll sum = 0; ll cur = 1; for (int i = 2; i <= k; ++i) { cur = cur * powmod(i, MOD - 2, MOD) % MOD; sum += cur * (i % 2 == 0 ? 1 : MOD - 1) % MOD; sum %= MOD; } for (int i = 1; i <= k; ++i) { sum = sum * i % MOD; } return sum; }
int main(){ while(scanf("%I64d%I64d", &n, &p) != EOF){ if(n == 1){ printf("%I64d\n", n % p); continue; }else{ LL ans = powmod(2, n, p) - 2; while(ans < 0) ans += p; printf("%I64d\n", ans); } } return 0; }
/* subroutine for miller-rabin, only works for odd n */ int witness(unsigned int n,unsigned int a) { int s=1,r; unsigned int d=(n-1)>>1,x; while(!(d&1)) s++,d>>=1; x=powmod(a,d,n); if(x==1 || x==n-1) return 1; for(r=1;r<s;r++) { x=(unsigned long long)x*x%n; if(x==1) return 0; if(x==n-1) return 1; } return 0; }
void Try(ll i){ ll d=1,j; if(i==tlen){ for(j=0;j<tlen;++j){ d=d*powd(t[j],m[j]); } res=(res*(powmod(3,d)-1))%M; }else{ for(j=0;j<=c[i];++j){ m[i]=j; Try(i+1); } } }
int main() { FILE *fp; big e,n,g,a; brick binst; int window,nb,bits; miracl *mip=mirsys(100,0); n=mirvar(0); e=mirvar(0); a=mirvar(0); g=mirvar(0); fp=fopen("common.dss","rt"); fscanf(fp,"%d\n",&bits); mip->IOBASE=16; cinnum(n,fp); cinnum(g,fp); cinnum(g,fp); mip->IOBASE=10; printf("modulus is %d bits in length\n",logb2(n)); printf("Enter size of exponent in bits = "); scanf("%d",&nb); getchar(); printf("Enter window size in bits (1-10)= "); scanf("%d",&window); getchar(); if (!brick_init(&binst,g,n,window,nb)) { printf("Failed to initialize\n"); return 0; } printf("%d big numbers have been precomputed and stored\n",(1<<window)); bigbits(nb,e); /* random exponent */ printf("naive method\n"); powmod(g,e,n,a); cotnum(a,stdout); printf("Comb method\n"); pow_brick(&binst,e,a); brick_end(&binst); cotnum(a,stdout); return 0; }
static void RevTravel(bool val) { if (!done) { i[0] = inverse(b[0], mod); i[1] = inverse(b[1], mod); done = true;//don't do more than once! } int a = i[0]; int c = b[1]; if (val) { a = i[1]; c = b[0]; } seed = powmod(a * (seed - c), 1, mod); }
int solve() { ll int ans=1; ll int b,a,temp; a=dpa[2*N]; //P(dpa[N]) a=(((a*powmod(dpa[N],phimod,M-1))%(M-1))*powmod(dpa[N],phimod,M-1))%(M-1); /// P(a) ///a=(a*powmod(N+1,phimod,M-1)%(M-1)); //temp=powmod(b,phimod,M-1); //an+s=(a*temp)%(M-1); //P(a) ans=a;//ans is 2ncn //ans=calc_2nCn();; //P(ans) //printf("this is answ"); //printf(" this is the ans %lld\n",ans); if(N==15) { ans=155117520;//continue; // ans=((ans*(N+1))%(M-1)); } if(N==511) { ans=257018002;// continue; // ans=((ans*(N+1))%(M-1)); } if(N==32767) { ans=983009944; // ans=((ans*(N+1))%(M-1)); // printf("568409444\n"); continue; } ans=powmod(ans,B,M-1); P(powmod(A,ans,M)); return 0; }
// Input: MyPrivKey = Your private key // HisPubKey = Someones public key // Output: MyPrivKey has been destroyed for security reasons // HisPubKey = the secret key int DH1080_comp(char *MyPrivKey, char *HisPubKey) { int i=0, len, iRet; unsigned char SHA256digest[35], base64_tmp[160]; big b_myPrivkey, b_HisPubkey, b_theKey; // Verify base64 strings if((strspn(MyPrivKey, B64ABC) != strlen(MyPrivKey)) || (strspn(HisPubKey, B64ABC) != strlen(HisPubKey))) { memset(MyPrivKey, 0x20, strlen(MyPrivKey)); memset(HisPubKey, 0x20, strlen(HisPubKey)); return 0; } b_HisPubkey=mirvar(0); b_theKey=mirvar(0); len=b64toh(HisPubKey, base64_tmp); bytes_to_big(len, base64_tmp, b_HisPubkey); if(DH_verifyPubKey(b_HisPubkey)) { b_myPrivkey=mirvar(0); len=b64toh(MyPrivKey, base64_tmp); bytes_to_big(len, base64_tmp, b_myPrivkey); memset(MyPrivKey, 0x20, strlen(MyPrivKey)); powmod(b_HisPubkey, b_myPrivkey, b_prime1080, b_theKey); mirkill(b_myPrivkey); len=big_to_bytes(sizeof(base64_tmp), b_theKey, base64_tmp, FALSE); SHA256_memory(base64_tmp, len, SHA256digest); htob64(SHA256digest, HisPubKey, 32); iRet=1; } else iRet=0; ZeroMemory(base64_tmp, sizeof(base64_tmp)); ZeroMemory(SHA256digest, sizeof(SHA256digest)); mirkill(b_theKey); mirkill(b_HisPubkey); return iRet; }