Example #1
0
int main() {
	int n, Q;

	scanf("%d%d", &n, &Q);
	mod = n + 1;
	for ( int i = 0; i < n; i ++ ) {
		scanf("%d", &A[i]); A[i] %= mod;
	}
	for ( int i = 0; i < n; i ++ ) {
		scanf("%d", &B[i]); B[i] %= mod;
	}
	root = getDiscreteRoot(n + 1);
	memcpy(coef, A, sizeof(coef));
	fft(0, 1, n, 0);
	memcpy(resA, res, sizeof(res));
	memcpy(coef, B, sizeof(coef));
	fft(0, 1, n, 0);
	memcpy(resB, res, sizeof(res));
	for ( int i = 0; i < n; i ++ )
		C[i] = mul(resA[i], powMod(resB[i], Q));
	memcpy(coef, C, sizeof(coef));
	fft(0, 1, n, 1);
	memcpy(resC, res, sizeof(res));
	for ( int i = 0; i < n; i ++ ) resC[i] = mul(resC[i], powMod(n, mod - 2));
	for ( int i = 0; i < n; i ++ ) printf("%d\n", resC[i]); 
}
Example #2
0
 int superPow(int a, vector<int>& b) {
     if(b.empty())
         return 1;
     int m = b.back();
     b.pop_back();
     return powMod(superPow(a, b), 10) * powMod(a, m) % base;
 }
Example #3
0
bool millerRabin(num n, num witness) {
    if (n == 2 || n == 3)
        return true;
    if (n <= 1 || !(n & 1))
        return false;

    // Write n-1 as m*2^k by factoring powers of 2 from n-1
    int k = 0;

    for (num i = n - 1; !(i & 1); i >>= 1) {
        ++k;
    }

    num m = (n - 1) / (1 << k);

    num x = powMod(witness, m, n);

    if (x == 1 || x == n - 1)
        return true;

    for (int r = 1; r <= k - 1; ++r) {
        x = powMod(x, 2, n);
        if (x == 1)
            return false;
        if (x == n - 1)
            return true;
    }

    return false;
}
Example #4
0
 int superPow(int a, vector<int>& b) {
     int n = b.size();
     if (n == 0) {
         return 1;
     }
     
     int power = 1;
     a = a % MODBASE;
     for (int i = 0; i < n; i++) {
         power = (powMod(power, 10) * powMod(a, b[i])) % MODBASE;
     }
     
     return power;
 }
