HDU 5478 Can you find it

Can you find it

Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1359 Accepted Submission(s): 565

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.

Sample Input
23 1 1 2

Sample Output
Case #1:
1 22


好难….靠自己完全想不到
a^(k1*n+b1)+b^(k2*(n-1)+1)==0 (mod c)
对n=1,2都成立
n=1时
a^(k1+b1)+b==0 (mod c)
a^(k1+b1)%cb<c
所以有
a^(k1+b1)%c+b==c
b==c-a^(k1+b1)%c //①
a^(k1+b1)%c==c-b //②
n=2时 a^(2*k1+b1) + b^(k2+1)==0 (mod c)
=> a^(k1+b1)*a^k1 + b^k2*b ==0 (mod c)
代入②
=> (c-b)*(a^k1) + b^k2 * b ==0 (mod c)
移项
=> a^k1==b^k2 (mod c) //③
所以枚举a 通过①求出b
通过③验证(a,b)能否成立

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<cmath>
#include<list>
#include<cstring>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

#define sci(a) scanf("%d",&a)
#define scll(a) scanf("%lld",&a)
#define scd(a) scanf("%lf",&a)
#define scs(a) scanf("%s",a)
#define pri(a) printf("%d\n",a);
#define prd4(a) printf("%.4lf\n",a);
#define prd2(a) printf("%.2lf\n",a);
#define prs(a) printf("%s\n",a);

int c,k1,b1,k2;

int POW(int x,int n){
    ll res=1;
    while(n>0){
        if(n&1)
            res=(res*x)%c;
        x=((ll)x*x)%c;
        n>>=1;
    }
    return res;
}

int main(){
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    bool op=false;
    for(int t=1;~scanf("%d%d%d%d",&c,&k1,&b1,&k2);++t){
        op=0;
        printf("Case #%d:\n",t);
        for(int i=1;i<c;++i){
            int j=c-POW(i,k1+b1);
            if(j>0)
                if(POW(i,k1)==POW(j,k2)){
                    printf("%d %d\n",i,j);
                    op=1;
                }
        }
        if(!op)
            pri(-1);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值