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 ...
随机推荐
- Redis数据结构之链表
Redis使用的链表是双向无环链表,链表节点可用于保存各种不同类型的值. 一.链表结构定义1. 链表节点结构定义: 2. 链表结构定义: 示例: 二.链表在Redis中的用途1. 作为列表键的底层实现 ...
- golang测试框架--smartystreets/goconvey
视频教程和配套博客:goconvey - 课时 1:优雅的单元测试 Go 语言虽然自带单元测试功能,在 GoConvey 诞生之前也出现了许多第三方辅助库.但没有一个辅助库能够像 GoConvey 这 ...
- 装B技能GET起来!Apple Pay你会用了吗?
科技圈儿有一个自带光环的品牌 它每次一有任何动静 不用宣传 也不用刻意营销 消息还是能传天下 2月18日 你敢说你的朋友圈儿没有被下面这个词儿刷屏? Apple Pay 这不,我就跟着凑凑热闹,开个小 ...
- Windows Phone 8.1 开发实例 网络编程 天气预报
首先感谢林政老师的博客,给了我很大的指导. 准备工作 我的开发环境: - Visual Studio 2013(With Update 4) - Windows Phone 8.1 - Windows ...
- 设计模式之装饰(Decorator)模式
设计模式之装饰(Decorator)模式 (一)什么是装饰(Decorator)模式 装饰模式,又称为包装模式,它以对客户端透明的方式扩张对象的功能,是继承关系的替代方案之一. 装饰模式可以在不使用创 ...
- Chrome查看DNS状态提示:DNS pre-resolution and TCP pre-connection is disabled.
chrome://dns 别试了,在这个功能在旧版可以通过关闭预读可以实现,但是新版的不行. 但是可以通过这种方式替代: chrome://net-internals/#dns 这个方式更直观,可以看 ...
- Google代码风格配置文件(Java)(IDEA/Eclipse)
官网:http://www.cnblogs.com/EasonJim/p/7837474.html 下载: 安装: IDEA/Eclipse导入相应文件即可. 说明: Google代码风格文件的缩进是 ...
- Ubuntu 16.04安装JAD反编译工具(Java)
JAD反编译工具有个好处,就是字节码和源代码一起输出. 官网:https://varaneckas.com/jad/ 安装步骤: 1.下载: 离线版本:(链接: https://pan.baidu.c ...
- 【Java TCP/IP Socket】构建和解析自定义协议消息(含代码)
在传输消息时,用Java内置的方法和工具确实很用,如:对象序列化,RMI远程调用等.但有时候,针对要传输的特定类型的数据,实现自己的方法可能更简单.容易或有效.下面给出一个实现了自定义构建和解析协议消 ...
- 打印报表以显示具有给定责任的用户-FNDSCRUR责任用户
select --&p_hint distinct user_name, decode ( greatest (u.sta ...