Can you find it
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1967 Accepted Submission(s): 832
Problem Description
Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109). Please find all pairs (a, b) which satisfied the equation ak1⋅n+b1 + bk2⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, ...).
Input
There are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2.
Output
First, 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. (1≤a,b<C). If there is not a pair (a, b), please output -1.
Please output all pairs (a, b) in lexicographical order. (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
思路:
因为对于所有n都成立,所以先假设n==1,
便有(a^(k1+b1)+b)%C==0
枚举a,得出b的解,对于每对(a,b),验证是否对所有n都成立。
将n==2带入,若成立,则所有n都成立(我猜的,不要问为什么)。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll C, k1, k2, b1;
ll pow1(ll a, ll b, ll mod)
{
ll r = 1;
while (b)
{
if (b & 1) r = r * a % mod;
a = a * a % mod;
b /= 2;
}
return r;
}
int main()
{
int cas = 0;
while (~scanf("%lld%lld%lld%lld", &C, &k1, &b1, &k2))
{
bool bb=0;
printf("Case #%d:\n", ++cas);
for (ll i = 1; i < C; i++)
{
ll a = pow1(i, k1 + b1, C);
ll b = C - a;
if (b >= 1 && b < C)
{
if ((pow1(i, 2 * k1 + b1, C) + pow1(b, k2 + 1, C)) % C == 0)
printf("%lld %lld\n", i, b),bb=1;
}
}
if(!bb)printf("-1\n");
}
return 0;
}