Can you find it
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1487 Accepted Submission(s): 614
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
题目大意:
给你四个数
C,k1,b1,k2
,让你求上面的公式满足所有的
n
成立的
解题思路:
因为对所有的
n
都满足所以我们取两个特殊的情况也就是
My Code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
LL Pow(LL a, LL b, LL MOD){
LL ans = 1;
while(b){
if(b&1)
ans = (ans*a)%MOD;
b>>=1;
a = (a*a)%MOD;
}
return ans;
}
int main()
{
LL C, k1, b1, k2;
int cas = 1;
while(cin>>C>>k1>>b1>>k2){
LL tp = k1+b1;
int ok = 0;
printf("Case #%d:\n",cas++);;
for(LL a=1; a<C; a++){
LL tmp = Pow(a, tp , C);
LL b = C - tmp;
if((Pow(a,tp+k1, C)+Pow(b, k2+1, C))%C == 0){
ok = 1;
printf("%I64d %I64d\n",a,b);
}
}
if(!ok)
puts("-1");
}
return 0;
}