POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit '8'.
Input
The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000).
The last test case is followed by a line containing a zero.
Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob's luckiest number. If Bob can't construct his luckiest number, print a zero.
Sample Input
8
11
16
0
Sample Output
Case 1: 1
Case 2: 2
Case 3: 0
题意:求最小的由8组成的数,是L的倍数。
思路:由一系列证明得到,ans=phi(N)的满足题意的最小因子。
关键: 对于X,求其满足题意的最小因子p|X,可以这样求,枚举素因子prime,如果X/prime满足题意,则X=X/prime。
存疑:我感觉复杂度是根号级别的,但是我看到ppt上说是log级别。
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
#define ll long long
ll gcd(ll a,ll b){ if(b==) return a;return gcd(b,a%b);}
ll qmul(ll a,ll x,ll Mod){ll res=; a%=Mod; while(x){if(x&) res=(res+a)%Mod;a=(a+a)%Mod;x>>=;} return res;}
ll qpow(ll a,ll x,ll Mod){ll res=; a%=Mod; while(x){if(x&1LL) res=qmul(res,a,Mod); a=qmul(a,a,Mod); x>>=;} return res;}
ll phi(ll x)
{
ll tx=x,res=x;
for(int i=;i*i<=tx;i++){
if(tx%i==){
res-=res/i;
while(tx%i==) tx/=i;
}
}
if(tx>) res-=res/tx;
return res;
}
ll find(ll Mod,ll py) //得到最小因子满足条件
{
ll n=py; ll tp[][]; int k=;
for(ll i=;i*i<=n;i++) //唯一分解
if(n%i==){
k++; tp[k][]=i; tp[k][]=;
while(n%i==){
n/=i; tp[k][]++;
}
}
if(n>) k++, tp[k][]=n, tp[k][]=; for(int i=;i<=k;i++)
for(int j=;j<=tp[i][];j++)
if(qpow(,py/tp[i][],Mod)==)
py/=tp[i][];
return py;
}
int main()
{
ll N,M,Case=;
while(~scanf("%lld",&N)&&N){
printf("Case %lld: ",++Case);
N=N*/gcd(N,);
if(gcd(N,)!=) printf("0\n");
else printf("%d\n",find(N,phi(N)));
}
return ;
}
POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)的更多相关文章
- poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <i ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
- poj 2773 利用欧拉函数求互质数
题意:找到与n互质的第 k个数 开始一看n是1e6 敲了个暴力结果tle了,后来发现k达到了 1e8 所以需要用到欧拉函数. 我们设小于n的 ,与n互质的数为 (a1,a2,a3.......a(p ...
- UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)
pid=26358">https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&cate ...
- 【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}
题意:求奇质数 P 的原根个数.若 x 是 P 的原根,那么 x^k (k=1~p-1) 模 P 为1~p-1,且互不相同. (3≤ P<65536) 解法:有费马小定理:若 p 是质数,x^( ...
- 欧拉函数求在1-n-1与n互质的个数
long long phi(long long x) { long long res=x,a=x,i; ;i*i<=a;i++) { ) { res=res/i*(i-); ) a=a/i; } ...
- 欧拉函数,打表求欧拉函数poj3090
欧拉函数 φ(n) 定义:[1,N]中与N互质的数的个数 //互质与欧拉函数 /* 求欧拉函数 按欧拉函数计算公式,只要分解质因数即可 */ int phi(int n){ int ans=n; ;i ...
- hdoj 1787 GCD Again【欧拉函数】
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU1695-GCD(数论-欧拉函数-容斥)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- (12)centos之stmp服务器
yum remove sendmail #卸载sendmail
- python decode unicode encode
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...
- luogu P1043 数字游戏
题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分 ...
- Java中ArrayList类
ArratList 类:存放同一数据类型容器(只能为引用数据类型,因实际其内部存放的是地址) 1.导入其所在包 import java.util.ArratList 2.创建对象 ArrayList& ...
- LucaCanali--SystemTap_Linux_IO
https://github.com/LucaCanali/Linux_tracing_scripts/tree/master/SystemTap_Linux_IO
- Python基础语法06--文件
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- Java 实现 淘宝秒杀 聚划算 自己主动提醒 源代码
说明 本实例可以监控聚划算的抢购button,在聚划算整点聚的时间到达时自己主动弹开页面(URL自定义). 能够自己定义监控持续分钟数,同一时候还能够通过多线程加快刷新速度. 源代码 package ...
- html中跳转方法(含设定时间)
脚本方式 如: <script language="JavaScript" type="text/JavaScript"> <!-- wind ...
- 【转载】.NET 框架浅析
经过了对C#较长时间的学习以及实践,不断地感受着.NET 框架--这个最本质,然而却似乎让人捉摸不透的概念.特别是经过拜读Anytao博主的<你必须知道的.NET>文章,使我对其概念关系得 ...
- [LightOJ 1018]Brush (IV)[状压DP]
题目链接:http://lightoj.com/volume_showproblem.php? problem=1018 题意分析:平面上有不超过N个点,如今能够随意方向划直线将它们划去,问:最少要划 ...