【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 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

思路:

由于(ak1*n+b1+bk2(n-1)+1)(modC)=0对于任意n为正整数恒成立,那么对于n=1成立可得(ak1+b1+b)(modC)=0;n =2时可得(a2*k1+b1+bk2+1)(modC)=0;

那么将n=1时所得的等式*ak1(modC)得(a2*k1+b1+b*ak1)(modC)=0;

那么和n=2时所得的式子比较可得(ak1)(modC)=(bk2)(modC);

那么由于1<=a,b<C;

那么从1循环到C枚举a,用快速幂求ak1,ak1+b1,用n=1时的等式求b,快速幂求bk2 ,判断是否(ak1)(modC)=(bk2)(modC);

下面证明;当a,b符合1,2式时,就(ak1*n+b1+bk2(n-1)+1)(modC)对于任意n为正整数恒成立。

1式可解得b=(C-(ak1+b1)modC);由1,2式得(ak1)(modC)=(bk2)(modC);

那么原式可改写为:(ak1*n+b1+ak1(n-1)*(C-(ak1+b1)modC))(modC)=0;

==(ak1*n+b1+ak1(n-1)*(C-(ak1+b1))(modC)=0

==(C*ak1(n-1))(modC)=0;

#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
const long long int MOD=1e6+3;
#define INF 0x3f3f3f3f
#define maxn 1010
using namespace std;
#define ll long long int
ll quick(ll x,ll y);
ll C;
using namespace std;
ll quick(ll x,ll y)//快速幂
{
    ll i,j,k;
    i=1;
    k=x;
    while(y)
    {
        if(y&1)
        {
            i=(i*k)%C;
        }
        k=(k*k)%C;
        y/=2;
    }
    return i;
}
int main()
{
    ll i,j,k,p,q;
    int e=0;
    while(scanf("%lld %lld %lld %lld",&C,&k,&p,&q)!=EOF)
    {   
        int flag=0;
        e++;
        printf("Case #%d:\n",e);
        for(i=1;i<C;i++)
        {
            ll kk=quick(i,k);//a^k1,
            ll k2=quick(i,k+p);//a^(k1+b1)
            ll b=(C-k2);//b
            ll z=quick(b,q);//b^k2;
            if(z==kk)
            {   
                flag=1;
                printf("%lld %lld\n",i,b);
            }
        }
        if(flag==0)
        {
            printf("-1\n");

        }
    }
    return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值