这里面的一个转换的小技巧很重要,把888...8转换成(10^x-1)/9*8。神来之笔,佩服。

这样有(10^x-1)/9*8=L*p得10^x-1=L*p*9/8,设m=9*L/gcd(L,8)。这一步如何想到的呢?其实是为了使m与10互质而做的。因为这样必有m*p1=10^x-1。使得同余方程

10^x=1 mod m,相信到了这一步,都知道用欧拉定理了。于是只需求出phi(m),枚举其因子,使得同余方程成立即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define LL __int64
using namespace std; LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
}
LL fac[10000]; int cnt;
LL Euler(LL m){
LL res=m;
LL k=(LL)sqrt((double)m);
for(LL i=2;i<=k;i++){
if(m%i==0){
res=res-res/i;
while(m%i==0)
m/=i;
}
}
if(m>1)
res=res-res/m;
return res;
} LL multi(LL a,LL b,LL m){
LL ret=0;
while(b>0){
if(b&1) ret=(ret+a)%m;
b>>=1;
a=(a<<1)%m;
}
return ret;
} LL quick(LL a,LL k,LL m){
LL ans=1;
while(k){
if(k&1)
ans=multi(ans,a,m);
k=(k>>1);
a=multi(a,a,m);
}
return ans;
} int main(){
LL l; int kase=0;
while(scanf("%I64d",&l),l){
if(l%16==0||l%5==0) {
printf("Case %d: 0\n",++kase);
continue;
}
cnt=0;
LL m=l*9/gcd(l,(LL)8);
LL phi=Euler(m);
LL ans;
LL k=(LL)sqrt((double)phi);
for(LL i=1;i<=k;i++){
if(phi%i==0){
fac[cnt++]=i;
fac[cnt++]=phi/i;
}
}
sort(fac,fac+cnt);
for(int i=0;i<cnt;i++){
ans=quick((LL)10,fac[i],m);
if(ans==1){
printf("Case %d: %I64d\n",++kase,fac[i]);
break;
}
}
}
return 0;
}

  

POJ 3696的更多相关文章

  1. poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用

    题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <i ...

  2. 【POJ 3696】 The Luckiest number

    [题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...

  3. poj 3696 The Luckiest Number

    The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...

  4. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  5. POJ 3696 神TM数论

    鸣谢: http://blog.csdn.net/yhrun/article/details/6908470 http://blog.sina.com.cn/s/blog_6a46cc3f0100tv ...

  6. POJ - 3696 同余

    给定\(L\),求最小的\(x\)满足$ L|8\frac{10^x-1}{9} $ /*H E A D*/ inline ll gcd(ll a,ll b){return b?gcd(b,a%b): ...

  7. POJ 3422 矩阵取数 最小费用流拆点+负边

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9153   Accepted:  ...

  8. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  9. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

随机推荐

  1. 在Eclipse中搭建Dagger和Dagger2使用环境

    眼下Dagger有两个版本号,一个是square的Dagger1.x,另外一个是由google主导与squre联合开发的Dagger2. 本文介绍一下在Eclipse中搭建Dagger和Dagger2 ...

  2. what we benefit from big data

    大数据玩的是什么,趋势,故障,寿命? 物联网拉动的是终端厂商的销量,作为终端设备生产商(OEM).无论是汽车.手机.家电行业.最有理由推动物联网的普及,可是作为传统行业,玩"网"并 ...

  3. JAVA学习第五十一课 — IO流(五)流的操作基本规律

    转换流: InputStreamReader:字节到字符的桥梁.解码 OutputStreamWriter:字符到字节的桥梁.编码 流的基本规律 1.明白源和汇 源:InputStream.Reade ...

  4. Struts2值栈的相关操作

    import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import ...

  5. hdu2476String painter (区间DP)

    Problem Description There are two strings A and B with equal length. Both strings are made up of low ...

  6. java 中的静态(static)代码块

    类字面常量 final 静态域不会触发类的初始化操作 非 final static 静态域(以及构造器其实是一种隐式的静态方法) Class.forName():会自动的初始化: 使用 .class来 ...

  7. Reusability1

    Reusability 1. 复用的层面 1.1 代码级别复用 顾名思义,代码复用就是把代码都搬过来,这是最主要的复用 1.1.1 白盒复用 白盒的意思就是里面的东西我们都知道是什么,具体实现的方式也 ...

  8. Memched——C#操作

    Memched还是比较简单的,这里把C#的相关操作整理了一下,Mark~ /// <summary> /// 缓存操作类. /// </summary> /// <rem ...

  9. linux的chmod,chown命令 详解

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  10. Ubuntu16.04下沙盒数据导入到 Neo4j 数据库(图文详解)

    不多说,直接上干货! 参考博客 http://blog.csdn.net/u012318074/article/details/72793914   (表示感谢)  前期博客 Neo4j沙盒实验申请过 ...