Description has only two Sentences

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1071    Accepted Submission(s): 323

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
 
很好的一个题目。
思路:对于此数列我们可以得到其通项为:
an = a0*xn + (1+x+x2+..xn-1)Y = (xn-1)/(x-1)*Y+a0xn
假设ak%a0 = 0,那么我们代入得 ((xk-1)/(x-1)*Y+a0xk)%a0 =0 又因为题目中说过Y%(x-1)=0 所以设 Y=Y/(x-1)。
最终我们得到 ak%a0 = (xn-1)*Y%a0 = 0 接下来就是这个题目的难点了 ,这个地方我至今无法想通,如果有大牛的话请指教一二。
我们得到 d = gcd(Y,a0),那么这个式子就变成了 (xn-1)*(Y/d)%(a0/d) = 0 ,又因为 Y/d 与 a0/d 互质,取模不可能为0,设 a = a0/d 所以最终我们得到:
(xn-1)%a = 0 ----------> xn%a=1 这里就可以根据欧拉定理求解了。
  欧拉定理:若n,a为正整数,且n,a互素,则: aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAAAkCAIAAADKJHVZAAADv0lEQVRoge2ZzWHcIBCFOXDkTAFUQAmUMDVMDVRBEZRBFdQxbZDDSwhBQvbuyrGy0Tt5JSRgvvkTVu3WBaS+ewG3WrsxXEQ3hkvoxnAJ3RguoRvDJXRjuIRuDJfQjeES+hIMIvLQ9VvnY4gxPn33v9XJGESEiA4GxBhzzudO+gY6GQMRHVtZRJxz5076BnoSQ4wxpSQiKaUYo7W2lNJaM8bUWjEmpWStba3lnK21/brW+oyVf6w+4xcp55xSOmUxz2AgIqT4EAKsz8whhNaaUj9fKCI5Z+dcSqmUYq3t9XlE9aJKKd773VvM/KUdQYxRaw0/++T4g10/jEFEYGsRMcbgIjMDzOjpYxD0ka015xzgvaJaKxE55zr4UaWU4xJ1ikIIn8fQWjvIxg9jKKXA1jnn7onOOZh7NAoRwRaTz44J6kXlnHcxnEL6QxHRoxhWhfPJaBARZmbm1hrKA+5qrXsq6LbAyLE2nJUudjGIyN8pP49iiDEidW/1MQbkfWutMQZmRcg755g5pTQS9t53N+xLDCGMKWJMUKPqWqu17WKIMY7hX0oJIWita61IYs45ESmlGGOUUtvqwsxE5L3f+m+tNYTgnDPGGGO2GJi5z87MmLevZOUfH2AIIaAZqLVOG+6JaFTO+fgDrZSCGNoK297V6m27GEIIo9OJSAhBKRVCqLXCFtZaIqq14g3jkrz33WlSSkqpTgLVbrTAhIGZsQtkCzzerdQTyXYjRxhKKd0EKaXJHCuwx7Xx3Mq5i8E5N80Cc/Sf3vsxIo0xPSBKKZOlkAbwt9Z6DI7dpITQ70VxspJSardoHWHw3ve+eLu33drYfuWW3VtIBavp4Fy7Wj2yi8FaOwXchIGIps6tY2Dm6YVEhCtbQrsYxgVP6bG1NsbWH9dXO8QzmDXGOD1/bNCnJQutxq+i4WkM3ehdHcyUYdoCg1Kq+673flrJw9HQ+41SCjBc8Hz0M7WhPYJhKgZ4Gzwa1XGqIhMGREz/qbWejD6B/H19tUO8Ec0odovv4dX414UsvKvVI7Da5B9jrwLBjfpPIhpT9tQFjFkFvtgNh0YLpzj4ip5S/zg1CvJ4lDlBGnWUlIwxgI8O4S98ED0kZrbWouMc27Na6/ZjHsO6+ZRSKHUwtNZ6dHM06ESE5mqcFL2v1hql2BgzRl4Ioa8EDfEYWDHG1dHLe/737cRjqxM1tjyT3hNDSmn1dfKNOmj53hNDa211bPBdmo4bJr0thi9qqZ/W8X/D3hbDv6UbwyV0Y7iEbgyX0I3hEvoB8Bf+XaskXEoAAAAASUVORK5CYII=" alt="" />
