HDU5478 Can you find it 數學快速冪

http://acm.hdu.edu.cn/showproblem.php?pid=5478

枚舉a,b=第一個式子中所得數,驗證即可。

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

 

#include<iostream>
#include<cstdio>
#include<cstring>
 
using namespace std;
 
typedef long long LL;
 
LL pow_mod(LL a,LL n,LL m)
{
    if(n==0)
        return 1;
    LL x=pow_mod(a,n/2,m);
    LL ans = x*x%m;
    if(n%2==1)ans=ans*a%m;
    return ans;
}
 
int main()
{
    //freopen("input.txt","r",stdin);
    int time=0;
    LL c, k1, k2, b1;
    while(scanf("%lld%lld%lld%lld",&c,&k1,&b1,&k2)!=EOF)
    {
        time++;
        printf("Case #%d:\n",time);
        bool flag=0;
        for(LL i=1;i<c;i++)
        {
            LL left=pow_mod(i,k1,c);
            LL b=c-pow_mod(i,k1+b1,c);
            LL right=pow_mod(b,k2,c);
            if(left==right)
            {
                flag=1;
                printf("%lld %lld\n",i,b);
            }
        }
        if(!flag)
            printf("-1\n");
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章