hdu3307 欧拉函数
题目链接: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
0.
Your task is to calculate the smallest positive integer k that
ak mod a0 = 0.
a0 ( 1 < X < 231, 0 <= Y < 263, 0
< a0 < 231).
is no such k, output "Impossible!".
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 ;
}
hdu3307 欧拉函数的更多相关文章
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- COGS2531. [HZOI 2016]函数的美 打表+欧拉函数
题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- 51Nod-1136 欧拉函数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...
- 欧拉函数 - HDU1286
欧拉函数的作用: 有[1,2.....n]这样一个集合,f(n)=这个集合中与n互质的元素的个数.欧拉函数描述了一些列与这个f(n)有关的一些性质,如下: 1.令p为一个素数,n = p ^ k,则 ...
- FZU 1759 欧拉函数 降幂公式
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- MySQL(基础技能)
一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...
- for循环,数字类型,字符串类型
for 循环: l=['a','b','c'] for i in l : print(i) while循环和for循环 while循环:条件循环,循环的次数取决于条件何时为False for循环:循环 ...
- Matplotlib-动画
Animation 动画 定义方程 参数设置 # Animation 动画 # 定义方程 # 使用matplotlib做动画也是可以的,我们使用其中一种方式,function animation来说说 ...
- Redis广播
参见:http://blog.csdn.net/u011734144/article/details/51782085
- .net core 2.0 HTTPS request fails using HttpClient 安全错误
最近.net core 项目中遇到一个问题,通过Httpclient 访问https的接口报错,错误如下: WinHttpException: A security error occurred Sy ...
- 最大化系统并发连接数.Windows.reg
最大化系统并发连接数.Windows.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentContro ...
- ArcGIS 要素类平移工具-arcgis案例实习教程
ArcGIS 要素类平移工具-arcgis案例实习教程 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:对整个要素类,按指定偏移距离,进行整体平移. 优点:使用 ...
- spring boot 2 统一异常处理
spring mvc 针对controller层异常统一处理非常简单,使用 @RestControllerAdvice 或 @RestControllerAdvice 注解就可以轻@RestContr ...
- 微信小程序---转发分享功能
1. 转发---onShareAppMessage 2.不带参数 //用户点击右上角分享 onShareAppMessage: function (res) { return { title: 'xx ...
- css入门第一天
一丶Web标准Web标准不是某一个标准,而是一系列标准的集合,内容与结构与表现形式的分离网页主要有四个部分组成:内容(content),结构(structure), 表现(presentation)和 ...