这里求出的欧拉函数不一定是最小的解,所以要到它的因子里面去找。
但是我还是想不通为什么要求出最大公约数之后再进行求解?
我在想是不是形如(xn-1)*y%a=0的式子可以都可以变成 (xn-1)%(a/gcd(y,a))=0进行求解???BUT,WHY??
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef long long LL;
LL e[][];
LL phi(LL x)
{
LL ans=x;
for(LL i=; i*i<=x; i++)
if(x%i==)
{
ans=ans/i*(i-);
while(x%i==) x/=i;
}
if(x>)
ans=ans/x*(x-);
return ans;
}
LL gcd(LL a,LL b)
{
return b==?a:gcd(b,a%b);
}
LL pow_mod(LL a,LL n,LL mod)
{
LL ans = ;
while(n)
{
if(n&) ans=ans*a%mod;
a=a*a%mod;
n>>=;
}
return ans;
}
void devide(LL ans,int &id)
{
for(LL i=; i*i<=ans; i++) ///分解质因数
{
if(ans%i==)
{
e[id][]=i;
e[id][]=;
while(ans%i==) ans/=i,e[id][]++;
id++;
}
}
if(ans>)
{
e[id][]=ans;
e[id++][]=;
}
} int main()
{
LL X,Y,a0;
while(~scanf("%lld%lld%lld",&X,&Y,&a0))
{
Y = Y/(X-);
LL d = gcd(Y,a0);
a0 = a0/d;
if(gcd(X,a0)!=)
{
printf("Impossible!\n");
}
else
{
LL ans = phi(a0);
int id = ;
devide(ans,id);
for(int i=; i<id; i++)
{
for(int j=; j<e[i][]; j++)
{
if(pow_mod(X,ans/e[i][],a0)==) ans/=e[i][]; ///分解本身,得到 X^ans % a0 = 1的最小ans
}
}
printf("%lld\n",ans);
}
}
return ;
}
 

hdu 3307(欧拉函数+好题)的更多相关文章

  1. 找新朋友 HDU - 1286 欧拉函数模板题

    题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...

  2. hdu 1286 找新朋友 欧拉函数模版题

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  3. (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

    题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

  4. UVA 10820 欧拉函数模板题

    这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...

  5. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  6. POJ 2407 Relatives(欧拉函数入门题)

    Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...

  7. hdu 2824(欧拉函数)

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. poj2407(欧拉函数模板题)

    题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...

  9. HDU 6322.Problem D. Euler Function -欧拉函数水题(假的数论题 ̄▽ ̄) (2018 Multi-University Training Contest 3 1004)

    6322.Problem D. Euler Function 题意就是找欧拉函数为合数的第n个数是什么. 欧拉函数从1到50打个表,发现规律,然后勇敢的水一下就过了. 官方题解: 代码: //1004 ...

随机推荐

  1. How to setup multimedia on CentOS 7

    You will need to also install the EPEL repository as nux-dextop depends on this for some of its pack ...

  2. TypeError: cannot use a string pattern on a bytes-like object

    一劳永逸解决:TypeError: cannot use a string pattern on a bytes-like object TypeError: cannot use a string ...

  3. An entity cannot be annotated with both @Entity and @MappedSuperclass: com.example1.demo1.Entity.User错误

    项目问SpringDataJpa项目,在运行的过程中出现的以下错误: Caused by: org.hibernate.AnnotationException: An entity cannot be ...

  4. debug环境下打印

    #ifdef DEBUG #    define NSLog(...) NSLog(__VA_ARGS__) #else #    define NSLog(...) {} #endif

  5. iOS开发-NSLog不打印设置 Prefix

    首先在-Prefix.pch,文件里添加如下代码 #ifdef DEBUG #define NSLog(...) NSLog(__VA_ARGS__) #define debugMethod() NS ...

  6. Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies

    Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. API 调用退出异常. (Except ...

  7. ubuntu linux下各种格式软件包的安装卸载

    http://www.cnblogs.com/mo-beifeng/archive/2011/08/14/2137954.html

  8. day04_07 while循环01

    while循环结构: #while 条件: print("any") print("any") 死循环案例 num = 1 while num<=10 : ...

  9. LR采用的Sigmoid函数与最大熵(ME) 的关系

    LR采用的Sigmoid函数与最大熵(ME) 的关系 从ME到LR 先直接给出最大熵模型的一般形式,后面再给出具体的推导过程. \[\begin{align*} P_w(y|x) &= \df ...

  10. 聊聊、Nginx GDB与MAIN参数

    接着上一篇,我们学习 Nginx 的 main 方法.用 gdb 工具调试 Nginx,首先 gdb nginx.如下: gdb 调试工具有很多的命令,上一篇为了找 main 方法用了 b 命令,也就 ...