杭电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(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.
Please output all pairs (a, b) in lexicographical order.
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)
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;
}
#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
- 杭电5478Can you find it
- 杭电2141-Can you find it?
- 杭电2141Can you find it?
- 杭电2141Can you find it?
- 【杭电】[2141]Can you find it?
- 【杭电2141】Can you find it?
- 【杭电2141】Can you find it?
- 杭电 HDU 2141 Can you find it
- HDU--杭电--2141--Can you find it?--二分
- 杭电 2141 Can you find it?二分法+暴力查找
- can you find it?
- Can you find it?
- Can you find it?
- Can you find it?
- can you find it?
- Can you find it?
- can you find it?
- Can you find it?
- mssql中的数据转存到Mysql中
- HDU 5733 tetrahedron (2016 Multi-University Training Contest 1 计算几何)
- mysql的索引
- 类设计原则
- kdtree c++版本
- 杭电5478Can you find it
- mac在安装genymotion下遇到的种种问题
- 关于如何求无向无环图中所有两点之间的长度和
- K-means算法上
- 《机器学习中的数学》第一课笔记1.1
- Linux ldconfig 查看动态库连接
- 《ShareX 》截图神器
- 剑指offer——把二叉树打印成多行
- Mysql 设置数据按条件查询下的序号