hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two Sentences
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 852 Accepted Submission(s): 259
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 | We have carefully selected several similar problems for you: 3308 3309 3306 3310 3314
因为考试放下了挺久,后来发现不做题好空虚寂寞...于是决定在做一段时间再说。
//31MS 236K 1482 B G++
/* 又是不太懂的数学题,求ak,令ak%a0==0 欧拉函数+快速幂:
欧拉函数相信都知道了。
首先这题推出来的公式为:
ak=a0+y/(x-1)*(x^k-1); 明显a0是可以忽略的,其实就是要令
y/(x-1)*(x^k-1) % a0 == 0;
可令 m=a0/(gcd(y/(x-1),a0)),然后就求k使得
(x^k-1)%m==0 即可
即 x^k==1(mod m) 又欧拉定理有:
x^euler(m)==1(mod m) (x与m互质,不互质即无解) 由抽屉原理可知 x^k 的余数必在 euler(m) 的某个循环节循环。
故求出最小的因子k使得 x^k%m==1,即为答案 */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
__int64 e[],id;
int cmp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
__int64 euler(__int64 n)
{
__int64 m=(__int64)sqrt(n+0.5);
__int64 ret=;
for(__int64 i=;i<=m;i++){
if(n%i==){
ret*=i-;n/=i;
}
while(n%i==){
ret*=i;n/=i;
}
}
if(n>) ret*=n-;
return ret;
}
__int64 Gcd(__int64 a,__int64 b)
{
return b==?a:Gcd(b,a%b);
}
void find(__int64 n)
{
__int64 m=(__int64)sqrt(n+0.5);
id=;
for(__int64 i=;i<m;i++)
if(n%i==){
e[id++]=i;
e[id++]=n/i;
}
if(m*m==n) e[id++]=m;
}
__int64 Pow(__int64 a,__int64 b,__int64 mod)
{
__int64 t=;
while(b){
if(b&) t=(t*a)%mod;
a=(a*a)%mod;
b/=;
}
return t;
}
int main(void)
{
__int64 x,y,a;
while(scanf("%I64d%I64d%I64d",&x,&y,&a)!=EOF)
{
if(y==){
puts("");
continue;
}
__int64 m=a/(Gcd(y/(x-),a));
if(Gcd(m,x)!=){
puts("Impossible!");
continue;
}
__int64 p=euler(m);
find(p);
qsort(e,id,sizeof(e[]),cmp);
for(int i=;i<id;i++){
if(Pow(x,e[i],m)==){
printf("%I64d\n",e[i]);
break;
}
}
}
return ;
}
hdu 3307 Description has only two Sentences (欧拉函数+快速幂)的更多相关文章
- XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】
1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 45 Solved: 8[Submit][Status][W ...
- 牛客训练:小a与黄金街道(欧拉函数+快速幂)
题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...
- 数学知识-欧拉函数&快速幂
欧拉函数 定义 对于正整数n,欧拉函数是小于或等于n的正整数中与n互质的数的数目,记作φ(n). 算法思路 既然求解每个数的欧拉函数,都需要知道他的质因子,而不需要个数 因此,我们只需求出他的质因子, ...
- Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...
- 小a与黄金街道(欧拉函数+快速幂)
链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...
- HDU 3307 Description has only two Sentences
数学实在是差到不行了…… #include <cstdio> #include <cstring> #include <algorithm> #include &l ...
- (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)
题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 1286:找新朋友(欧拉函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...
- HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)
题意 \(n\) 局石头剪刀布,设每局的贡献为赢的次数与输的次数之 \(\gcd\) ,求期望贡献乘以 \(3^{2n}\) ,定义若 \(xy=0\) 则,\(\gcd(x,y)=x+y\) 思路 ...
随机推荐
- thinkphp3.2!Go for it!
http://document.thinkphp.cn/manual_3_2.html
- Android自学笔记:Git下载源代码
Info:做J2ME几年了,现在基本没有公司用了,是时候向Android领域进军了. 自学中,难免会有疏漏,有问题请及时提出,共同学习共同进步. 2014-10-13:初版 2014-10-14:添加 ...
- Visual C++2012中CMFCPropertySheet的用法
看到了一个例子(NewControls),该例子中使用了按钮图片等特效(哈哈,个人觉得挺高端),但是仔细看它的工程,没有xxxDlg.cpp就觉得奇诡了,难道不是基于对话框完成的?最终查阅居然是这样的 ...
- 2016-10-17: source insight插件
使用快捷键注释,单行注释,多行注释,#if 0注释 将文件 mycomment.em点此下载放到sourceinsight的Base工程的路径下(一般是在C:\Documents and Settin ...
- 关于JavaScript lastIndexOf() 方法 w3school.com.cn写的不一定全对
关于JavaScript lastIndexOf() 方法 w3school.com.cn的表述是 定义和用法 lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的 ...
- [转载]Python 资源大全
原文链接:Python 资源大全 环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex ...
- VS2013配置WTL91_5321_Final
网上关于WTL的文章,尤其是中文的文章不多,根据收集的资料整理出了VS2013安装WTL的方法. .下载.文件很小的,地址:http://sourceforge.net/projects/wtl/fi ...
- EUI 自动滚动的聊天文本
一 自动滚动的聊天文本 当文本输入改变时,将scrollV值等于maxScrollV值. private scrollLabel:eui.Label; egret.Tween.).call(()=&g ...
- XML.01-语法简介
body,td { font-family: calibri; font-size: 10pt } XML.01-语法简介 文档声明 元素(标签) 属性 注释 特殊字符 CDATA区域 处理指令 ...
- 获取B表数据添加到A表中作为一个下拉列表元素存在
1.ProductController类里toedit方法内添加: ProductModel product = ProductModel.dao.findById(id); //通过id查找服务类 ...