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

227 篇文章 0 订阅
97 篇文章 0 订阅

传送门

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(1C2×105) , and three integers k1, b1, k2 (1k1,k2,b1109) . Please find all pairs (a, b) which satisfied the equation ak1n+b1 + bk2nk2+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.
(1a,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 成立的 (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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值