HDU 5478 Can you find it

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


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
 

 

Source
 

 

Recommend
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;
}

 

posted @ 2015-09-27 11:36  wmxl  阅读(162)  评论(0编辑  收藏  举报