HDU5478 Can you find it 数学快速幂

Given a prime number C(1≤C≤2×105)C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109)(1≤k1,k2,b1≤109). Please find all pairs (a, b) which satisfied the equation ak1⋅n+b1ak1⋅n+b1 + bk2⋅n−k2+1bk2⋅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)(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

题意:

       

分析:

代码:

#include<cstdio>
#include<utility>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int c,k1,b1,k2,time,flag;
int qic_mod(int a, int b, int p) { /// calculate (a ^ b) mod p
	int ans = 1 % p;
	for (; b; b >>= 1)
    {
		if (b & 1) ans = (long long)ans * a % p;
		a = (long long)a * a % p;
	}
	return ans;
}
int main()
{
    while(scanf("%d",&c)!=EOF)
    {
        scanf("%d%d%d",&k1,&b1,&k2);
        printf("Case #%d:\n",++time);
        flag=0;
        for(int a=1;a<c;a++)
        {
            int right=qic_mod(a,k1,c);
            int b=c-qic_mod(a,k1+b1,c);
            int left=qic_mod(b,k2,c);
            if(right==left)
            {
                printf("%d% d\n",a,b);
                flag=1;
            }
        }
         if(!flag) printf("-1\n");
    }
       return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会敲代码的小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值