给定\(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. 9. Palindrome Number 回文数的判断

    [抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...

  2. 重命名Docker容器

    重命名Docker容器: Docker rename [Old container name]  [New container name]

  3. linux安全关机脚本

    linux安全关机脚本 在断电4分钟后判断关键 目的:在断电以后服务器连接UPS,UPS最多只能支持5分钟也会没电,所以在这里做个判断,如果断电4分钟后,市电还没来就关机. 以下两个设备为两个下路由器 ...

  4. Controlling Session Behavior in Asp.Net MVC4

    Posted By : Shailendra Chauhan, 06 Jan 2013 Updated On : 11 Jun 2014 By default, Asp.Net MVC support ...

  5. Reading Text-based Files In ASP.NET

    Friday, July 17, 2015 1:43 PM Every time I need to work with the contents of text-based files in an ...

  6. 编写高质量代码改善C#程序的157个建议——建议1:正确操作字符串

    最近拜读了陆敏技老师的<编写高质量代码改善C#程序的157个建议>,感觉不错,决定把笔记整理一遍. 建议1: 正确操作字符串 字符串应该是所有编程语言中使用最频繁的一种基础数据类型.如果使 ...

  7. 使用Sencha Cmd创建脚本框架

    从Ext JS 4.1.1a 开始,为了配合 Sencha Touch开发  而设计了 Sencha Cmd这个跨平台的命令行工具. 要使用Sencha Cmd,必须先安装好 Java Run-tim ...

  8. [.net 多线程]ThreadPool的安全机制

    ThreadPool类,有两个方法我们没有用到,UnsafeQueueUserWorkItem 和UnsafeRegisterWaitForSingleObject. 为了完全理解这些方法,首先,我们 ...

  9. PLSQL导出语句的查询结果

    不需要把全部结果都展示出来

  10. C# Winform下一个热插拔的MIS/MRP/ERP框架(多语言方案)

    个别时候,我们需要一种多语种切换方案. 我的方案是这样的: 1.使用文本文本存储多语言元素,应用程序启动时加载到内存表中: 2.应用程序启动时从配置文件加载语种定义: 3.所有窗体继承自一个Base基 ...