poj_3696_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*(10^x -1)/9=n*L
d=gcd(8,n)
8*(10^x -1)/d=9*n*L/d
p=8/d,q=9*n/d
p*(10^x -1)=L*q
p,q互质,10^x=1 mod q
根据同余定理可知,10^x ≡1(mod q)
根据欧拉定理可知当gcd(a,b)==1时,a^φ(b)≡1(mod b);
即可得出:当gcd(10,q)==1时 10^φ(q)≡1(mod q) 即通过枚举φ(q)的因子(最小因子)就能得出结果
#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
#define ll long long
#define N 1000010
using namespace std;
int prime[N];
int pn=0;
bool vis[N];
int ans[N];
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
long long P(long long n){ //返回euler(n)
long long res=n,a=n;
for(long long i=2;i*i<=a;i++){
if(a%i==0){
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
ll mul(ll a,ll b,ll m)
{
ll res=0;
while(b)
{
if(b&1) res+=a;
if(res>m) res-=m;
a+=a;
if(a>m)
a-=m;
b>>=1;
}
return res;
}
ll pow(ll a,ll b,ll m)
{
ll res=1;
while(b)
{
if(b&1) res=mul(res,a,m);
a=mul(a,a,m);
b>>=1;
}
return res;
}
ll p[10000],cnt[10000];
ll fac[100000];
ll cc,s;
ll phi(ll n)
{
ll ans=1,i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
n/=i;
ans*=i-1;
while(n%i==0)
{
n/=i;ans*=i;
}
}
}
if(n>1)
ans*=n-1;
return ans;
}
void split(ll x)
{
cc=0;
for(ll i=2;i*i<=x;i++)
{
if(x%i==0)
{
p[cc]=i;
int count=0;
while(x%i==0)
{
count++;x/=i;
}
cnt[cc]=count;
cc++;
}
}
if(x>1)
{
p[cc]=x;cnt[cc]=1;cc++;
}
}
void dfs(ll count,ll step)
{
if(step==cc)
{
fac[s++]=count;
return ;
}
ll sum=1;
for(int i=0;i<cnt[step];i++)
{
sum*=p[step];
dfs(count*sum,step+1);
}
dfs(count,step+1);
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
LL n;
int t=0;
while(~scanf("%lld",&n),n)
{
LL cnt=1;
int flag=0;
t++;
printf("Case %d: ",t);
LL q=9*n/gcd(8,n);
if(gcd(q,10)!=1)
{
puts("0");
continue;
}
LL phi=P(q);
split(phi);
s=0;
dfs(1,0);
sort(fac,fac+s);
//cout<<ans[0]<<endl;
for(int i=0;i<s;i++)
{
if(pow(10,fac[i],q)==1)
{
cout<<fac[i]<<endl;
break;
}
} }
}
poj_3696_The Luckiest number的更多相关文章
- [题解](同余)POJ_3696_The Luckiest Number
还是挺难的吧......勉强看懂调了半天 首先表达式可以写成 8(10^x -1)/9,题意为求一个最小的x使L | 8(10^x -1)/9 设d=gcd(L,8) L | 8(10^x -1)/9 ...
- poj 3696 The Luckiest Number
The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...
- POJ3696 The Luckiest number
题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...
- POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- HDU 2462 The Luckiest number
The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...
- The Luckiest number(hdu2462)
The Luckiest number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- [POJ3696]The Luckiest number(数论)
题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们 ...
- POJ 3696 The Luckiest number (欧拉函数,好题)
该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...
随机推荐
- jdk 动态代理 数据连接池
package com.itheima.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandl ...
- Starting cloudera-scm-server: * Couldn't start cloudera-scm-server的解决办法(图文详解)
bigdata@ubuntucmbigdata1:~$ sudo /etc/init.d/mysql start start: Job is already running: mysql bigdat ...
- Java反射机制分析指南
一.JAVA是动态语言吗? 一般而言,说到动态言,都是指在程序运行时允许改变程序结构或者变量类型,从这个观点看,JAVA和C++一样,都不是动态语言. 但JAVA它却有着一个非常突出的动态相关机制:反 ...
- 编写tab切换插件
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 无法找到msvcp90.dll的一个碰巧解决办法
作者:朱金灿 来源:http://blog.csdn.net/clever101 上周同事使用VS2008编译一个C++的控制台工程.工程在release模式下可以编译成功,但是运行总是出现无法 ...
- C++ Knowledge series Template & Class
Function Function is composed of name, parameter (operand, type of operand), return value, body with ...
- 我的java开发规范
关于文件的命名参考阮一峰的这篇文章:http://www.ruanyifeng.com/blog/2017/02/filename-should-be-lowercase.html,文中说文件名全部使 ...
- Java问题定位之Java线程堆栈分析
采用Java开发的大型应用系统越来越大,越来越复杂,很多系统集成在一起,整个系统看起来像个黑盒子.系统运行遭遇问题(系统停止响应,运行越来越慢,或者性能低下,甚至系统宕掉),如何速度命中问题的根本原因 ...
- Oracle三种循环例题:打印九九乘法表
数据库SQL三种循环语句(For.While.Loop) --如果要将执行结果输出,需要先执行 setserveroutput on 命令,在窗口里显示服务器输出信息 set serveroutput ...
- HTML5开发必备工具
现在除了移动APP开发之外,比较火的就是html5开发了,现阶段的HTML5被看做是Web开发者创建流行web应用的利器,增加了对视频和Canvas2D的支持,它的优点就是可以跨平台使用,比如你是开发 ...