您的位置:首页 > 其它

HDU 5478 Can you find it

2015-09-27 11:36 387 查看

Can you find it

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


[align=left]Problem Description[/align]
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, ...).

[align=left]Input[/align]
There are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

23 1 1 2

[align=left]Sample Output[/align]

Case #1:
1 22

[align=left]Source[/align]
2015 ACM/ICPC Asia Regional Shanghai Online

[align=left]Recommend[/align]
hujie

首先想n为1,2,3……都要成立,所以先保证n=1成立,然后验证其他是否成立,把等式左边的b那一项移到右边,除一下,发现每次增量都是a^k1 和b^k2 所以只要n=2成立,后面的都成立。
下回这种题不要怕,还是可以做的。

#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1234567
#define M 12
int c, k1, b1, k2;
bool flag;
int quickpow(int m,int n,int k)// m^n % k
{
int  b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
int main()
{
int tt = 1; int b;
while(~scanf("%d %d %d %d", &c, &k1, &b1, &k2))
{

printf("Case #%d:\n", tt++);
flag = 0;
for(int a = 1; a < c; a++)
{
b = c - quickpow(a,k1+b1,c);
if( ( quickpow(a, k1*2+b1, c) + quickpow(b, k2*2-k2+1, c) ) %c == 0)
{
printf("%d %d\n",a,b);
flag = 1;
}
}
if(flag == 0)
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: