给定\(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):a;}
ll euler(ll n){
ll ans=n;
for(ll i = 2; i*i <= n; i++){
if(n%i==0){
ans=ans/i*(i-1);
while(n%i==0) n/=i;
}
}
if(n>1) ans=ans/n*(n-1);
return ans;
}
ll fmp(ll a,ll b,ll m){
ll ans=0;
while(b){
if(b&1) ans=(ans+a)%m;
a=(a+a)%m;
b>>=1;
}
return ans;
}
ll fpw(ll a,ll n,ll m){
ll ans=1;
while(n){
if(n&1) ans=fmp(ans,a,m);
a=fmp(a,a,m);
n>>=1;
}
return ans;
}
int main(){
ll L,kase=0;
while(cin>>L){
if(L==0) break;
L=9ll*L/gcd(L,8);
printf("Case %lld: ",++kase);
if(gcd(10,L)!=1){
println(0);
continue;
}
ll p=euler(L);
ll ans=p;
for(ll i=1; i*i<=p; i++){
if(p%i!=0)continue;
if(fpw(10,i,L)==1){
ans=min(ans,i);
}
if(i*i!=p&&fpw(10,p/i,L)==1){
ans=min(ans,p/i);
}
}
println(ans);
}
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 神TM数论

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

  4. poj 3696 The Luckiest Number

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

  5. poj 1426(同余搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26926   Accepted: 111 ...

  6. poj 2251(同余)

    Ones Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11461   Accepted: 6488 Description ...

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

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

  8. POJ 1006 同余方程组

    以前的做法 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring& ...

  9. POJ 3696

    这里面的一个转换的小技巧很重要,把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). ...

随机推荐

  1. Mac notes

    1. Mac应用数据存放位置 ~/Library/Application Support/ 比如sublime text的应用数据~/Library/Application Support/Subli ...

  2. CAP理论与HBase

    The short summary of the article is that CAP isn't "C, A, or P, choose two," but rather &q ...

  3. css总结16:HTML5 多媒体音频(Audio)视频(video )

    1 显示嵌入网页中的 MP3 文件: <embed height="50" width="100" src="horse.mp3"&g ...

  4. Maven类包冲突终极三大解决技巧 mvn dependency:tree

    Maven对于新手来说是<步步惊心>,因为它包罗万象,博大精深,因为当你初来乍到时,你就像一个进入森林的陌生访客一样迷茫. Maven对于老手来说是<真爱配方>,因为它无所不能 ...

  5. Redis主从服务部署

    Redis__WindowsServer主从服务部署及调用实例       一.先谈谈单个Redis服务的安装         使用的redis是2.8.17版本,从官网下载解压缩后文件内容为:   ...

  6. APUE(3)---文件I/O (2)

    七.函数write #include <unistd.h> size_t write(int fd, const void *buf, size_t nbytes); //若成功,返回已写 ...

  7. MongoDB整理笔记の安装及配置

    1.官网下载 地址:http://www.mongodb.org/downloads mongodb-linux-x86_64-2.4.9.tgz (目前为止,64位最新版本) 2.解压 切换到下载目 ...

  8. wp8 与wp7.5图标规格说明

    wp8 小图标 159*159 中图标 336*336 大图标 691*336 wp7.5 173*173

  9. MessageBox.Show() 多重用法

    MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBox.Show (IWin32Window, S ...

  10. Linux Linux下安装wine

    Linux下安装wine Linux下安装wine可以从源码编译安装,但一般都觉得麻烦,所以尽量利用yum进行安装,解决很多包的依赖关系. 首先安装一个epel rpm -ivh http://dl. ...