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
(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.Examples
Input
23 1 1 2
Output
Case #1:
1 22
题意:给出一个质数 c,三个正整数 k1,b1,k2,对于公式 ,若能找出最小的 a,b 则输出,若找不出则输出 -1
思路:公式题
对于公式
当 n=1 时, ①
当 n=2 时, ②
令 * ②,
③
令 ③-②,
即:
由于 a、b < c,只需要枚举 a 的值(1~c-1),通过快速幂计算 、
,从而算出 b,再求出
,比较
是否等于
即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define N 500001
#define LL long long
using namespace std;
LL Pow_Mod(LL a, LL b, LL m)
{
LL res=1;
while(b)
{
if(b&1)
res=(res*a)%m;
a=(a*a)%m;
b>>=1;
}
return res;
}
int main(){
LL c,k1,b1,k2;
int Case=1;
while(scanf("%lld%lld%lld%lld",&c,&k1,&b1,&k2)!=EOF){
bool flag=false;
printf("Case #%d:\n",Case++);
for(LL i=1;i<=c-1;i++){
LL a=Pow_Mod(i,k1,c);
LL b=c-Pow_Mod(i,k1+b1,c);
LL temp=Pow_Mod(b,k2,c);
if(a==temp){
flag=true;
printf("%lld %lld\n",i,b);
}
}
if(!flag)
printf("-1\n");
}
return 0;
}