Can you find it —HDU5478
Given a prime numberC(1≤C≤2×105)C(1≤C≤2×105), and three integers k1,b1,k2(1≤k1,k2,b1≤109)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,...).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)(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
题意:
让你输出所有满足的式子 ak1⋅n+b1+bk2⋅n−k2+1=0(mod C)(n=1,2,3,...).ak1⋅n+b1+bk2⋅n−k2+1=0(mod C)(n=1,2,3,...). 的数对(a,b)
分析:
我们按照数学归纳法的思想来分析一下
1) 验证n=1时是否成立
2)假设n = n时成立即
我们只需要验证n+1时成立即可
3)n+1时的式子相当于
成立
根据乘法取模运算的分配律
因为
所以只需要让ak1和bk2模C后值相同即可,也就相当于模C后乘上了相同的系数,因而证明了对所有的n式子成立ak1和bk2模C后值相同即可,也就相当于模C后乘上了相同的系数,因而证明了对所有的n式子成立
此时ab满足条件
我们只需要在验证n=1成立和n=2成立即可证明ak1,bk2模C后相同的关系ak1,bk2模C后相同的关系
所以n=1时我们发现bk2n−k+1=bbk2n−k+1=b,因此通过枚举a,我们可以通过计算ak1+b1ak1+b1然后用C减去就得到了b,进而为了满足n=2的时候成立,我们只需计算ak1,bk2a1k,b2k模C是否相等即可
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll q_pow(ll a,ll b,ll mod){
ll ans = 1;
while(b){
if(b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main(){
int cas = 1;
int c,k1,b1,k2;
while(~scanf("%d%d%d%d",&c,&k1,&b1,&k2)){
int flag = 0;
printf("Case #%d:\n",cas++);
for(ll a = 1; a < c; a++){
ll b = c - q_pow(a,k1+b1,c);
if(q_pow(a,k1,c) == q_pow(b,k2,c)){
flag = 1;
printf("%lld %lld\n",a,b);
}
}
if(!flag) printf("-1\n");
}
return 0;
}