hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

时间:2021-07-11 23:15:50

Description has only two Sentences
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 852 Accepted Submission(s): 259

Problem Description
an = X*an-1 + Y and Y mod (X-1) = 0.
Your task is to calculate the smallest positive integer k that ak mod a0 = 0.

Input
Each line will contain only three integers X, Y, a0 ( 1 < X < 231, 0 <= Y < 263, 0 < a0 < 231).

Output
For each case, output the answer in one line, if there is no such k, output "Impossible!".

Sample Input
2 0 9

Sample Output
1

Author
WhereIsHeroFrom

Source
HDOJ Monthly Contest – 2010.02.06

Recommend
wxl | We have carefully selected several similar problems for you: 3308 3309 3306 3310 3314

因为考试放下了挺久,后来发现不做题好空虚寂寞...于是决定在做一段时间再说。

 //31MS    236K    1482 B    G++
/* 又是不太懂的数学题,求ak,令ak%a0==0 欧拉函数+快速幂:
欧拉函数相信都知道了。
首先这题推出来的公式为:
ak=a0+y/(x-1)*(x^k-1); 明显a0是可以忽略的,其实就是要令
y/(x-1)*(x^k-1) % a0 == 0;
可令 m=a0/(gcd(y/(x-1),a0)),然后就求k使得
(x^k-1)%m==0 即可
即 x^k==1(mod m) 又欧拉定理有:
x^euler(m)==1(mod m) (x与m互质,不互质即无解) 由抽屉原理可知 x^k 的余数必在 euler(m) 的某个循环节循环。
故求出最小的因子k使得 x^k%m==1,即为答案 */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
__int64 e[],id;
int cmp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
__int64 euler(__int64 n)
{
__int64 m=(__int64)sqrt(n+0.5);
__int64 ret=;
for(__int64 i=;i<=m;i++){
if(n%i==){
ret*=i-;n/=i;
}
while(n%i==){
ret*=i;n/=i;
}
}
if(n>) ret*=n-;
return ret;
}
__int64 Gcd(__int64 a,__int64 b)
{
return b==?a:Gcd(b,a%b);
}
void find(__int64 n)
{
__int64 m=(__int64)sqrt(n+0.5);
id=;
for(__int64 i=;i<m;i++)
if(n%i==){
e[id++]=i;
e[id++]=n/i;
}
if(m*m==n) e[id++]=m;
}
__int64 Pow(__int64 a,__int64 b,__int64 mod)
{
__int64 t=;
while(b){
if(b&) t=(t*a)%mod;
a=(a*a)%mod;
b/=;
}
return t;
}
int main(void)
{
__int64 x,y,a;
while(scanf("%I64d%I64d%I64d",&x,&y,&a)!=EOF)
{
if(y==){
puts("");
continue;
}
__int64 m=a/(Gcd(y/(x-),a));
if(Gcd(m,x)!=){
puts("Impossible!");
continue;
}
__int64 p=euler(m);
find(p);
qsort(e,id,sizeof(e[]),cmp);
for(int i=;i<id;i++){
if(Pow(x,e[i],m)==){
printf("%I64d\n",e[i]);
break;
}
}
}
return ;
}