Example #5
0
void fft(int delta, int step, int size, bool nega) {
	if ( size == 1 ) { res[delta] = coef[delta]; return; }
	int k;
	for ( k = 2; ; k ++ ) if ( size % k == 0 ) break;
	for ( int i = 0; i < k; i ++ ) fft(delta + i * step,  step * k, size / k, nega);
	int acc = 1, pri = powMod(root, (mod - 1) / size);
	if ( nega ) pri = powMod(pri, mod - 2);
	for ( int i = 0; i < size; i ++, acc = mul(acc, pri) ) {
		tmp[delta + i * step] = 0;
		for ( int j = 0, cur = 1; j < k; j ++, cur = mul(cur, acc) )
			add(tmp[delta + i * step], mul(cur, res[delta + j * step + (i % (size / k)) * step * k]));
	}
	for ( int i = 0; i < size; i ++ ) res[delta + i * step] = tmp[delta + i * step];
}
Example #6
0
Int geometricProgression(Int x,Int k,Int m){
	if(k==0)return 0;
	Int s=geometricProgression(x,k/2,m),t=powMod(x,k/2,m);
	s=s*(t+1)%m;
	if(k%2==1)s=(s*x+1)%=m;
	return s;
}
Example #7
0
void solve(int a, int b, int p)
{
    if (!b) {
        outDate(0);
        return;
    }
    int g = PriRoot(p);
    int x = BSGS(g, b, p);
    LL y, t, d = ext_gcd(a, p-1, y, t);
    if (x == -1 || x%d) {
        puts("Transmission error");
        return;
    }
/*    
    y=(y*(x/d))%(p-1);
    d=(p-1)/d;
    int ti = 0;
    for (y=(y%d+d)%d;y<p-1;y+=d)
        rec[ti++] = powMod(g,y,p);
    d = ti;
    */

y %= (p-1)/d;
    if (y < 0) y += (p-1)/d;
    
    for (int i = 0; i < d; ++i) {
        LL ty = y*(x/d)+(p-1)/d*i;
        rec[i] = powMod(g, ty, p);
    }

    std::sort(rec, rec + d);
    for (int i = 0; i < d; ++i) outDate(rec[i]);
}
Example #8
0
long long gao(long long a, long long b) {
    if (b == 1) {
        return 0;
    } else {
        long long d = phi(b);
        return powMod(a, d + gao(a, d), b);
    }
}
Example #9
0
int getDiscreteRoot(int p) {
	for ( int x = 2; ; x ++ ) {
		bool fail = false;
		for ( int y = 2; !fail && y * y <= p; y ++ )
			if ( (p - 1) % y == 0 && powMod(x, (p - 1) / y) == 1 ) 
				fail = true;
		if ( ! fail ) return x;
	}
}
Example #10
0
int main() {
	freopen("t.in", "r", stdin);
	scanf("%d", &d);
	scanf("%s", str);
	n = strlen(str);
	int t;
	for ( t = n-1; t >= 0 && str[t] == 'z'; t -- );
	if ( t == -1 ) {
		printf("Impossible\n");
		return 0;
	}
	str[t]++;
	for ( int i = t+1; i < n; i ++ ) 
		str[i] = 'a';
	powD = powMod(BASE, d-1);
	kInv = powMod(BASE, kMod-2);
	dfs(0, 0, 0, 1);
	printf("Impossible\n");
}
int PriRoot(int p)
{
    if (p==2) return 1;
    int phi = p - 1, g, i;
    Factor(phi);
    for (g = 2; g < p; ++g) {
        for (i = 0; i < fCnt; ++i)
            if (powMod(g, phi/pf[i], p) == 1) break;
        if (i == fCnt) break;
    }
    return g;
}
Example #12
0
int primality(int num) {
	int randbase, val, i;
	if (num == 1) {
		return 0;
	}
	for (i=1; i<=100 && i<=num; i++) {
		randbase = (rand() % (num - 1)) + 1;
		val = powMod(randbase, num - 1, num);
		if (val != 1) {
			return 0;
		}
	}
	return 1;
}
Example #13
0
vector<int> QuadraticResidue(LL a, LL b, LL c, int P) {
	int h, t; LL r1, r2, delta, pb = 0;
	a = normalize(a, P); b = normalize(b, P); c = normalize(c, P);
	if (P == 2) { vector<int> res;
		if (c % P == 0) res.push_back(0);
		if ((a + b + c) % P == 0) res.push_back(1);
		return res;
	} delta = b * rev(a + a, P) % P;
	a = normalize(-c * rev(a, P) + delta * delta, P);
	if (powMod(a, P / 2, P) + 1 == P) return vector<int>(0);
	for (t = 0, h = P / 2; h % 2 == 0; ++t, h /= 2);
	r1 = powMod(a, h / 2, P);
	if (t > 0) { do b = random() % (P - 2) + 2;
		while (powMod(b, P / 2, P) + 1 != P); }
	for (int i = 1; i <= t; ++i) {
		LL d = r1 * r1 % P * a % P;
		for (int j = 1; j <= t - i; ++j) d = d * d % P;
		if (d + 1 == P) r1 = r1 * pb % P; pb = pb * pb % P;
	} r1 = a * r1 % P; r2 = P - r1;
	r1 = normalize(r1 - delta, P); r2 = normalize(r2 - delta, P);
	if (r1 > r2) swap(r1, r2); vector<int> res(1, r1);
	if (r1 != r2) res.push_back(r2);
	return res;
}
Example #14
0
int primality(int num) {
	int randbase, val, i;
	srand(time(NULL));
	if (num == 1) {
		return 0;
	}
	for (i=1; i<=100 && i<=num; i++) {
		randbase = (rand() % (num - 1)) + 1;
		printf("%d\n", randbase);
		val = powMod(randbase, num - 1, num);
		if (val != 1) {
			return 0;
		}
	}
	return 1;
}
Example #15
0
    bool BigInt::isPrime() {
        BigInt* curr = new BigInt(2);
        BigInt* end = new BigInt(200);
        BigInt* base = new BigInt();
        BigInt* deg = (new BigInt(*this))->sub(*ONE);

        bool ret = true;

        while (*curr < *end) {
            base->copy(*this);
            if (*base->mod(*curr) == *ZERO) {
                ret = false;
                break;
            }
            curr->add(*ONE);
        }

        delete curr;
        delete end;
        delete base;

        vector<BigInt*> *data = new vector<BigInt*> ();
        for (int i = 0; i < 200; ++i) {
            data->push_back(new BigInt(rand()));
        }

        powMod(data, *deg, *this);
        
        for(int i = 0; i < data->size(); ++i) {
            if(ret && *data->at(i) != *ONE) {
                ret = false;
            }
            delete data->at(i);
        }
        delete data;
        delete deg;

        return ret;
    }
