Description has only two Sentences

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 124 Accepted Submission(s): 55
 
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
 
/*
题意:如题给出的递推公式,让你求出最小的k满足ak mod a0 ==0; 如果没有的话输出impossible 初步思路:an=an-1*X+Y => an=Xn*a0+(1+X1+X2+.....+Xn-1)*Y (里面有一个等比数列)
=> 然后两边同时膜a0 得到 an mod a0 = ( (Xn -1) * Y ) mod a0 / (X -1) = 0
=> 令 T=Y/(X-1) 得到0 =T (Xn - 1) mod a0 (T是任意整数 )
=> 将 mod a0 移到左边
=> 0 (mod a0) = T (Xn - 1)
(这里的mod是提出来的)
=> 令p=__gcd(a0,T) 然后得到
=> 0 (mod a0/p) = T/p (Xn - 1) = 0 (mod a0') =T' (Xn - 1)
=> 此时a0' 和T' 互质了 那么得到
=> Xn-1=0 (mod a0') 如果(Xn -1 )mod a0' !=0那么就无解 即:
=> Xn mod a0' ==1 否则就是无解的情况
然后就没有思路了.......
#改进:由上一步能得出来 X^n=1(mod a0')
=> 欧拉定理,X^euler(a0')=1(mod a0');//其中X和a0'必须是互质的,不然没有解
=> 如果是互质的,那么然后就可以从a0'中的质因子枚举,然后快速幂就可以了
#感悟:!!!质因子忘记排序了,错了两罚!!!!想吐,一天了,就想了这一个题。。。。
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll X,Y,A;
ll T; /*******************分解质因子模板*********************/
bool comp(ll a,ll b){
return a<b;
}
vector<ll>v;
void find(ll n)//分解质因子
{
v.clear();
ll m=(ll)sqrt(n+0.5);
for(ll i=;i<m;i++)
if(n%i==){
v.push_back(i);
v.push_back(n/i);
}
if(m*m==n) v.push_back(m);
sort(v.begin(),v.end(),comp);
}
/*******************分解质因子模板*********************/ /************快速幂模板****************/
ll power(ll n,ll x,ll mod){
if(x==) return ;
ll t=power(n,x/,mod);
t=t*t%mod;
if(x%==) t=t*n%mod;
return t;
}
/************快速幂模板****************/ /**************************欧拉函数模板*****************************/
//直接求解欧拉函数
ll euler(ll n){ //返回euler(n)
ll res=n,a=n;
for(int i=;i*i<=a;i++){
if(a%i==){
res=res/i*(i-);//先进行除法是为了防止中间数据的溢出
while(a%i==) a/=i;
}
}
if(a>) res=res/a*(a-);
return res;
}
/**************************欧拉函数模板*****************************/ int main(){
//freopen("in.txt","r",stdin);
while(scanf("%lld%lld%lld",&X,&Y,&A)!=EOF){
if(Y==){
puts("");
continue;
}
T=Y/(X-);
ll p=__gcd(T,A);//最大公因子
//化简到最简单
T/=p;
A/=p;//a0'
//cout<<T<<" "<<A<<endl;
if(__gcd(X,A)!=){//如果这两个数不是互质的,由欧拉定理的肯定是无解的
printf("Impossible!\n");
}else{
//X^euler(a0')=1(mod a0')
ll cur=euler(A);
find(cur);//分解质因子,打到p中
//cout<<v.size()<<endl;
for(int i=;i<v.size();i++){
//cout<<power(X,v[i],A)<<endl;
if(power(X,v[i],A)==){
printf("%lld\n",v[i]);
break;
}
}
}
}
return ;
}

Description has only two Sentences(欧拉定理 +快速幂+分解质因数)的更多相关文章

  1. BZOJ 3884: 上帝与集合的正确用法 扩展欧拉定理 + 快速幂

    Code: #include<bits/stdc++.h> #define maxn 10000004 #define ll long long using namespace std; ...

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

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  3. 2^x mod n = 1(欧拉定理,欧拉函数,快速幂乘)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. Super A^B mod C (快速幂+欧拉函数+欧拉定理)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...

  6. hdu 2462(欧拉定理+高精度快速幂模)

    The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...

  8. [SDOI2010] 古代猪文 (快速幂+中国剩余定理+欧拉定理+卢卡斯定理) 解题报告

    题目链接:https://www.luogu.org/problemnew/show/P2480 题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色 ...

  9. URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)

    题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...

随机推荐

  1. iOS开发中KVC、KVO简介

    在iOS开发中,KVC和KVO是经常被用到的.可以使用KVC对对象的属性赋值和取得对象的属性值,可以使用KVO监听对象属性值的变化.简单介绍一下KVC和KVO. 一:键值编码(KVC) KVC,全称 ...

  2. go-fasthttp源码分析

    1.架构 listener->server->workerpool 1.1.workerpool中有两种缓存: a.wp.ready,缓存未退出worker, b.worker退出后用sy ...

  3. JS -- The Scope Chain 作用域链

    The Scope Chain JavaScript is a lexically scoped language: the scope of a variable can be thought of ...

  4. 一.把传统服务做成dubbo分布式服务架构的步骤

    1.把传统服务按照一定原则(根据项目的业务逻辑和场景)拆分成多个服务(主要服务是服务提供者和服务消费者,服务提供者或服务消费者的公共部分也可以拆分成其他服务,如公共DAO.公共工具类.公共实体,公共w ...

  5. 【原创】流程引擎的网关(遵循BPMN2.0)设计总结

    概述 BPMN 2.0是什么呢?业务流程模型注解(Business Process Modeling Notation - BPMN)是 业务流程模型的一种标准图形注解.这个标准 是由对象管理组(Ob ...

  6. Codeforces Round #420 (Div. 2)

    /*************************************************************************************************** ...

  7. web应用程序 前段部分调优

    1. 使用瀑布图初步诊断网站性能瓶颈 一般来说,打开一个网页的速度会受到以下几项的影响: 1) 服务器花了太长的时间将.aspx页面的内容转化为html. 2) .aspx页面花了太长的时间从服务器端 ...

  8. 跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题

    问题 生成缩略图 生成验证码 生成二维码 给图片加水印 外部引用 Node  不解释  https://nodejs.org/en/download/ sharp 高性能缩略图  https://gi ...

  9. 第四章 MySQL高级查询(二)

    第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ...

  10. Mybatis,Spring,SpringMVC框架面试题

    Mybatis测试 1,   Mybatis的核心是(  sqlsessionfactory    ) 2,   使用Mybatis持久化框架进行数据查询需要返回的一个实体类的集合, 在<sel ...