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. (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
1 22
Source
没做出的主要原因在于没有想到化简式子的方法,快速幂还是很容易就想到的,但是以前并没有用过这种方法,主要是题意没有理解好,把n看的太重要,其实题意就是告诉你n=1,2...的时候肯定成立,并不是选其中一个n成立!!!!那么就可以只取1,2来进行计算。
n=1时,ak1+b1+ b = 0 (mod C)------------------①
n=2时,a2*k1+b1+ bk2+1 = 0 (mod C)----------②
①*ak1 ,等式仍成立,a2*k1+b1+ak1 *b = 0 (mod C)-------------③
由方程②=③,可推出 :ak1 (mod C) = bk2 (mod C)---------------*
遍历a:1~c-1,利用快速幂从①计算出b,再利用快速幂计算*式等号两边,比较是否相等。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10 using namespace std; __int64 Quick_Mod(int a, int b, int m)
{
__int64 res = ,term = a % m;
while(b)
{
if(b & ) res = (res * term) % m;
term = (term * term) % m;
b >>= ;
}
return res%m;
} int main()
{
int c,k1,b1,k2,t;
int f;
t=;
while(cin>>c>>k1>>b1>>k2)
{
cout<<"Case #"<<t++<<":"<<endl;
f=;
int a,b,x,y;
for(a=;a<c;++a)
{
x=Quick_Mod(a,k1,c);
b=c-Quick_Mod(a,k1+b1,c);
y=Quick_Mod(b,k2,c);
if(x==y)
{
f=;
cout<<a<<" "<<b<<endl;
}
}
if(f==)
{
cout<<-<<endl;
} } return ;
}