int main(){ int b, p, m; while(scanf("%d%d%d", &b, &p, &m) != EOF){ printf("%d\n", bigmod(b, p, m)); } return 0; }
int main() { long long a; int p, m; while (scanf("%lld %d %d", &a, &p, &m) == 3) printf("%d\n", bigmod(a, p, m)); return 0; }
int main() { long bigmod(long b,long p,unsigned int m); long b,p; unsigned int m,ans; while((scanf("%ld%ld%ul",&b,&p,&m))!=EOF) { ans=bigmod(b,p,m); printf("%d\n",ans); } return 0; }
static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen) { struct dss_key *dss = (struct dss_key *) key; Bignum k, gkp, hash, kinv, hxr, r, s; unsigned char digest[20]; unsigned char *bytes; int nbytes, i; SHA_Simple(data, datalen, digest); k = dss_gen_k("DSA deterministic k generator", dss->q, dss->x, digest, sizeof(digest)); kinv = modinv(k, dss->q); /* k^-1 mod q */ assert(kinv); /* * Now we have k, so just go ahead and compute the signature. */ gkp = modpow(dss->g, k, dss->p); /* g^k mod p */ r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */ freebn(gkp); hash = bignum_from_bytes(digest, 20); hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */ s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */ freebn(hxr); freebn(kinv); freebn(k); freebn(hash); /* * Signature blob is * * string "ssh-dss" * string two 20-byte numbers r and s, end to end * * i.e. 4+7 + 4+40 bytes. */ nbytes = 4 + 7 + 4 + 40; bytes = snewn(nbytes, unsigned char); PUT_32BIT(bytes, 7); memcpy(bytes + 4, "ssh-dss", 7); PUT_32BIT(bytes + 4 + 7, 40); for (i = 0; i < 20; i++) { bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i); bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i); } freebn(r); freebn(s); *siglen = nbytes; return bytes; }
int main() { scanf("%s",s); m=strlen(s); ans+=(s[m-1]-48)%7; for(int h=m-2; h>-1; h--) ans+=((s[h]-48)*bigmod(10,m-h-1,7))%7; printf("%d",ans%7); }
int main() { int a, b, m, t; long long y, r; char A[MAX], B[MAX], N[MAX]; scanf("%d", &t); while(t--) { scanf("%s%s%s%d", A, B, N, &m); if(m==1) printf("0\n"); else if(N[0]=='0' && N[1]==0) printf("1\n"); else { lastlen = strlen(N); a = bigmod(A, m); b = bigmod(B, m); r = solve(a, y, N, m); r = (y + b*r) % m; printf("%lld\n", r); } } return 0; }
bool is_prime(int p){ if(p < 2 || (p != 2 && !(p & 1))) return false; int s = p - 1; while(!(s & 1)) s >>= 1; for(int i = 0; i < 10; ++i){ int a = rand() % (p - 1) + 1, temp = s; int mod = int(bigmod(LL(a), LL(temp), LL(p))); while(temp != p - 1 && mod != 1 && mod != p - 1){ mod = (LL(mod) * mod) % p; temp <<= 1; } if(mod != p - 1 && !(temp & 1)) return false; } return true; }
int main() { scanf("%lld %lld %lld",&a,&b,&c); for(int h=0; h<b; h++) { if(bigmod(h,a,b)==c) { printf("%d ",h); d++; } } if(d==0) printf("-1"); }
poly operator*(const poly& _b)const{ poly a=*this,b=_b; int k=n+b.n,i,N=1; while(N<=k)N*=2; a.co.resize(N,0); b.co.resize(N,0); int r=bigmod(root,(P-1)/N),Ni=inv(N,P); ps[0]=1; for(i=1;i<N;++i)ps[i]=(ps[i-1]*r)%P; a.trans1(N);b.trans1(N); for(i=0;i<N;++i)a.co[i]=((long long)a.co[i]*b.co[i])%P ; r=inv(r,P); for(i=1;i<N/2;++i)std::swap(ps[i],ps[N-i]); a.trans2(N); for(i=0;i<N;++i)a.co[i]=((long long)a.co[i]*Ni)%P; a.n=n+_b.n; return a; }
static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen) { /* * The basic DSS signing algorithm is: * * - invent a random k between 1 and q-1 (exclusive). * - Compute r = (g^k mod p) mod q. * - Compute s = k^-1 * (hash + x*r) mod q. * * This has the dangerous properties that: * * - if an attacker in possession of the public key _and_ the * signature (for example, the host you just authenticated * to) can guess your k, he can reverse the computation of s * and work out x = r^-1 * (s*k - hash) mod q. That is, he * can deduce the private half of your key, and masquerade * as you for as long as the key is still valid. * * - since r is a function purely of k and the public key, if * the attacker only has a _range of possibilities_ for k * it's easy for him to work through them all and check each * one against r; he'll never be unsure of whether he's got * the right one. * * - if you ever sign two different hashes with the same k, it * will be immediately obvious because the two signatures * will have the same r, and moreover an attacker in * possession of both signatures (and the public key of * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q, * and from there deduce x as before. * * - the Bleichenbacher attack on DSA makes use of methods of * generating k which are significantly non-uniformly * distributed; in particular, generating a 160-bit random * number and reducing it mod q is right out. * * For this reason we must be pretty careful about how we * generate our k. Since this code runs on Windows, with no * particularly good system entropy sources, we can't trust our * RNG itself to produce properly unpredictable data. Hence, we * use a totally different scheme instead. * * What we do is to take a SHA-512 (_big_) hash of the private * key x, and then feed this into another SHA-512 hash that * also includes the message hash being signed. That is: * * proto_k = SHA512 ( SHA512(x) || SHA160(message) ) * * This number is 512 bits long, so reducing it mod q won't be * noticeably non-uniform. So * * k = proto_k mod q * * This has the interesting property that it's _deterministic_: * signing the same hash twice with the same key yields the * same signature. * * Despite this determinism, it's still not predictable to an * attacker, because in order to repeat the SHA-512 * construction that created it, the attacker would have to * know the private key value x - and by assumption he doesn't, * because if he knew that he wouldn't be attacking k! * * (This trick doesn't, _per se_, protect against reuse of k. * Reuse of k is left to chance; all it does is prevent * _excessively high_ chances of reuse of k due to entropy * problems.) * * Thanks to Colin Plumb for the general idea of using x to * ensure k is hard to guess, and to the Cambridge University * Computer Security Group for helping to argue out all the * fine details. */ struct dss_key *dss = (struct dss_key *) key; SHA512_State ss; unsigned char digest[20], digest512[64]; Bignum proto_k, k, gkp, hash, kinv, hxr, r, s; unsigned char *bytes; int nbytes, i; SHA_Simple(data, datalen, digest); /* * Hash some identifying text plus x. */ SHA512_Init(&ss); SHA512_Bytes(&ss, "DSA deterministic k generator", 30); sha512_mpint(&ss, dss->x); SHA512_Final(&ss, digest512); /* * Now hash that digest plus the message hash. */ SHA512_Init(&ss); SHA512_Bytes(&ss, digest512, sizeof(digest512)); SHA512_Bytes(&ss, digest, sizeof(digest)); SHA512_Final(&ss, digest512); memset(&ss, 0, sizeof(ss)); /* * Now convert the result into a bignum, and reduce it mod q. */ proto_k = bignum_from_bytes(digest512, 64); k = bigmod(proto_k, dss->q); freebn(proto_k); memset(digest512, 0, sizeof(digest512)); /* * Now we have k, so just go ahead and compute the signature. */ gkp = modpow(dss->g, k, dss->p); /* g^k mod p */ r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */ freebn(gkp); hash = bignum_from_bytes(digest, 20); kinv = modinv(k, dss->q); /* k^-1 mod q */ hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */ s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */ freebn(hxr); freebn(kinv); freebn(hash); /* * Signature blob is * * string "ssh-dss" * string two 20-byte numbers r and s, end to end * * i.e. 4+7 + 4+40 bytes. */ nbytes = 4 + 7 + 4 + 40; bytes = snewn(nbytes, unsigned char); PUT_32BIT(bytes, 7); memcpy(bytes + 4, "ssh-dss", 7); PUT_32BIT(bytes + 4 + 7, 40); for (i = 0; i < 20; i++) { bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i); bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i); } freebn(r); freebn(s); *siglen = nbytes; return bytes; }
int main(){ long int i = 0,j = 0; long int primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; long int primefactorisation[110][26] = {0}; long int n,k = 0; long int queries = 0; long int temp[25] = {0}; long int prime_number; long int l = 0 , r=0 ,m=0,cnt=0; long long int ans = 1; for(i=2;i<=100;i++){ k = i; for(j=0;j<25 && k > 1;j++){ prime_number = primes[j]; while(k>1){ if(k % prime_number == 0){ primefactorisation[i][j]++; k = k/prime_number; } else break; } } } n = scan2(); for(i=1;i<=n;i++){ k = scan2(); for(j=0;j<25;j++){ temp[j] += primefactorisation[k][j]; c_factoristion_addition[i][j] = temp[j]; } } //Now then , i have the cumulative frequecies queries = scan2(); while(queries--){ l = scan2(); r = scan2(); m = scan2(); ans = 1 % m; for(j = 0; j < 25; j++) { cnt = c_factoristion_addition[r][j] - c_factoristion_addition[l-1][j]; if(cnt) ans = (ans * bigmod(primes[j], cnt, m))%m; if(ans == 0) break; } printf("%lld\n",ans); } return 0; }
/* p=a*2^n+1 n 2^n p a root 5 32 97 3 5 6 64 193 3 5 7 128 257 2 3 8 256 257 1 3 9 512 7681 15 17 10 1024 12289 12 11 11 2048 12289 6 11 12 4096 12289 3 11 13 8192 40961 5 3 14 16384 65537 4 3 15 32768 65537 2 3 16 65536 65537 1 3 17 131072 786433 6 10 18 262144 786433 3 10 (605028353, 2308, 3) 19 524288 5767169 11 3 20 1048576 7340033 7 3 21 2097152 23068673 11 3 22 4194304 104857601 25 3 23 8388608 167772161 20 3 24 16777216 167772161 10 3 25 33554432 167772161 5 3 (1107296257, 33, 10) 26 67108864 469762049 7 3 27 134217728 2013265921 15 31 */ int bigmod(long long a,int b){ if(b==0)return 1; return (bigmod((a*a)%P,b/2)*(b%2?a:1ll))%P; }
template<class _T> bigmod(_T b, _T p, _T m) { if(p == 0) return 1; else if(p % 2 == 0) return square(bigmod(b, p/2, m)) % m; return ((b % m) * bigmod(b, p-1, m)) % m; }
Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key, unsigned char *digest, int digest_len) { /* * The basic DSS signing algorithm is: * * - invent a random k between 1 and q-1 (exclusive). * - Compute r = (g^k mod p) mod q. * - Compute s = k^-1 * (hash + x*r) mod q. * * This has the dangerous properties that: * * - if an attacker in possession of the public key _and_ the * signature (for example, the host you just authenticated * to) can guess your k, he can reverse the computation of s * and work out x = r^-1 * (s*k - hash) mod q. That is, he * can deduce the private half of your key, and masquerade * as you for as long as the key is still valid. * * - since r is a function purely of k and the public key, if * the attacker only has a _range of possibilities_ for k * it's easy for him to work through them all and check each * one against r; he'll never be unsure of whether he's got * the right one. * * - if you ever sign two different hashes with the same k, it * will be immediately obvious because the two signatures * will have the same r, and moreover an attacker in * possession of both signatures (and the public key of * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q, * and from there deduce x as before. * * - the Bleichenbacher attack on DSA makes use of methods of * generating k which are significantly non-uniformly * distributed; in particular, generating a 160-bit random * number and reducing it mod q is right out. * * For this reason we must be pretty careful about how we * generate our k. Since this code runs on Windows, with no * particularly good system entropy sources, we can't trust our * RNG itself to produce properly unpredictable data. Hence, we * use a totally different scheme instead. * * What we do is to take a SHA-512 (_big_) hash of the private * key x, and then feed this into another SHA-512 hash that * also includes the message hash being signed. That is: * * proto_k = SHA512 ( SHA512(x) || SHA160(message) ) * * This number is 512 bits long, so reducing it mod q won't be * noticeably non-uniform. So * * k = proto_k mod q * * This has the interesting property that it's _deterministic_: * signing the same hash twice with the same key yields the * same signature. * * Despite this determinism, it's still not predictable to an * attacker, because in order to repeat the SHA-512 * construction that created it, the attacker would have to * know the private key value x - and by assumption he doesn't, * because if he knew that he wouldn't be attacking k! * * (This trick doesn't, _per se_, protect against reuse of k. * Reuse of k is left to chance; all it does is prevent * _excessively high_ chances of reuse of k due to entropy * problems.) * * Thanks to Colin Plumb for the general idea of using x to * ensure k is hard to guess, and to the Cambridge University * Computer Security Group for helping to argue out all the * fine details. */ SHA512_State ss; unsigned char digest512[64]; Bignum proto_k, k; /* * Hash some identifying text plus x. */ SHA512_Init(&ss); SHA512_Bytes(&ss, id_string, strlen(id_string) + 1); sha512_mpint(&ss, private_key); SHA512_Final(&ss, digest512); /* * Now hash that digest plus the message hash. */ SHA512_Init(&ss); SHA512_Bytes(&ss, digest512, sizeof(digest512)); SHA512_Bytes(&ss, digest, digest_len); while (1) { SHA512_State ss2 = ss; /* structure copy */ SHA512_Final(&ss2, digest512); smemclr(&ss2, sizeof(ss2)); /* * Now convert the result into a bignum, and reduce it mod q. */ proto_k = bignum_from_bytes(digest512, 64); k = bigmod(proto_k, modulus); freebn(proto_k); if (bignum_cmp(k, One) != 0 && bignum_cmp(k, Zero) != 0) { smemclr(&ss, sizeof(ss)); smemclr(digest512, sizeof(digest512)); return k; } /* Very unlikely we get here, but if so, k was unsuitable. */ freebn(k); /* Perturb the hash to think of a different k. */ SHA512_Bytes(&ss, "x", 1); /* Go round and try again. */ } }
int main(){ int query, i, j, k = 0, x, a, b; fread_unlocked(str, 1, 10000000, stdin); for (i = 0; i < 25; i++) ar[0][i] = 0; while (str[k] < 48 || str[k] > 57) k++; for (; str[k] > 47 && str[k] < 58; k++) n = T(n); for (i = 1; i <= n; i++){ x = 0; while (str[k] < 48 || str[k] > 57) k++; for (; str[k] > 47 && str[k] < 58; k++) x = T(x); for (j = 0; j < 25; j++){ ar[i][j] = ar[i - 1][j]; int p = primes[j]; while ((x % p) == 0){ ar[i][j]++; x /= p; } } } while (str[k] < 48 || str[k] > 57) k++; for (; str[k] > 47 && str[k] < 58; k++) q = T(q); for (query = 0; query < q; query++){ a = 0, b = 0, MOD = 0; while (str[k] < 48 || str[k] > 57) k++; for (; str[k] > 47 && str[k] < 58; k++) a = T(a); while (str[k] < 48 || str[k] > 57) k++; for (; str[k] > 47 && str[k] < 58; k++) b = T(b); while (str[k] < 48 || str[k] > 57) k++; for (; str[k] > 47 && str[k] < 58; k++) MOD = T(MOD); long long int res = 1; if (MOD > 1){ for (j = 0; j < 25; j++){ x = ar[b][j] - ar[a - 1][j]; if (x){ int y = bigmod(primes[j], x); res = (res * y) % MOD; } } } else res = 0; j = 0; x = res; for (; ;){ if (!x) break; str3[j++] = (x % 10) + 48; x *= 0.1; } if (!j) str3[j++] = 48; for (i = j - 1; i >= 0; i--) str2[ye++] = str3[i]; str2[ye++] = 10; } fwrite_unlocked(str2, 1, ye, stdout); return 0; }