题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3307

Description has only two Sentences

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

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 = X*an-1 + Y ,给你X,Y,a0,叫你求出最小的整数k(k>0)使得 ak% a0=0。根据公式我们可以求出an= a0*Xn-1+(x0+x1+x2+……+xn-1)Y。由等比数列前项和公式可得

an=a0*Xn-1+(xn-1)*Y/(x-1)。

所以只需令(xk-1)*Y/(x-1)%a0=0,求出k的值。

令Y'=Y/(x-1),d=gcd(Y/(x-1),a0)

Y'/=d,a0/=d;

此时Y'与a0互质

(xk-1)*Y/(x-1)%a0=0

等价于

(xk-1)%a0=0

xk%a0=1

而这就符合欧拉定理aφ(n)Ξ 1(mod n)

xφ(a0)Ξ 1(mod a0)

如果gcd(x,a0)!=1则k无解

否则k为phi(a0)除以满足条件的phi(a0)的因子

#include<iostream>
using namespace std;
#define ll long long
ll zys[][],cnt;
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
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;
}
void fj(ll ans)
{
for(ll i=;i*i<=ans;i++)
{
if(ans%i==)
{
zys[cnt][]=i;
zys[cnt][]=;
while(ans%i==)
{
ans/=i;
zys[cnt][]++;
}
cnt++;
}
}
if(ans>)
{
zys[cnt][]=ans;
zys[cnt++][]=;
}
}
ll poww(ll a,ll b,ll mod)
{
ll ans=;
while(b)
{
if(b&)ans=ans*a%mod;
a=a*a%mod;
b>>=;
}
return ans;
}
int main()
{
ll x,y,a,c,d;
while(cin>>x>>y>>a)
{
if(x==)
{
if(gcd(y,a)==)cout<<a<<endl;
else cout<<a/gcd(y,a);
continue;
}
c=y/(x-);
if(c%a==)
{
cout<<<<endl;
continue;
}
d=gcd(c,a);
if(gcd(x,a/d)!=)cout<<"Impossible!"<<endl;
else
{
a/=d;
ll ans=phi(a);
cnt=;
fj(ans);
for(int i=;i<cnt;i++)
{
for(int j=;j<=zys[i][];j++)
{
if(poww(x,ans/zys[i][],a)==)ans/=zys[i][];
}
}
cout<<ans<<endl;
}
}
return ;
}

最新文章

  1. SPOJ DISUBSTR ——后缀数组
  2. listview+seekbar问题的解决
  3. Odoo Many2many 指定默认分组过滤
  4. AngularJS快速入门指南11:事件
  5. socket对于大数据的发送和接收
  6. ios开发之 MPMoviePlayerController 视频播放器
  7. delta simulation time[(delta cycle), (delta delay)]
  8. WPF动画之线性插值动画(1)
  9. Fedora 21 安装VirtualBox
  10. Vue深度学习(4)-方法与事件处理器
  11. 全局唯一ID发号器的几个思路
  12. dva/docs/GettingStarted.md
  13. c提高第六次课 文件读取
  14. go_micro相关书签
  15. python基础之作业2----购物车小练习
  16. jqgrid修改表格内容为居中
  17. [转]Redis配置文件详解
  18. django 通过邮箱和用户名都能登录
  19. C#概念总结(二)
  20. 【Spark-core学习之七】 Spark广播变量、累加器

热门文章

  1. lvs+keepalived+ipvsadm 完整搭建笔记
  2. blade 学习
  3. QTP测试.NET程序的时候,ComboBox下拉框控件选择后,运行时对象不可见解决方案
  4. js 向上、向下取整
  5. 如何高效的学习Java开发
  6. 推荐几个IDEA插件,Java开发者撸码利器(转载)
  7. keil5一点project就闪退
  8. github 绑定域名
  9. Linux ansible 之 playbook
  10. linux下mycat自启动方法