【HDU】5478 公式变换

版权声明:抱最大的希望,为最大的努力,做最坏的打算。 https://blog.csdn.net/qq_37748451/article/details/86497612
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;

}

猜你喜欢

转载自blog.csdn.net/qq_37748451/article/details/86497612
今日推荐