HDU 5478 Can you find it 数学

Can you find it

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


 

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 ak1⋅n+b1 + bk2⋅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

 

 

Source

2015 ACM/ICPC Asia Regional Shanghai Online

 

 

Recommend

hujie

 

当n==1时,

a^{k1+b1}+b=0(mod C)     (1)

 

当n==2时候

a^{k1*2+b1}+b^{k2+1}=0(mod C)        (2)

 

(1)*a^{k1}==a^{2*k1+b1}+a^{k1}*b= 0 ( mod C)      (3)

 

   (3)-(2)=b*(a^{k1}+b^{k2})=0 (mod C)

b是可以消去的

 

因为a,b小于C,我们只需遍历一遍a的取值(1~C-1),快速幂计算出,(1)算出b,快速幂求出,比较(mod C)是否等于(mod C)即可

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
 
#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define ll long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
ll quick_qow(ll a,ll b,ll p)///计算a^b %p
{
	ll ans=1%p;///ans不能为1,b为0,p为1的情况不成立
	for(;b;b>>=1)
	{
		if(b&1) 
			ans=(ans*a)%p ;
		a=a*a%p ;
	}
	return ans;
}
int main()
{
    ll C,k1,b1,k2,i,k=1;
    bool flag;
    while(~scanf("%lld%lld%lld%lld",&C,&k1,&b1,&k2))
    {
        flag=0;
        printf("Case #%lld:\n",k++);
        for(i=1;i<C;i++)
        {
            ll a=quick_qow(i,k1,C);
         
            ll b=C-quick_qow(i,k1+b1,C);
            ll t=quick_qow(b,k2,C);
           
            if(a==t)
			{
                	flag=1;
                	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、付费专栏及课程。

余额充值