poj 3696 The Luckiest Number
The Luckiest Number
题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除。
注释:如果无解输出0。poj多组数据,第i组数据前面加上Case i: 即可。
想法:这题还是挺好的。我最开始的想法是一定有超级多的数输出0。然后...我就在哪里找啊找....其实这道题是一道比较好玩儿的数论题。我们思考:连续的8可用什么来表示出来?$\frac{(10^x-1)}{9}\cdot 8$。其实想到这一步这题就做完了。这题的精髓就在于告诉我们连续的连续的一串数的表达方式。想到这点其实有一个比较容易接受的方法:这鬼东西是一个等比数列。然后,式子就可以化成了以下的形式及推导
$\Rightarrow n|\frac{10^x-1}{9}\cdot 8$
$\Rightarrow 9\cdot n|(10^x-1)\cdot 8$
$\Rightarrow \frac{9\cdot n}{gcd(n,8)}|\frac{(10^x-1)\cdot8}{gcd(n,8)}$
$\because gcd(\frac n{gcd(n,8)},\frac8{gcd(n,8)})=1$
且$gcd(9,8)=1$
$\therefore gcd(\frac{9\cdot n}{gcd(n,8)},\frac{8}{gcd(n,8)})=1$
$\Rightarrow \frac{9\cdot n}{gcd(n,8)}|10^x-1$
$\Rightarrow 10^x\equiv1(mod\frac{9\cdot n}{gcd(n,8)})$
所以此时,我们只需要枚举mod数即可。但是有些操作是不必要的,在此,我们有两种简单的优化:
1.对于mod数取$\varphi$,然后暴力枚举$\varphi$的所有因子。时间复杂度$O(\sqrt{n})$,验证是用快速幂,时间复杂度O(logn),所以,总时间复杂$O(\sqrt{n}\cdot {logn})$。
2.用BSGS优化,我没想到(鸣谢CQzhangyu)。时间复杂度同理。
但是对于第一种我们可以用Miller_Rabin 和Pullard_rho进行爆炸般的优化,但是没什么必要......
最后,附上丑陋的代码......
#include <iostream>
#include <cstdio>
typedef long long ll;
using namespace std;
ll gcd(ll a,ll b)//只取一次mod的gcd,鸣谢EdwardFrog
{
return b?gcd(b,a%b):a;
}
ll quick_multiply(ll a,ll b,ll mod)//快速乘,防止爆longlong,虽然没有必要
{
ll ans=;
a%=mod;
b%=mod;
while(b)
{
if(b&) ans=(ans+a)%mod;
b>>=;
a=(a+a)%mod;
}
return ans;
}
ll quick_power(ll a,ll b,ll mod)//这题不爆longlong,但是这样是必须的,因为9*n在longlong范围内
{
ll ans=;
a%=mod;
while(b)
{
if(b&) ans=quick_multiply(ans,a,mod);
b>>=;
a=quick_multiply(a,a,mod);
}
return ans;
}
int main()
{
ll n;
ll cnt=;
while()
{
scanf("%lld",&n);
if(n==) return ;
printf("Case %lld: ",++cnt);
n=*n/gcd(n,);
ll m=n;
ll phi=n;
if(gcd(n,)!=)//这是欧拉定理所必须满足的,如果不行显然无解
{
printf("0\n");
continue;
}
for(ll i=;i*i<=m;++i)
{
if(m%i==)
{
phi=phi/i*(i-);
while(m%i==)
{
m/=i;
}
}
}
if(m!=) phi=phi/m*(m-);
// cout<<"phi="<<phi<<endl;调试信息
ll minn=phi;//我想取最小值,且最大值是phi
for(ll i=;i*i<=phi;i++)//这步是验证。
{
if(phi%i==)
{
if(quick_power(,i,n)==) minn=min(minn,i);
if(quick_power(,phi/i,n)==) minn=min(minn,phi/i);
}
}
printf("%lld\n",minn);
}
}
小结:错误,枚举一个数的因子其实是可以根号时间内完成的...我傻逼了......
还有,别忘记phi开始的初值是n,不是1.
poj 3696 The Luckiest Number的更多相关文章
- poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <i ...
- POJ 3696 The Luckiest number (欧拉函数,好题)
该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...
- POJ3696 The Luckiest number
题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...
- poj_3696_The Luckiest number
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- HDU 2462 The Luckiest number
The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...
- The Luckiest number(hdu2462)
The Luckiest number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 【POJ 3696】 The Luckiest number
[题目链接] http://poj.org/problem?id=3696 [算法] 设需要x个8 那么,这个数可以表示为 : 8(10^x - 1) / 9, 由题, L | 8(10^x - 1) ...
随机推荐
- R语言︱构造新序列
1.数值构造函数rep与seq #数值构造rep与seq rep(1:4,each=2)#依次重复1:4两遍 rep(1:4,2) #注意,重复1:4两遍 seq(from=3,to=5,by=0.2 ...
- freemarker中的if...elseif...else语句
freemarker中的if...elseif...else语句 1.设计示例 <#if student.studentAge lt 12> ${student.studentName}不 ...
- windows打开和关闭默认共享方法汇总
Windows启动时都会默认打开admin$ ipc$ 和每个盘符的共享,对于不必要的默认共享,一般都会把它取消掉,可当又需要打开此默认共享时,又该从哪里设置呢.经过自己的验证,汇总出一下方法. 一: ...
- 初识lucene(想看代码的跳过)
最早是在百度贴吧里看到的lucene这个名称,只知道跟搜索引擎有关,因为工作中一直以来没有类似的需求,所以没有花时间学习这方面的知识. 刚过完年,公司不忙,自己闲不住把<Netty权威指南> ...
- 内置函数:filter函数
功能: filter函数用于过滤序列,将满足条件的元素取出来构成新的序列. 用法: filter(function, iterable) 接受两个参数,第一个函数为过滤函数(返回True后者False ...
- View的放大->旋转->还原动画
以UIButton为例,创建一个类,继承于UIButton /*页面的创建用storyboard*/ .h文件 @interface PTSRecommendButton : UIButton - ...
- 一个TokenUtils程序,亲测可用
1. Token用途 token是HTTP请求的令牌,通俗一点说是凭证,目的是防止api被随意访问获取信息. 可使用随机数生成,也可以使用用户id.密码或时间之类进行排序或者加密进行声称. token ...
- 【BZOJ3924】幻想乡战略游戏(动态点分治)
[BZOJ3924]幻想乡战略游戏(动态点分治) 题面 权限题...(穷死我了) 洛谷 题解 考虑不修改 发现一个贪心的做法 假设当前放在当前位置 如果它有一个子树的兵的总数大于总数的一半 那么,放到 ...
- 进一步理解阿贾克斯(Ajax)
一.ajax简介 1.Asynchronous JavaScript and XML(异步的Javascript和XML) 2.是一种在无需重新加载整个网页的情况下能够更新部分网页的技术. 二.aja ...
- 【经验随笔】Java程序远程调试定位特定运行环境上出现的问题
Java后台程序远程调试 第一步:在JVM的启动参数中增加-Xdebug -Xrunjdwp:transport=dt_socket,address=6688,server=y,suspend=n 第 ...