题意:给你一个分数,求它在二进制下的循环节的长度,还有第一个循环节从哪一位开始。

For example, x = 1/10 = 0.0001100110011(00110011)w and 0001100110011 is a preperiod and 00110011 is a period of 1/10.

思路一:

我们可以观察一下1/10这组数据,按照二进制转换法(乘二法),我们可以得到:
1/10 2/10 4/10 8/10 16/10 32/10 ...
然后都分子都尽可能减去10,得到:
1/10 2/10 4/10 8/10 6/10 2/10 ...
这时候,发现出现了重复,那么这个重复就是我们要求的最小循环。
抽象出模型如下:对p/q
首先p'=p/gcd(p,q)
q'=q/gcd(p,q); 然后我们就是求p'*2^i == p'*2^j (mod q') (“==”表示同余,i<j)
经过变换得到:
p'*2^i*(2^(j-i)-1) ==0 (mod q')
也就是 q' | p'*2^i*(2^(j-i)-1)
由于gcd(p',q')=1,
得到: q' | 2^i*(2^(j-i)-1)
因为2^(j-i)-1为奇数,所以q'有多少个2的幂,i就是多少,而且i就是循环开始位置的前一位。
那么令q''为q'除去2的幂之后的数
此时 q'' | 2^(j-i)-1
也就是求出x,使得 2^x ==1 (mod q'')

就是求p*(2^i) == p*(2^j) (mod q),除过来就是2^(i-j)==1(mod q)

思路二:转自http://www.cnblogs.com/Konjakmoyu/p/5183339.html

实现方法:

方法一: 直接BSGS

求2^x==1(mod n),就先消因子,然后在BSGS求解。

过程中如果b%d!=0,那就说明在x>=T时无解。

方法二:欧拉定理

先消因子,则2与n互质。

求2^x==1(mod n),根据欧拉定理2^phi(n)==1(%n),然后找phi(n)的质因子k。

从小到大枚举质因子k,判断2^k==1(%n)则的得到答案。

代码

BSGS的:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; typedef long long LL;
const LL N=;
LL pl,bl;
LL p[N];
bool vis[N];
struct node{
LL d,id;
}bit[N]; bool cmp(node x,node y){
if(x.d==y.d) return x.id<y.id;
return x.d<y.d;
} LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==) {x=,y=;return a;}
LL tx,ty;
LL d=exgcd(b,a%b,tx,ty);
x=ty;y=tx-(a/b)*ty;
return d;
} LL find(LL x)
{
int l=,r=bl;
while(l<=r)
{
int mid=(l+r)>>;
if(bit[mid].d==x) return bit[mid].id;
if(bit[mid].d<x) l=mid+;
if(bit[mid].d>x) r=mid-;
}
return -;
} void exBSGS(LL b,LL &xx,LL &yy)
{
LL t,m,g,x,y,pm,am;
while(b%==) {b/=;xx++;}
t=;
for(int i=;i<=;i++)
{
if(t%b==) {yy=i;return ;}
t=t*%b;
}
m=(LL)(ceil((double)sqrt((double)b)));
pm=%b;bit[].d=%b;bit[].id=;
for(int i=;i<=m;i++)
{
bit[i].d=bit[i-].d*%b;
bit[i].id=i;
pm=pm*%b;
}
sort(bit+,bit++m,cmp);
bl=;
for(int i=;i<=m;i++)
{
if(bit[i].d!=bit[bl].d) bit[++bl]=bit[i];
}
exgcd(pm,b,x,y);
am=x%b+b;
t=%b;
for(int i=;i<=m;i++)
{
x=find(t);
if(x!=-) {yy=i*m+x;return ;}
t=t*am%b;
}
return ;
} int main()
{
freopen("a.in","r",stdin);
freopen("b.out","w",stdout);
LL T=,a,b;
char c;
while(scanf("%I64d%c%I64d",&a,&c,&b)!=EOF)
{
LL x,y;
LL g=exgcd(a,b,x,y);
a/=g,b/=g;
x=,y=-;
exBSGS(b,x,y);
printf("Case #%I64d: %I64d,%I64d \n",++T,x,y);
}
return ;
}

