HDU 5478 Can you find it

来源:互联网 发布:mp4 元数据 放头部 编辑:程序博客网 时间:2024/05/16 12:48



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
 

Source
2015 ACM/ICPC Asia Regional Shanghai Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5493 5492 5491 5490 5489 
 


因为式子对所有的n都满足,我们把n=1和n=2代入.可以推出:a^k1=b^k2。b=c-a^(k1+b1) 。

我们从1到c-1枚举a。再求出b,然后判断a^k1是否等于b^k2即可。


#include<iostream>#include <algorithm>#include <cmath>#include <cstdio>using namespace std;typedef long long ll;ll fast(ll a,ll b,ll mod){    ll res=1;    while(b)    {        if(b&1)            res=(res*a)%mod;        a=(a*a)%mod;        b/=2;    }    return res%mod;}int main(){    ll c,k1,b1,k2;    int Case=0;    while(cin>>c>>k1>>b1>>k2)    {        cout<<"Case #"<<++Case<<":"<<endl;        ll i;        int flag=0;        for(i=1; i<c; i++)        {            ll ans=fast(i,k1,c);            ll ans1=c-fast(i,k1+b1,c);            ll ans2=fast(ans1,k2,c);            if(ans==ans2)            {                flag=1;                cout<<i<<" "<<ans1<<endl;            }        }        if(!flag)            puts("-1");    }    return 0;}

0 0