HDU - 5478(Can you find it)

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


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 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. (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

题意:给出c,k1,b1,k2,求满足题意给出的公式的所有(a,b)数对。

思路:

       因为要求出所有满足条件的(a,b)对,那么n=1和n=2是的情况都应该满足条件得出两个公式

a^(k1+b1)+b=0(mod c)   .......(1)

a^(2k1+b1)+b^(k2+1)=0(mod c)....(2)

将(1)式乘a^k1然后将两式合并整理得出a^(k1)=b^(k2),同时b=(c-a^(k1+b1))%c,那么就可以暴力1到c-1求出所有数对。

#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1)
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;

long long qpow(long long a,long long b,long long m)
{
    a=a%m;
    long long ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%m;
            b--;
        }
        b>>=1;
        a=a*a%m;
    }
    return ans;
}
int main()
{
    ll c,k1,b1,k2,a,b;
    int Case=0;
    while(scanf("%lld%lld%lld%lld",&c,&k1,&b1,&k2)!=EOF)
    {
        printf("Case #%d:\n",++Case);
        int flag=0;
        for(ll i=1;i<=c-1;i++)
        {
            a=i;
            b=c-qpow(a,k1+b1,c);
            if(qpow(a,k1,c)==qpow(b,k2,c))
            {
                flag=1;
                cout<<a<<" "<<b<<endl;
            }
        }
        if(!flag) cout<<"-1"<<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值