void test() {
    for (int a = -20; a < 20; a++) {
        for (int b = 2; b < 40; b++) {
            if (gcd(a, b) == 1) {
                assert_equal(1, mod((a * mod_inv(a, b)), b));
            } else {
                assert_equal(-1, mod_inv(a, b));
            }
        }
    }
}
void test() {
  for (ll a = -20; a < 20; a++) {
    for (ll b = 2; b < 40; b++) {
      if (gcd(a, b) == 1) {
        assert_equal(1LL, smod((a * mod_inv(a, b)), b));
      } else {
        assert_equal(-1LL, mod_inv(a, b));
      }
    }
  }
}
예제 #3
0
파일: mod_inv.c 프로젝트: sduc/apkc
int main(int argc, const char **argv)
{
    int64_t a;
    int64_t b;
    int64_t d,x;

    if(argc > 2)
    {
        sscanf(argv[1],"%"PRId64,&a);
        sscanf(argv[2],"%"PRId64,&b);
    }
    else
    {
        printf("Need at least two argument\n");
        return 1;
    }

    if(~b & 0x1)
    {
        printf("b must be an odd integer\n");
        return 1;
    }

    mod_inv(a,b,&d,&x);

    printf("The inverse is : %lld\n",x);
    printf("The gcd is : %lld\n",d);

}
예제 #4
0
파일: lcm.cpp 프로젝트: M4573R/pc-code
int main()
{
    int T;
    scanf("%d", &T);

    int ncase = 0;
    while (T--) {
        scanf("%d", &C);

        i64 ans = 1;
        i64 fac = 1;
        while (C--) {
            scanf("%d%d", &P, &a);
            ans = (ans * mod_pow(P, a, MOD)) % MOD;

            i64 x = mod_pow(P, a, MOD) - 1 + MOD;
            x = x * (mod_inv(P - 1, MOD) + MOD) % MOD;

            i64 y = a + 1;
            y = y * mod_pow(P, a, MOD) % MOD;

            x = (x + y) % MOD;
            fac = fac * x % MOD;
        }

        ans = (ans + fac) % MOD;

        printf("Case %d: %lld\n", ++ncase, ans);
    }

    return 0;
}
예제 #5
0
파일: 4816.cpp 프로젝트: tangjz/acm-icpc
int main()
{
	f[1] = f[2] = 1;
	for(int i = 3; i < maxn; ++i)
		if((f[i] = f[i - 1] + f[i - 2]) >= mod)
			f[i] -= mod;
	f[0] = g[0] = g[1] = g[2] = 1;
	for(int i = 3; i < maxn; ++i)
	{
		int val = mod_inv(f[i]);
		for(int j = i + i; j < maxn; j += i)
			f[j] = (LL)f[j] * val % mod;
		f[i] = (LL)f[i - 1] * f[i] % mod;
		g[i] = (LL)g[i - 1] * val % mod;
	}
	scanf("%d", &t);
	while(t--)
	{
		int ans = 1;
		scanf("%d%d", &n, &m);
		for(int i = 3, j, u, v; i <= n && i <= m; i = j + 1)
		{
			u = n / i;
			v = m / i;
			j = std::min(n / u, m / v);
			ans = (LL)ans * mod_pow((LL)f[j] * g[i - 1] % mod, (LL)u * v % (mod - 1)) % mod;
		}
		printf("%d\n", ans);
	}
	return 0;
}
예제 #6
0
파일: 759F.cpp 프로젝트: tangjz/acm-icpc
Node gen(int L, int R, int L1, int R1, int pw) // [L, R), L != R, |L| = |R-1| = len, 10^len = pw
{
	if(pw == 1)
		return (Node){(int)((((L + R - 1LL) * (R - L)) >> 1) % mod), 1};
	int ivs = mod_inv(pw - 1), ppw = mod_pow(pw, L1 < R1 ? R1 - L1 : R1 - L1 + mod - 1);
	int ret = ((LL)L * ppw - R + (ppw - 1LL) * ivs) % mod * ivs % mod;
	if(ret < 0)
		ret += mod;
	return (Node){ret, ppw};
}
예제 #7
0
파일: tmp.cpp 프로젝트: YnkDK/dynAlg14
int main() {
//	std::cout << mod_add(2147483645UL, 1231) << std::endl;
//	std::cout << (2147483645ULL+1231) % P << std::endl;

//	std::cout << mod_sub(1231, 2147483645UL) << std::endl;
//	std::cout << ((1231-2147483645UL)+P) % P << std::endl;

//	std::cout << mod_mul(2147483645UL, 2147483645UL) << std::endl;
//	std::cout << (2147483645UL * 2147483645UL) % P << std::endl;
    std::cout << mod_inv(123125) << " < " << P << std::endl;
}
예제 #8
0
pair<long,long> crt(const vector<pair<long,long> >& rems) {
    long x = 0, m = 1, y, n;
    for (auto& p : rems) {
        tie(y, n) = p;
        long g = __gcd(m, n), u = x - y % m + m;
        if (u % g != 0) return {-1, -1};
        m /= g;
        long t = mod_inv(n/g, m) * (u/g) % m;
        m *= n, x = (t * n + y) % m;
    }
    return {x, m};
}
예제 #9
0
static void decipher(FILE* msg, int k1, int k2){
  int inv = mod_inv(k1,26);
  char ch, cip;
  while(fscanf(msg,"%c",&ch) != EOF){
    if(ch>=65 && ch <= 90){
      /* uppercase */
      cip = (((ch - 65)-k2)*inv + 26)%26 + 65;
    } else if(ch>=97 && ch <= 122){
      /* lowercase */
      cip = (((ch - 97)-k2)*inv + 26)%26 + 97;
    } else {
      /* space or other characters */
      cip = ch;
    }
    fprintf(stdout,"%c",cip);
    if(log_f)
      fprintf(log_file,"%c",cip);
  }
}
예제 #10
0
파일: hdu_3037.cpp 프로젝트: keroro520/ACM
ll C(ll N, ll M, ll P)
{
  if(N < M) return 0;
  return A[N] * mod_inv(A[M], P) % P * mod_inv(A[N-M], P) % P;
}
예제 #11
0
파일: 5684.cpp 프로젝트: tangjz/acm-icpc
int mod_inv(int x)
{
	return x <= 1 ? x : p - p / x * (LL)mod_inv(p % x) % p;
}
예제 #12
0
파일: e2.C 프로젝트: Janusz13/VBF
u4byte *set_key(const u4byte in_key[], const u4byte key_len)
{   u4byte  lk[8], v[2], lout[8];
    u4byte  i, j, k, w;

    if(!lb_init)
    {
        for(i = 0; i < 256; ++i)
        {
            l_box[0][i] = ((u4byte)(s_box[i]));
            l_box[1][i] = ((u4byte)(s_box[i])) <<  8;
            l_box[2][i] = ((u4byte)(s_box[i])) << 16;
            l_box[3][i] = ((u4byte)(s_box[i])) << 24;
        }

        lb_init = 1;
    }

    v[0] = bswap(v_0); v[1] = bswap(v_1);

    lk[0] = io_swap(in_key[0]); lk[1] = io_swap(in_key[1]);
    lk[2] = io_swap(in_key[2]); lk[3] = io_swap(in_key[3]);

    lk[4] = io_swap(key_len > 128 ? in_key[4] : k2_0);
    lk[5] = io_swap(key_len > 128 ? in_key[5] : k2_1);

    lk[6] = io_swap(key_len > 192 ? in_key[6] : k3_0);
    lk[7] = io_swap(key_len > 192 ? in_key[7] : k3_1);

    g_fun(lk, lout, v);

    for(i = 0; i < 8; ++i)
    {
        g_fun(lk, lout, v);

        for(j = 0; j < 4; ++j)
        {
            // this is complex because of a byte swap in each 32 bit output word

            k = 2 * (48 - 16 * j + 2 * (i / 2) - i % 2);

            ((u1byte*)l_key)[k + 3]   = ((u1byte*)lout)[j];
            ((u1byte*)l_key)[k + 2]   = ((u1byte*)lout)[j + 16];

            ((u1byte*)l_key)[k + 19]  = ((u1byte*)lout)[j +  8];
            ((u1byte*)l_key)[k + 18]  = ((u1byte*)lout)[j + 24];

            ((u1byte*)l_key)[k + 131] = ((u1byte*)lout)[j +  4];
            ((u1byte*)l_key)[k + 130] = ((u1byte*)lout)[j + 20];

            ((u1byte*)l_key)[k + 147] = ((u1byte*)lout)[j + 12];
            ((u1byte*)l_key)[k + 146] = ((u1byte*)lout)[j + 28];
        }
    }

    for(i = 52; i < 60; ++i)
    {
        l_key[i] |= 1; l_key[i + 12] = mod_inv(l_key[i]);
    }

    for(i = 0; i < 48; i += 4)
    {
        bp2_fun(l_key[i], l_key[i + 1]);
    }

    return (u4byte*)&l_key;
};
예제 #13
0
파일: 4816.cpp 프로젝트: tangjz/acm-icpc
inline int mod_inv(int x)
{
	return x <= 1 ? x : mod - (int)(mod / x * (LL)mod_inv(mod % x) % mod);
}
예제 #14
0
파일: pi.c 프로젝트: maweaver/Exercises
static long mod_sum_binom(long k, long n, long m)
{
	long j;
	long A, B, C, C_acc;
	long a, b, a_star, b_star;
	
	size_t         num_factors_m;
	struct factor *prime_factors_m;
	struct factor *cur_fact;
	
	struct factor *r      = NULL;
	struct factor *cur_r  = NULL;
	
	if(k > n / 2) {
		return int_modulus(expt_mod(2, n, m) - mod_sum_binom(n - k - 1, n, m), m);
	}
	
	/* Step 1 */
	prime_factors_m = calc_prime_factors(m, k, &num_factors_m);
	
	/* Step 2 */
	A = 1; B = 1; C = 1;
	
	for(j = 0; j < num_factors_m; j++) {
		if(!cur_r) {
			r = malloc(sizeof(struct factor));
			cur_r = r;
		} else {
			cur_r->next = malloc(sizeof(struct factor));
			cur_r = cur_r->next;
		}
		cur_r->value = 1;
		cur_r->next = NULL;
	}
	
	for(j = 1; j <= k; j++) {
		
		/* Step 3a */
		a = n - j + 1;
		b = j;
		
		/* Steps 3b and 3c */
		cur_fact = prime_factors_m;
		cur_r = r;
		a_star = a;
		b_star = b;
		while(cur_fact) {
			while(a_star % cur_fact->value == 0) {
				a_star /= cur_fact->value;
				cur_r->value *= cur_fact->value;
			}
			
			while(b_star % cur_fact->value == 0) {
				b_star /= cur_fact->value;
				cur_r->value /= cur_fact->value;
			}
			
			cur_fact = cur_fact->next;
			cur_r = cur_r->next;
		}
		
		/* Step 3d */
		A = mod_mul(A, a_star, m);
		B = mod_mul(B, b_star, m);
	
		C = mod_mul(C, b_star, m);
	
		C_acc = A;
		for(cur_r = r; cur_r; cur_r = cur_r ->next) {
			C_acc = mod_mul(C_acc, cur_r->value, m);
		}
		
		C = int_modulus(C + C_acc, m);
	}
	
	free_factors(prime_factors_m);
	free_factors(r);
	
	/* Step 4 */
	return mod_mul(C, mod_inv(B, m), m);
}
예제 #15
0
파일: 1229.cpp 프로젝트: tangjz/acm-icpc
int main()
{
	iact[1] = 1;
	for(int i = 2; i < maxm; ++i)
		iact[i] = mod - mod / i * (LL)iact[mod % i] % mod;
	iact[0] = 1;
	for(int i = 1; i < maxm; ++i)
		iact[i] = (LL)iact[i - 1] * iact[i] % mod;
	for(int i = 0; i < maxm; ++i)
	{
		c[i][0] = c[i][i] = 1;
		for(int j = 1; j < i; ++j)
			if((c[i][j] = c[i - 1][j - 1] + c[i - 1][j]) >= mod)
				c[i][j] -= mod;
	}
	LL tn, tq;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%lld%d%lld", &tn, &m, &tq);
		if((q = tq % mod) == 1)
		{
			tot = 0;
			memset(f + 1, 0, (m + 1) * sizeof(int));
			f[1] = 1;
			for(int i = 2; i <= m + 1; ++i)
			{
				if(!f[i])
				{
					prime[tot++] = i;
					f[i] = mod_pow(i, m);
				}
				for(int j = 0, o; j < tot && (o = i * prime[j]) <= m + 1; ++j)
				{
					f[o] = (LL)f[i] * f[prime[j]] % mod;
					if(i % prime[j] == 0)
						break;
				}
			}
			for(int i = 2; i <= m + 1; ++i)
				if((f[i] += f[i - 1]) >= mod)
					f[i] -= mod;
			if(tn <= m + 1)
			{
				printf("%d\n", f[(int)tn]);
				continue;
			}
			pre[0] = tn % mod;
			for(int i = 1; i <= m + 1; ++i)
				pre[i] = (tn - i) % mod * pre[i - 1] % mod;
			suf[m + 1] = (tn - m - 1) % mod;
			for(int i = m; i >= 0; --i)
				suf[i] = (tn - i) % mod * suf[i + 1] % mod;
			ans = 0;
			for(int i = 0; i <= m + 1; ++i)
			{
				int tmp = (LL)f[i] * iact[i] % mod * iact[m + 1 - i] % mod;
				if(i > 0)
					tmp = (LL)tmp * pre[i - 1] % mod;
				if(i <= m)
					tmp = (LL)tmp * suf[i + 1] % mod;
				if(((m + 1 - i) & 1) && tmp > 0)
					tmp = mod - tmp;
				if((ans += tmp) >= mod)
					ans -= mod;
			}
			printf("%d\n", ans);
		}
		else if(q > 0)
		{
			n = tn % mod;
			int sei = mod_pow(q, (tn + 1) % (mod - 1)), iei = mod_inv(q - 1);
			if((f[0] = (LL)(sei - q) * iei % mod) < 0)
				f[0] += mod;
			for(int i = 1, coeff = n; i <= m; ++i, coeff = (LL)coeff * n % mod)
			{
				f[i] = (LL)coeff * sei % mod;
				for(int j = 0; j < i; ++j)
				{
					int tmp = (LL)c[i][j] * f[j] % mod;
					if(((i - j) & 1) && tmp > 0)
						tmp = mod - tmp;
					if((f[i] += tmp) >= mod)
						f[i] -= mod;
				}
				f[i] = (LL)f[i] * iei % mod;
			}
			printf("%d\n", f[m]);
		}
		else
			puts("0");
	}
	return 0;
}
예제 #16
0
파일: 1229.cpp 프로젝트: tangjz/acm-icpc
int mod_inv(int x)
{
	return x <= 1 ? x : mod - mod / x * (LL)mod_inv(mod % x) % mod;
}
예제 #17
0
파일: 759F.cpp 프로젝트: tangjz/acm-icpc
int mod_inv(int x)
{
	return x <= 1 ? x : (int)(mod - mod / x * (LL)mod_inv(mod % x) % mod);
}