Can you find it(HDU-5478)

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 equationa^{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.

Examples

Input

23 1 1 2

Output

Case #1:
1 22

题意:给出一个质数 c,三个正整数 k1,b1,k2,对于公式 a^{k1*n+b1} + b^{k2*n-k2+1} = 0 (mod \:\:C),若能找出最小的 a,b 则输出,若找不出则输出 -1

思路:公式题

对于公式 a^{k1*n+b1} + b^{k2*n-k2+1} = 0 (mod \:\:C)

当 n=1 时,a^{k1+b1} + b = 0 \:(mod \:\:C) ①

当 n=2 时,a^{2*k1+b1} + b^{k2+1} = 0 (mod \:\:C) ②

令 a^{k1} * ②,a^{2*k1+b1} + a^{k1}b= 0 (mod \:\:C) ③

令 ③-②,a^{k1}b+b^{k2+1}= 0 (mod \:\:C)

即:b(a^{k1}+b^{k2})= 0 (mod \:\:C)

由于 a、b < c,只需要枚举 a 的值(1~c-1),通过快速幂计算 a^{k1}a^{k1+b1},从而算出 b,再求出 b^{k2},比较 a^{k1}\:\:(mod\:\:C) 是否等于 b^{k2}\:\:(mod\:\:C) 即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define N 500001
#define LL long long
using namespace std;
LL Pow_Mod(LL a, LL b, LL m)
{
    LL res=1;
    while(b)
    {
        if(b&1)
            res=(res*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return res;
}

int main(){
    LL c,k1,b1,k2;
    int Case=1;
    while(scanf("%lld%lld%lld%lld",&c,&k1,&b1,&k2)!=EOF){
        bool flag=false;
        printf("Case #%d:\n",Case++);

        for(LL i=1;i<=c-1;i++){
            LL a=Pow_Mod(i,k1,c);
            LL b=c-Pow_Mod(i,k1+b1,c);
            LL temp=Pow_Mod(b,k2,c);
            if(a==temp){
                flag=true;
                printf("%lld %lld\n",i,b);
            }
        }
        if(!flag)
            printf("-1\n");

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值