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 ...
随机推荐
- 在Airtest中如何使用无线模式控制手机
在使用Airtest超快速开发App爬虫文章的最后,我们留了一个尾巴:如何启动Airtest的无线模式,不用USB线就能控制手机? 本文将会讲到具体的做法.做法分为两种:第一种是在Airtest的ID ...
- hadoop的环境变量
# hadoop && yarn export HADOOP_PREFIX=/home/ochadoop/apps/hadoop export HADOOP_HOME=${HADOOP ...
- app常见性能测试点
转载自 https://blog.csdn.net/xiaomaoxiao336368/article/details/83547318 1.响应 冷启动 首次启动APP的时间间隔 adb shell ...
- Flyway数据表迁移框架的使用
目录 1. 概述 2. Maven配置 3. SQL文件规范 4. 命令 5. 总结 1. 概述 Flyway是一个根据表结构快速生成数据表的工具,类似于Hibernate的自动生成表的特性. 官网: ...
- ActiveMq Windows 配置优化
最近ActiveMQ 5.15.3 会报 OutofMemory的错误 在 wrapper.conf中 #wrapper.java.additional.8=-Dorg.apache.activemq ...
- .net 语音,视频等格式转换
最近在做微信公众号开发的时候遇到一个问题,就是微信接收到语音消息的格式为amr,在网页上通常不能直接播放,需要先转为mp3,于是找到了一个办法,使用ffmpeg.exe,网上可以搜一下ffmpeg.e ...
- Excel VBA 连接各种数据库(一) VBA连接MySQL数据库
本文参考[东围居士]的cnblog博文 Excel.VBA与MySQL交互 在自己机器上调试成功,把调试中遇到的问题一并写出了. 本文主要涉及: VBA中的MySQL环境配置 VBA连接MySQL ...
- Python学习—数据库篇之pymysql
一.pymysql简介 对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...
- AVL树实现记录
https://github.com/xieqing/avl-tree An AVL Tree Implementation In C There are several choices when i ...
- checkbox、radio控件和文字不对齐
一般使用html控件的时候 单选按钮和复选框的控件和文字不对齐 给input控件加上 style="vertical-align: middle; margin-top: -2px; ...