hdu5478 Can you find it

来源:互联网 发布:spark java api 中文 编辑:程序博客网 时间:2024/05/01 16:38

Can you find it

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


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 

题意:求出最小的a和对应的b满足题目所给式子。
分析:刚看到这题时也想到了快速幂,但一看数据范围,感觉肯定会超时,想了很久,未果,参考大牛博客,就是快速幂。详解见这--某大牛博客

#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;const int MOD = 1000000007;#define ll long long#define CL(a) memset(a,0,sizeof(a))ll C,k1,b1,k2;ll a,b;ll mod_pow(ll x, ll y, ll mod){    ll re=1;    while (y)    {        if (y&1) re=re*x%C;        x=x*x%C;        y>>=1;    }    return re;}int main (){    int cas=1;    while (cin>>C>>k1>>b1>>k2)    {        cout<<"Case #"<<cas++<<":"<<endl;        bool flag=false;        for (a=1; a<C; a++)        {            b = C-mod_pow(a, k1+b1, C);            if ((mod_pow(a,k1,C)*(C-b)%C+mod_pow(b,k2,C)*b%C)%C==0)                cout<<a<<" "<<b<<endl,flag=true;        }        if (!flag)            cout<<"-1"<<endl;    }    return 0;}


0 0