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\) 思路 ...
随机推荐
- 根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容
1.action String templete=ConstantsAppParams.CONTRACT_TEMPLET_DOC;//contract_templet.tldString temple ...
- PHP中对淘宝URL中ID提取
<?php $taobao = 'taobao.com'; $tmall = 'tmall.com'; $guojitmall = 'tmall.hk'; $juhuasuan = 'ju.ta ...
- JSP+Servlet中使用jspsmartupload.jar进行图片上传下载
JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...
- 来自 Thoughtram 的 Angular 2 系列资料
Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...
- Python体验(09)-图形界面之Pannel和Sizer
import wx class Form(wx.Frame): def __init__(self,parent,ID,title): wx.Frame.__init__(self,parent,ID ...
- autoit使用WMIC获取硬件信息
效果图: 直接上源码了 #cs ---------------------------------------------------------------------------- AutoIt ...
- Android开发:程序目录结构详解
HelloWorld程序的目录结构概述 我们可以在文件夹中看到,HelloWorld程序的目录主要包括:src文件夹.gen文件夹.Android文件夹.assets.res文件夹. AndroidM ...
- iOS 代码控制iPhone&iPad旋转
在APPDelegate中添加如下代码 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } p.p2 { margin ...
- python 常用高效代码写法集锦
一.打开文件 #使用 with 语句操作文件对象 with open(r'somefileName') as somefile: for line in somefile: print line # ...
- 第六百一十八天 how can I 坚持
此刻好烦,乱七八糟的,红米2死机也没弄好,哎. 下周三去长城,感觉还有很大提升空间啊,什么都不会.哎. 眼累的不行了,得抓紧睡觉了.ls他们来北京开年会了.. 明天,zjp来找我玩呢. 睡觉了.累.