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
Source
Recommend
因为式子对所有的n都满足,我们把n=1和n=2代入.可以推出:a^k1=b^k2。b=c-a^(k1+b1) 。
我们从1到c-1枚举a。再求出b,然后判断a^k1是否等于b^k2即可。
#include<iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
ll fast(ll a,ll b,ll mod)
{
ll res=1;
while(b)
{
if(b&1)
res=(res*a)%mod;
a=(a*a)%mod;
b/=2;
}
return res%mod;
}
int main()
{
ll c,k1,b1,k2;
int Case=0;
while(cin>>c>>k1>>b1>>k2)
{
cout<<"Case #"<<++Case<<":"<<endl;
ll i;
int flag=0;
for(i=1; i<c; i++)
{
ll ans=fast(i,k1,c);
ll ans1=c-fast(i,k1+b1,c);
ll ans2=fast(ans1,k2,c);
if(ans==ans2)
{
flag=1;
cout<<i<<" "<<ans1<<endl;
}
}
if(!flag)
puts("-1");
}
return 0;
}