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 ...
随机推荐
- JavaScript 实现格式化字符串函数String.format (解决引号嵌套转义符问题)
在js开发中,我们可能会遇到这样一个问题 当需要通过js动态插入html标签的时候 特别是当遇到大量的变量拼接.引号层层嵌套的情况,会出现转义字符问题,经常出错 我们来看个例子 <!DOCTYP ...
- ZOJ 3717 二分+2-sat判定。
好久没有2-sat了,此题当复习之用,二分求最大值+2-sat判断可行,此题主要跪于题意:The results should be rounded to three decimal places. ...
- CodeForces - 813C The Tag Game (树的dfs遍历)
[传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...
- HDU - 5572 An Easy Physics Problem (计算几何模板)
[题目概述] On an infinite smooth table, there's a big round fixed cylinder and a little ball whose volum ...
- js long类型的日期转成Date,字符串StringBuilder拼接
longToDate.js //扩展Date的format方法 Date.prototype.format = function (format) { var o = { "M+" ...
- 【postMan】发送post请求,返回错误码415
解决方法: 参看:https://www.cnblogs.com/spec-dog/p/3731279.html 将Request的Content-Type:application/json;char ...
- scapy在wlan中的应用
Scapy 又是scapy,这是python的一个网络编程方面的库,它在wlan中也有很强大的应用.一般我们买块网卡,然后aircrack-ng套件爆破一下邻居的密码,其实我们可以用scapy写一些有 ...
- vim编码方式配置的学习和思考
哎呀呀,今天9月30号,立即就要十一长假了,心里还有点小小浮躁.工作已经基本做完,想成为技术大牛怎么能够如此浮躁.为了应付浮躁的心灵,决定写一篇小博,平静一把. 今天一个配置文件须要有中文,而且同事是 ...
- java开始到熟悉100-102
本次内容:arraylist() 1. package list; import java.util.ArrayList; import java.util.Date; import java.uti ...
- MongoDB与MySQL的插入性能测试【转】
1.1 MongoDB的简单介绍 在当今的数据库市场上,MySQL无疑是占有一席之地的.作为一个开源的关系型数据库,MySQL被大量应用在各大网站后台中,承担着信息存储的重要作用.2009年,甲骨文 ...