码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5478 Can you find it

时间:2015-10-11 11:38:37      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:数论   快速幂   

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×10技术分享5技术分享)技术分享, and three integers k1, b1, k2 (1k1,k2,b110技术分享9技术分享)技术分享. Please find all pairs (a, b) which satisfied the equation a技术分享k1?n+b1技术分享技术分享 + b技术分享k2?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. (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

题意:求出最小的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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu5478 Can you find it

标签:数论   快速幂   

原文地址:http://blog.csdn.net/d_x_d/article/details/49029637

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!