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\) 思路 ...
随机推荐
- Android UI 绘制过程浅析(四)draw过程
前言 draw是绘制View三个步骤中的最后一步.同measure.layout一样,通常不对draw本身进行重写,draw内部会调用onDraw方法,子类View需要重写onDraw(Canvas) ...
- OpenGL ES(一.概念)
OpenGL ES是以手持和嵌入式设备为目标的高级3D图形应用程序编程接口,主要的支持平台是iOS,Android,Linux和Windows 1.顶点着色器 他可以用于通过矩阵变换位置,计算照明公式 ...
- WinForm DataGridView分页功能
WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件 .CS: 1 using System; ...
- setTimeout传递参数
window.SetTimeout = function (callback, millisec, param) { var args = Array.prototype.slice.call(arg ...
- onethink上传图片(资源)和预览
直接上干货 不废话了 普通上传: onthink框架 后台已经有图片和文件上传功能 controller里只需: public function addPicture(){ /* 调用文件上传组件上 ...
- android platform下载地址
大陆直接访问Android的光放网站一般情况下比较困难,特此列出了Android各个SDK版本的直接下载地址. ADT 23.0.4:https://dl.google.com/android/ADT ...
- Oracle 中新增字段后patch
begin ad_zd_table.patch('APPLSYS', 'CUX_3_GL_FARMER_BALANCE'); end ;
- 通过DIV+span方式模拟进度条的实现方法
上上周用FusionCharts做报表时,有个图是进度条的形式,其实在FusionCharts 3.0之后已经支持了(Linear Gauge),可惜现有系统用的还是1.2.3版本的,重新引入新版本有 ...
- mysql source导入报错ERROR 1366的解决方法
文件是utf8的,数据库表是utf8的,为什么客户端导入会报错呢? 发现客户端用的是gbk的 改为utf8后正常 SHOW VARIABLES LIKE 'character%'; +-------- ...
- heartbeat重要文件的配置参数说明
主要三个重要的文件:ha.cf, authkey, haresource 1)ha.cf的重要参数的说明: 参数 说明 debugfile /var/log/ha-debug h ...