该题没思路,参考了网上各种题解。。。。

注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9
进而简化:8 * (10^x-1)/9=L * k (k是一个整数)
8*(10^x-1)=9L*k
d=gcd(9L,8)=gcd(8,L)
8*(10^x-1)/d=9L/d*k
令p=8/d q=9L/d p*(10^x-1)=q*k
因为p,q互质,所以q|(10^x-1),即10^x-1=0(mod q),也就是10^x=1(mod 9*L/d)
由欧拉定理可知,当q与10互质的时候,10^(φ(q))=1 (mod q),即必定存在一个解x。
而题目中要求的是最小的解,设为min,那么有a^min=1%q,因为要满足a^φ(q)=1%q,那么a^φ(q)肯定能变换成(a^min)^i。
所以接下来只要枚举φ(q)的因子,找出符合条件的最小者即可。

无解的时候就是q与10不互质的时候,因为若q与10有公因子d:
1.若d=2,q=2*k,那么10^x=2^x*5^x=1%2k
   即2^x*5^x=1+2k*m,左边为偶数,右边为奇数,显然矛盾。
2.若d=5,q=5*k,那么10^x=2^x*5^x=1%5k
   即2^x*5^x=1+5k*m,左边是5的倍数,右边不是5的倍数,显然矛盾。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
long long L; long long gcd(long long a,long long b) {
return b==?a:gcd(b,a%b);
}
long long multi(long long a,long long b,long long mod) {
long long ret=;
while(b) {
if(b&)
ret=(ret+a)%mod;
a=(a<<)%mod;
b=b>>;
}
return ret;
}
long long quickPow(long long a,long long b,long long mod) {
long long ret=;
while(b) {
if(b&)
ret=multi(ret,a,mod); //直接相乘的话可能会溢出
a=multi(a,a,mod);
b=b>>;
}
return ret;
}
//求欧拉函数
long long eular(long long n) {
long long ret=,i;
for(i=; i*i<=n; i++) {
if(n%i==) {
n=n/i;
ret*=i-;
while(n%i==) {
n=n/i;
ret*=i;
}
}
}
if(n>)
ret*=n-;
return ret;
} int main() {
int t=;
while(scanf("%I64d",&L)!=EOF) {
if(L==)
break;
long long p=*L/gcd(L,);
long long d=gcd(,p);
if(d==) {
long long phi=eular(p);
long long ans=phi;
long long m=sqrt((double)phi);
bool flag=false;
//先枚举大小在1~sqrt(phi)之间的因子
for(int i=; i<=m; i++) {
if(phi%i== && quickPow(,i,p)==) {
ans=i;
flag=true;
break;
}
}
//若1~sqrt(phi)没找到符合的因子,那么枚举sqrt(phi)~phi之间的因子
if(!flag) {
for(int i=m; i>=; i--) {
if(phi%i== && quickPow(,phi/i,p)==) {
ans=phi/i;
break;
}
}
}
printf("Case %d: %I64d\n",++t,ans);
} else {
printf("Case %d: 0\n",++t);
}
}
return ;
}

POJ 3696 The Luckiest number (欧拉函数,好题)的更多相关文章

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

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

  2. POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)

    Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...

  3. POJ 2407 Relatives(欧拉函数入门题)

    Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...

  4. POJ 2407:Relatives(欧拉函数模板)

    Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accept ...

  5. hdu 1286 找新朋友 欧拉函数模版题

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  6. (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

    题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

  7. UVA 10820 欧拉函数模板题

    这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...

  8. poj2407(欧拉函数模板题)

    题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...

  9. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. iFreeThinking - 记录生活,分享思考

    http://www.ifreethinking.com iFreeThinking.com 是一个非营利性个人博客网站.开于 2014 年,博客主要记录分享一些思考和感悟. 文章列表:http:// ...

  2. strcpy/strlen/strcat/strcmp面试总结

    <strcpy拷贝越界问题> 一. 程序一 #include<stdio.h> #include<string.h> void main() { char s[]= ...

  3. Android--启动系统的剪切图像功能并返回结果

    直接上代码: //启动裁剪图片 private void cropPhotoUri(Uri uri){ Intent intent = new Intent("com.android.cam ...

  4. 条款20:以const-reference传递替换by-value传递

    缺省情况下,C++中函数参数的传递方式为by-value.即函数都是以实际参数的副本进行传递,而函数返回的也是一个副本.考虑如下实例程序: #include <iostream> clas ...

  5. 自己编写基于MVC的轻量级PHP框架

    做WEB开发已有三年,每次都写重复的东西, 因此,想自己写一下框架,以后开发方便.本人之前asp.NET一年开发,jsp半年,可是后来因为工作的原故换成PHP.其实很不喜欢PHP的语法.还有PHP的函 ...

  6. 20145120 《Java程序设计》第8周学习总结

    20145120 <Java程序设计>第8周学习总结 教材学习内容总结 NIO使用频道(channel)来衔接数据节点 read()将ReadableByteChannel中的数据读至By ...

  7. 针对《来用》的NABC分析

    项目名:<来用> 特点:拥有以往win7在内的众多小游戏 NABC分析 N(need需求): 之所以有这个想法是因为,在WIN7,XP系统中往往有很多众所周知的小游戏(比如扫雷),但是在w ...

  8. mysql几个命令

    1.格式化输出 select * from mysql.user\G 2.显示版本 show version() 3.显示引擎 show engines mysql> show engines; ...

  9. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  10. mongodb 操作类

    在使用这个类之前,建议先自己去写,把方法都了解了再用,这样你就可以在适当的时候修个此类,另外请自己构建PagerInfo using System; using System.Collections. ...