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 ...
随机推荐
- HDU 1358字符串循环节问题 ,next数组
求字符串循环节,要求每前i个字符串前缀是否循环,有的话打印出来. 我对j=next[i]数组(未优化,从0开始,第一个为-1,)理解:字符s[i]的前面的字符串,最长的相同的前缀和后缀 的长度,因此, ...
- Ajax 实现文件的下载
JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...
- webstorm调试(一)提示css未使用的选择器Selector is never used
一.css未使用的选择器Selector 今天写vue的时候,给动态绑定了一个class属性,然后样式里面就给了warning,看起来怪怪的,很不舒服
- BZOJ——1202: [HNOI2005]狡猾的商人
http://www.lydsy.com/JudgeOnline/problem.php?id=1202 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: ...
- 嵌入式cpu架构
原文:http://www.kaixin001.com/repaste/11007221_7220618944.html### 内容: 目前主要CPU架构有ARM.X86/Atom.MIPS.Powe ...
- 【JSON注解】注解@JsonIgnoreProperties和@JsonIgnore的另一个使用情况
之前关于这两个注解,是用在JSON循环引用的情况上,那么现在关于这两个注解,还可以使用在另外一种情况上 即: 一般标记在属性或者方法上,返回的json数据即不包含该属性 关于这种情况在什么时候会遇到呢 ...
- 子组件跟随父组件re-render
想象一下这种场景,一个父组件下面一大堆子组件.然后呢,这个父组件re-render.是不是下面的子组件都得跟着re-render.可是很多子组件里面是冤枉的啊!!很多子组件的props 和 state ...
- linux cat 文件操作
简略版: cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename 只能创建新文件,不能编辑已有文件.3.将几个文 ...
- libxml/HTMLparser.h file not found
在导入asihttprequest包时出问题导入了libxml2.dylib.可是却提示libxml/HTMLparser.h file not found. 这是由于你的开发环境默认的路径无法找到这 ...
- struts.xml中为什么加上<constant name="struts.devMode" value="true" />就出错
严重: Exception starting filter struts2 java.lang.NullPointerException at com.opensymphony.xwork2.util ...