这里面的一个转换的小技巧很重要,把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. 洛谷—— P1120 小木棍 [数据加强版]

    https://www.luogu.org/problem/show?pid=1120 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接 ...

  2. 打造一个全命令行的Android构建系统

    IDE都是给小白程序员的,大牛级别的程序员一定是命令行控,终端控,你看大牛都是使用vim,emacs 就一切搞定” 这话说的虽然有些绝对,但是也不无道理,做开发这行要想效率高,自动化还真是缺少不了命令 ...

  3. UIButton上字体的对齐方式

    设置UIButton上字体的对齐方式,不是用: [Button.titleLabel setTextAlignment:UITextAlignmentCenter]; 而是用: [Button set ...

  4. 初探swift语言的学习笔记十(block)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/35783341 转载请注明出处 假设觉得文章对你有所帮助,请通过留言 ...

  5. 稀疏编码(Sparse Coding)的前世今生(二)

    为了更进一步的清晰理解大脑皮层对信号编码的工作机制(策略),须要把他们转成数学语言,由于数学语言作为一种严谨的语言,能够利用它推导出期望和要寻找的程式.本节就使用概率推理(bayes views)的方 ...

  6. Gym 100418J Lucky tickets(数位dp)

    题意:给定一个n.求区间[1, n]之间的全部的a的个数.a满足: a能整除  把a表示自身二进制以后1的个数 思路:题意非常绕.... 数位dp,对于全部可能的1的个数我们都dfs一次 对于某一个可 ...

  7. Cocos2D-x设计模式发掘之中的一个:单例模式

    http://www.tuicool.com/articles/NBRn2murl=pVtZACoQFKXC3u3uGwMLnTy4YDWihcVg0ata5gy506pmPpQEc0PO9hm6wG ...

  8. UVA 1541 - To Bet or Not To Bet 记忆化DP概率

    Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a n ...

  9. Java中Array、List、Set、Map

    一.Java中数组 数组用来存放固定数量的同类元素,声明方法: T[] ref,T ref[],如int[] intAry; int intAry[].推荐用T[]的方式,后一种方式为兼容C++习惯写 ...

  10. 杂项-Java:Spring Cloud

    ylbtech-杂项-Java:Spring Cloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册. ...