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

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

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

 1 //31MS    236K    1482 B    G++
 2 /*
 3
 4     又是不太懂的数学题,求ak,令ak%a0==0
 5
 6     欧拉函数+快速幂:
 7          欧拉函数相信都知道了。
 8          首先这题推出来的公式为:
 9              ak=a0+y/(x-1)*(x^k-1);
10
11          明显a0是可以忽略的,其实就是要令
12              y/(x-1)*(x^k-1) % a0 == 0;
13          可令 m=a0/(gcd(y/(x-1),a0)),然后就求k使得
14              (x^k-1)%m==0 即可
15              即 x^k==1(mod m)
16
17          又欧拉定理有:
18               x^euler(m)==1(mod m)  (x与m互质,不互质即无解)
19
20          由抽屉原理可知 x^k 的余数必在 euler(m) 的某个循环节循环。
21          故求出最小的因子k使得 x^k%m==1,即为答案
22
23 */
24 #include<stdio.h>
25 #include<stdlib.h>
26 #include<math.h>
27 __int64 e[1005],id;
28 int cmp(const void*a,const void*b)
29 {
30     return *(int*)a-*(int*)b;
31 }
32 __int64 euler(__int64 n)
33 {
34     __int64 m=(__int64)sqrt(n+0.5);
35     __int64 ret=1;
36     for(__int64 i=2;i<=m;i++){
37         if(n%i==0){
38             ret*=i-1;n/=i;
39         }
40         while(n%i==0){
41             ret*=i;n/=i;
42         }
43     }
44     if(n>1) ret*=n-1;
45     return ret;
46 }
47 __int64 Gcd(__int64 a,__int64 b)
48 {
49     return b==0?a:Gcd(b,a%b);
50 }
51 void find(__int64 n)
52 {
53     __int64 m=(__int64)sqrt(n+0.5);
54     id=0;
55     for(__int64 i=1;i<m;i++)
56         if(n%i==0){
57             e[id++]=i;
58             e[id++]=n/i;
59         }
60     if(m*m==n) e[id++]=m;
61 }
62 __int64 Pow(__int64 a,__int64 b,__int64 mod)
63 {
64     __int64 t=1;
65     while(b){
66         if(b&1) t=(t*a)%mod;
67         a=(a*a)%mod;
68         b/=2;
69     }
70     return t;
71 }
72 int main(void)
73 {
74     __int64 x,y,a;
75     while(scanf("%I64d%I64d%I64d",&x,&y,&a)!=EOF)
76     {
77         if(y==0){
78             puts("1");
79             continue;
80         }
81         __int64 m=a/(Gcd(y/(x-1),a));
82         if(Gcd(m,x)!=1){
83             puts("Impossible!");
84             continue;
85         }
86         __int64 p=euler(m);
87         find(p);
88         qsort(e,id,sizeof(e[0]),cmp);
89         for(int i=0;i<id;i++){
90             if(Pow(x,e[i],m)==1){
91                 printf("%I64d\n",e[i]);
92                 break;
93             }
94         }
95     }
96     return 0;
97 }

hdu 3307 Description has only two Sentences (欧拉函数+快速幂),布布扣,bubuko.com

时间: 2024-12-30 16:56:28

hdu 3307 Description has only two Sentences (欧拉函数+快速幂)的相关文章

XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][Web Board] Description 刘备(161年-223年6月10日),字玄德,东汉末年幽州涿郡涿县,西汉中山靖王刘胜的后代.刘备一生极具传奇色彩,早年颠沛流离.备尝艰辛最终却凭借自己的谋略终成一方霸主.那么在那个风云激荡的年代,刘备又是如何从一个卖草鞋的小人物一步一步成为蜀汉的开国皇帝呢

HDU3307 Description has only two Sentences 欧拉函数 欧拉定理

  Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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

Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers (if you do not, you might want to stop reading at this point). There are many ways of constructing really big numbers known to humankind, for instanc

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

欧拉函数&amp;&amp;快速乘方

1 //phi(a)=a*(a1-1)*(a2-1)*(a3-1)*...*(an-1)/(a1*a2*a3*...*an); 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 typedef __int64 LL; 8 LL phi(LL a){//phi 9 LL temp=a; 10 for(LL

(hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 125 Accepted Submission(s): 84   Problem Description Do you have spent some time to think and try to solve those unsolved problem afte

HDU 1286:找新朋友(欧拉函数)

http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define N 33000 6 int is_prime[N], prime[N], phi[N]; 7 8 void Euler() { 9 int n = 32

hdu 5152 A Strange Problem线段树+欧拉函数

*****************************************BC题解**********************************************************************1003 A Strange Problem 对于操作二,利用公式 当x >= Phi(C), A^x = A ^ (x%Phi(C) + Phi(C)) (mod C) 对于2333333这个模数来说,求18次欧拉函数后就变成了1,所以只需要保存19层第三次操作的加数

【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)

读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用欧拉函数降一下幂,因为两个数一定互质因此不用再加一个phi(m),于是放心的乘吧宝贝!! #include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> #include &