[快速幂] K - Can you find it 2015 ACM/ICPC Asia Regional Shanghai Online K

https://vjudge.net/contest/237063#problem/K

Given a prime number  C(1C2×105)C(1≤C≤2×105), and three integers k1, b1, k2  (1k1,k2,b1109)(1≤k1,k2,b1≤109). Please find all pairs (a, b) which satisfied the equation  ak1n+b1ak1⋅n+b1 +  bk2nk2+1bk2⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, ...).
InputThere are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2.OutputFirst, please output "Case #k: ", k is the number of test case. See sample output for more detail. 
Please output all pairs (a, b) in lexicographical order.  (1a,b<C)(1≤a,b<C). If there is not a pair (a, b), please output -1.Sample Input
23 1 1 2
Sample Output
Case #1:
1 22



#include <bits/stdc++.h>
using namespace std;

long long c, k1, b1, k2;

long long quickpow(long long a, long long b)
{
	long long ans = 1;
	long long base = a;
	while (b)
	{
		if (b & 1)
			ans = ans*base%c;
		
		base =base*base%c;
		b = b >> 1;
	}
	return ans;
}

int main()
{
	int T = 0;
	while (~scanf("%lld %lld %lld %lld", &c, &k1, &b1, &k2))
	{
		printf("Case #%d:\n", ++T);
		
		bool flag = 0;
		for (long long a = 1; a < c; a++)
		{
			// 取特殊 n = 1 时 枚举 a 求对应的唯一 b
			long long u = k1 + b1;
			long long v = quickpow(a, u) % c;
			long long b = c - v;
			if (b == c)
				continue;
			if (quickpow(a, k1) == quickpow(b, k2))
			{  // 每次递推乘相同的数 仍为C的倍数
				printf("%lld %lld\n", a, b);
				flag = 1;
			}
		}
		if (!flag)
			printf("-1\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/80962562