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^ ...
随机推荐
- GridLayout(网格布局)
常用属性: 排列对齐: ①设置组件的排列方式: android:orientation="" vertical(竖直,默认)或者horizontal(水平) ②设置组件的 ...
- WCF、WebAPI、WCFREST、WebService 、RPC、HTTP 概念解释
在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...
- 文件监控只FileSystemWatcher控件
FileSYstemWatcher控件是用来监控一个文件系统或监控文件变化.该控件会通知文件创建.修改.删除的消息,分别通过Created事件.Changed事件和Deleted事件来处理对应的操作 ...
- Javascript 对象(Object)
//声明方式 //调用 Object构造函数创建 var user = new Object(); user.name = "小白"; user.say = function () ...
- jdk安装完为什么会有两个JRE?
jdk安装完为什么会有两个JRE? 之所以需要两套 jre ,是因为可以分担工作:当用户只需要执行 java 的程序时,那么 c:\program files\java\jre 下的 jre 就 ok ...
- Spring自定义注解配置切面实现日志记录
一: spring-mvc.xml: <!--配置日志切面 start,必须与mvc配置在同一个配置文件,否则无法切入Controller层--><!-- 声明自动为spring容器 ...
- javaSE练习3——数组
一.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值. package com.test; public class t01 { public stat ...
- 当尝试从ArcCatalog、.net应用或是Java应用中连接ArcGIS Server 时,显示下面任何一种错误提示: "Access Denied" 或 "The connection could not be made"
Error: 访问拒绝或无法连接错误 文章编号 : 29042 软件: ArcGIS Server 9.0, 9.1, 9.2, 9.3, 9.3.1 操作系统: Windows 2000, XP, ...
- 安装Android模拟器Genymotion【Android学习入门】
安装Android模拟器Genymotion 推荐教程:一个强大的Android模拟器Genymotion具体内容如下: 相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了 ...
- 将零散文件使用ICSharpCode.SharpZipLib压缩打包后一次性下载
public static Stream CreateZip(List<string> listPath, int level = 5) { MemoryStream mstream = ...