您的位置:首页 > 大数据 > 人工智能

HDU 5478 Can you find it(数学归纳法 + 快速幂)——2015 ACM/ICPC Asia Regional Shanghai Online

2016-10-25 21:06 465 查看
传送门

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


[align=left]Problem Description[/align] 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, …).
[align=left]Input[/align] There are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2.
[align=left]Output[/align] 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.
[align=left]Sample Input[/align]
23 1 1 2

[align=left]Sample Output[/align]
Case #1:

1 22


题目大意:

给你四个数 C,k1,b1,k2,让你求上面的公式满足所有的 n成立的 (a,b) 的对,并且将 (a,b) 按照 a 的字典序输出,如果没有输出 −1。

解题思路:

因为对所有的 n 都满足所以我们取两个特殊的情况也就是 n==1 和 n==2 的情况,然后解出一对 (a,b) ,如果这个 (a,b) 满足的话,对于所有的 n 也是满足的,可以根据数学归纳法证明一下,所以现在就是暴力找 a 就行了。

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