欧拉定理的:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; typedef long long LL;
const LL Max=(LL)1e6;
const LL N=Max+;
LL fl;
LL f[N]; LL gcd(LL a,LL b)
{
if(b==) return a;
return gcd(b,a%b);
} bool cmp(int x,int y){return x<y;} LL eular(LL x)
{
LL ans=x;
for(int i=;i*i<=x;i++)
{
if(x%i==) ans/=i,ans=ans*(i-);
while(x%i==) x/=i;
}
if(x>) ans/=x,ans=ans*(x-);
return ans;
} LL quickpow(LL a,LL b,LL mod)
{
LL ans=%mod;
while(b)
{
if(b&) ans=ans*a%mod;
a=a*a%mod;
b>>=;
}
return ans;
} void solve(LL b,LL &xx,LL &yy)
{
LL k=,x=b,ans=x;
while(b%==) xx++,b/=;
LL phi=eular(b);
for(int i=;i*i<=phi;i++)
{
if(phi%i==) f[++fl]=i,f[++fl]=phi/i;
}
sort(f+,f++fl,cmp);
for(int i=;i<=fl;i++)
{
if(quickpow(,f[i],b)==) {yy=f[i];return ;}
}
} int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
LL T=,a,b;
char c;
while(scanf("%I64d%c%I64d",&a,&c,&b)!=EOF)
{
LL x=,y=;
LL g=gcd(a,b);
a/=g,b/=g;
solve(b,x,y);
printf("Case #%I64d: %I64d,%I64d \n",++T,x,y);
}
return ;
}

【poj3358】消因子+BSGS 或 消因子+欧拉定理 两种方法的更多相关文章

  1. 高斯消元(Gauss消元)

    众所周知,高斯消元可以用来求n元一次方程组的,主要思想就是把一个n*(n+1)的矩阵的对角线消成1,除了第n+1列(用来存放b的)的其他全部元素消成0,是不是听起来有点不可思议??! NO NO NO ...

  2. 高斯消元和高斯约旦消元 Gauss(-Jordan) Elimination

    高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵. 在讲算法前先介绍些概念 矩阵的初等变换 矩阵的初等变换又分为矩阵的初等行变换和矩阵的初等列变换 ...

  3. 基于FPGA的key button等开关消抖,按键消抖电路设计

    最近要用上一个key消抖的功能.于是找到了之前写的并放入博客的程序,发现居然全部有问题.http://www.cnblogs.com/sepeng/p/3477215.html —— 有问题,包括很多 ...

  4. 为什么HashMap初始大小为16,为什么加载因子大小为0.75,这两个值的选取有什么特点?

    先看HashMap的定义: public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V> ...

  5. HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版

    题目链接:传送门 题目要求:求S(2004^x)%29. 题目解析:因子和函数为乘性函数,所以首先质因子分解s(2004^x)=s(2^2*x)*s(3^x)*s(167^x); 因为2与29,166 ...

  6. [问题2014A04] 解答

    [问题2014A04]  解答 (1) 由条件可得 \(AB+BA=0\), 即 \(AB=-BA\), 因此 \[AB=A^2B=A(AB)=A(-BA)=-(AB)A=-(-BA)A=BA^2=B ...

  7. key单片机按键抖动

    //write by:cyt //Time:2017-2-10 //Porject Name:key shake_destory #include<reg51.h> #define GPI ...

  8. Mongodb的性能优化问题

    摘要 数据库性能对软件整体性能有着至关重要的影响,对于Mongodb数据库常用的性能优化方法主要有: 范式化与反范式化: 填充因子的使用: 索引的使用: 一. 范式化与反范式化 范式是为了消除重复数据 ...

  9. POJ 3185 The Water Bowls (高斯消元)

    题目链接 题意:翻译过来就是20个0或1的开关,每次可以改变相邻三个的状态,问最小改变多少次使得所有开关都置为0,题目保证此题有解. 题解:因为一定有解,所以我们可以正序逆序遍历两次求出较小值即可.当 ...

随机推荐

  1. [转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1)

    [转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1) http://yanue.net/post-123.html ...

  2. 浅谈dynamic的简单使用用法

    今天看了博客园里面的dynamic用法,我犹豫从来没接触过,今天恶补了一下,把我对dynamic的认识分享了出来,大家一起学习. Visual C# 2010 引入了一个新类型 dynamic. 该类 ...

  3. Android实现简单音乐播放器(MediaPlayer)

    Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能 ...

  4. snmp数据包分析

    今天看了一下snmp数据包的报文格式,用wireshark抓了两个数据包来分析. 先说说snmp get-request的书报包格式吧,get-next-request,get-response,se ...

  5. Oracle EBS中查询Profile的各种SQL【转载】

    1.List E-Business Suite Profile Option Values For All Levels SELECT p.profile_option_name SHORT_NAME ...

  6. hadoop日志太大

    hadoop jobtracker日志太大在jobtracker服务器上的mapred-site.xml中添加以下参数: <property> <name>mapreduce. ...

  7. 问题:ldconfig

    显示加载库文件libjli.so时候出错. 解决办法 1.find / -name 'libjli.so'文件 路径在:/data0/home/app/act/jdk/jdk1.7.0_15/jre/ ...

  8. 安装配置Apache2.4和php7.0

    接下来就要进入到PHP的学习了,所以要安装Apache服务器和PHP,从昨天开始一直到刚刚才配置完成,中间也遇到了一些问题,所以整理一下写了下来.接下来就是Win64位系统配置Apache2.4和PH ...

  9. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  10. android 后台代码设置动画

    1.设置旋转动画 final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF, 0. ...