void solve(int a, int b, int p)
{
    if (!b) {
        outDate(0);
        return;
    }
    int g = PriRoot(p);
    int x = BSGS(g, b, p);
    LL y, t, d = ext_gcd(a, p-1, y, t);
    y %= (p-1)/d;
    if (y < 0) y += (p-1)/d;
//    printf("%d %d %d %d\n", g, x, y, d);
    if (x == -1 || x%d) {
        puts("Transmission error");
        return;
    }
    for (int i = 0; i < d; ++i) {
        LL ty = y*(x/d)+(p-1)/d*i;
        rec[i] = powMod(g, ty, p);
    }
    std::sort(rec, rec + d);
    for (int i = 0; i < d; ++i) outDate(rec[i]);
}
Example #17
0
int main() {
	freopen("t.in", "r", stdin);
	prework();
//	check();
	int TST;
	scanf("%d", &TST);
	while ( TST -- ) {
		scanf("%d", &K);
		for ( int i = 0; i < K; i ++ ) {
			scanf("%d%d", &A[i], &B[i]);
		}
		int ans = 0;
		for ( int d = 1; d <= B[0]; d ++ ) {
			int acc = phi[d];
			for ( int i = 0; i < K; i ++ )
				acc = mul(acc, B[i]/d-(A[i]-1)/d);
			add(ans, acc);
		}
		for ( int i = 0; i < K; i ++ )
			ans = mul(ans, powMod(B[i]-A[i]+1, kMod-2));
		ans = (-ans + kMod) % kMod;
		printf("%d\n", ans);
	}
}
Example #18
0
Int powMod(const Int &x, const Int &k, const Int &m) {
	if (k == 0)     return 1;
	if (k % 2 == 0) return powMod(x*x % m, k/2, m);
	else            return x*powMod(x, k-1, m) % m;
}
Example #19
0
 Hash operator + (const Hash&b) {
     return Hash(len + b.len, value * powMod(MAGIC, b.len) + b.value);
 }
Example #20
0
int main(int argc, char* argv[])
{
	char *file1 = argv[1];
	char *file2 = argv[3];
	char *file3 = argv[4];
	char *file4 = NULL;
	char op = argv[2][0];
	int bin = 0;
	struct bNum n, m, md;

	if (argc == 6)
	{
		if (!strcmp(argv[5], "-b"))
			bin = 1;
		else
			file4 = argv[5];
	}

	if (argc == 7)
	{
		bin = 1;
		file4 = argv[6];
	}

	if (bin == 1)
	{
		n = ReadFromBFile(file1);
		m = ReadFromBFile(file2);
		if (file4 != NULL)
			md = ReadFromBFile(file4);
	}
	else
	{
		n = ReadFromTFile(file1);
		m = ReadFromTFile(file2);
		if (file4 != NULL)
			md = ReadFromTFile(file4);
	}

	struct bNum k;
	k.size = 0;
	k.sign = 0;
	k.nums = NULL;
	if (argv[2][0] == '+')
		k = Sum1(n, m);

	if (argv[2][0] == '-')
	{
		struct bNum minusB = mins(m);
		k = Sum1(n, minusB);
		free(minusB.nums);
	}

	if (argv[2][0] == '*')
		k = mul(n, m);

	if (argv[2][0] == '/')
	{
		struct bNum q;
		k = dividing(n, m, &q);
		free(q.nums);
	}

	if (argv[2][0] == '%')
	{
		struct bNum q = dividing(n, m, &k);
		free(q.nums);
	}

	if (file4 != NULL)
	{
		struct bNum tmp;
		struct bNum q = dividing(k, md, &tmp);
		free(q.nums);
		free(k.nums);
		k = tmp;
	}

	if (argv[2][0] == '^')
	{
		if (file4 == NULL)
			k = pPow(n, m);
		else
			k = powMod(n, m, md);
	}

	if (bin == 1)
		WriteToBFile(file3, k);
	else
		WriteToTFile(file3, k);

	free(n.nums);
	free(m.nums);
	free(k.nums);
	if (file4 != NULL)
		free(md.nums);

	return 0;
}