杭电5478Can you find it

来源:互联网 发布:如何评价魔兽世界 知乎 编辑:程序博客网 时间:2024/06/04 19:25

Can you find it

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


Problem Description
Given a prime number C(1C2×105), and three integers k1, b1, k2 (1k1,k2,b1109). Please find all pairs (a, b) which satisfied the equation ak1n+b1 + bk2nk2+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. (1a,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
 
快速幂算法(计算a^bmod c)
int powermod(int a,int b,int c)
{
int ans=1;
a=a%c;
while(b)
{
if(b%2)
ans=ans*a%c;
b/=2;
a=(a*a)%c;
}
return ans;
 } 
 当n=1时 a^(k1+b1)+b=0(mod c);.....1
 当n=2时 a^(2*k1+b1)+b^(k2+1)=0(mod c);...2
 1*a^k1  a^(2*k1+b1)+b*a^k1=0(mod c)........3
由2 3得a^k1=b^k2(mod c)
 
 




















#include<stdio.h>
#define LL long long
LL powermod(LL a,LL b,LL c)
{
LL ans=1;
a=a%c;
while(b)
{
if(b%2)
ans=ans*a%c;
a=a*a%c;
b/=2;
}
return ans;
}
int main()
{
int k=1;
int i,j;
LL c;
LL k1,k2,b1;
while(~scanf("%lld%lld%lld%lld",&c,&k1,&b1,&k2))
{
int ans=0;
printf("Case #%d:\n",k++);
for(i=1;i<c;i++)
{
j=c-powermod(i,k1+b1,c);
if(powermod(i,k1,c)==powermod(j,k2,c))
{
printf("%d %d\n",i,j);
ans=1;
}
}
if(ans==0)
printf("-1\n");
}
return 0;
}
 

0 0
原创粉丝点击