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\) 思路 ...
随机推荐
- 6410实现网卡(DM9000A)收发功能及ARP协议实现
1. 网卡硬件结构(DM9000A) 网卡的实质就是MAC通过MII接口控制PHY的过程. MAC主要负责数据帧的构建.数据差错检查.传送控制等. PHY是物理接口收发器,属于物理层,当它收到MAC过 ...
- Java反射机制DOME
Java反射机制 public class TestHibernate { @Test public void TestHb(){ try { Class cs = Class.forName(&qu ...
- javascript 闭包最简单理解
首先说3点与闭包有关系的东西. 一.变量的作用域 变量的作用域不难理解. 1.函数内部可以访问函数外部的变量,而函数外部不能访问函数内部的变量. 2.如果在函数内定义变量的时候,不加var,那么是全局 ...
- 一键安装lamp环境 centos
linux centos yum安装LAMP环境 /*************链接**************/http://www.cnblogs.com/suger/p/3832093.html ...
- GitHub的.gitignore文件设置
用Eclipse连接GitHub 在本地仓库(最上层文件夹)建立.gitignore文件后,所有子文件夹下对应文件或者文件夹在submit的时候就会被忽略. 我将Eclipse的workspace当作 ...
- Windows内核原理系列01 - 基本概念
1.Windows API Windows 应用编程接口(API)是针对WIndwos操作系统用户模式的系统编程接口,包含在WindwosSDK中. 2.关于.NET .NET由一个被称为FCL的类库 ...
- ubuntu 'Unable to correct problems, you have held broken packages' 错误
在用apt 安装软件时,有时会用国内的源以加快下载速度. 但是在使用ubuntu 14.04的过程中,这一过程可能会导致错误“Unable to correct problems, you have ...
- input 放大镜
<input results="s" type="search" size="20px" placeholder="搜 ...
- uicollectionview registerclass vs registernib
当cell是用代码实现的时候用registerclass,当cell是用xib文件实现的时候用registernib
- 前端mac下的工具
1.制作base64图片的 DataURLMaker imageAlpha 减少png图片尺寸 2.共享iphone X-Mirage 3.制作交互图 briefs 4.制作矢量图 sketch