poj 2462 Period of an Infinite Binary Expansion
欧拉定理。根据分数转换成2进制的过程,分子每次都乘2。对于循环节x,当2^x = 1(mod b)时肯定是循环节。显然当分母不能整除2的时候,即分母和2互质的话,就可以利用欧拉定理,使得2^(Euler(b)) = 1(mod b)。然后对于Euler(b),枚举其因子,找到最小循环节就可以了。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
#define REP(i, n) for(int i = 0; i < n; i ++)
using namespace std;
const int N = 400100;
bool isp[N];
vector<int> p;
vector<LL> hav; void get_P()
{
CLR(isp, true);p.clear();
for(int i = 2; i < N; i ++)
{
if(isp[i])
{
p.push_back(i);
if(i < 1111) for(int j = i * i; j < N; j += i)
{
isp[j] = false;
}
}
}
} LL Euler_phi(LL n)
{
LL ret = n;
for(int i = 0; (LL)p[i] * p[i] <= n; i ++) if(n % (LL)p[i] == 0)
{
ret = ret / p[i] * (p[i] - 1);
while(n % p[i] == 0) n /= p[i];
}
if(n > 1) ret = ret / n * (n - 1);
return ret;
} LL Mul(LL a, LL b, LL mod)
{
LL ret = 0;
while(b)
{
if(b & 1)
ret = (ret + a) % mod;
a = a * 2 % mod;
b >>= 1;
}
return ret;
} LL Pow(LL a, LL b, LL mod)
{
LL ret = 1;
while(b)
{
if(b & 1) ret = Mul(ret, a, mod);
a = Mul(a, a, mod);
b >>= 1;
}
return ret;
} LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
} void get_hav(LL n)
{
hav.clear();
for(int i = 0; i < p.size() && n > 1; i ++)
{
while(n % (LL)p[i] == 0)
{
n /= p[i];
hav.push_back(p[i]);
}
}
if(n > 1) hav.push_back(n);
} int main()
{
int cas = 1;
LL ans, m, x, a, b, g;get_P();
while(scanf("%I64d/%I64d", &a, &b) != EOF)
{
g = gcd(a, b);
a /= g;b /= g;ans = 1;
while(b % 2 == 0)
{
ans ++;
b /= 2;
a %= b;
g = gcd(a, b);
a /= g;b /= g;
}
x = Euler_phi(b);
get_hav(x);
for(int i = 0; i < hav.size(); i ++)
{
if(Pow(2LL, x / hav[i], b) == 1)
x /= hav[i];
}
printf("Case #%d: %I64d,%I64d\n", cas ++, ans, x);
}
}
poj 2462 Period of an Infinite Binary Expansion的更多相关文章
- poj 3358 Period of an Infinite Binary Expansion
由乘2取整得到分数的小数位,可以找到规律!!! 例如:1/10,2/10,4/10,8/10,16/10,32/10,64/10…… 取整后:1/10,2/10,4/10,8/10,6/10,2/10 ...
- poj3358 Period of an Infinite Binary Expansion
Period of an Infinite Binary Expansion 题目大意:给你一个分数,求这个分数二进制表示下从第几位开始循环,并求出最小循环节长度. 注释:int范围内. 想法:这题说 ...
- poj3358 Period of an Infinite Binary Expansion 数论有难度
这道题目感觉好难,根本就是无从下手的感觉,尝试了以前的所有方法,都没有思路,毫无进展,参考了一下别人的思路,感觉学到了新的知识 接下来开始分析 观察1/10这组数据,按照二进制转化法可以得到: 1/1 ...
- Period of an Infinite Binary Expansion 题解
Solution 简单写一下思考过程,比较水的数论题 第一个答案几乎已经是可以背下来的,在此不再赘述 考虑我们已经知道了\((p,q)\),其中\((p \perp q) \wedge (q \per ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- poj 1961 Period
Period http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Description Fo ...
- KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period
首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
随机推荐
- 弹出对话框 UIAlertController
双选 //实例化UIAlertController var av=UIAlertController(title: "
- angularJs工作日记-自定义指令Directive01
新项目组使用完善的angularMVVM设计思路架构,很庆幸能够来到这个项目组,在这里的每一天都能够学习到新的知识,为了防止以后忘记,记录一下个人的理解 首先接触最多的是directive,direc ...
- 桂电在线-转变成bootstrap版
由于angularjs的不熟悉,而且SEO需要学习更多东西,于是先采用bootstrap版本,毕竟工作上也需要使用bootstrap,然后参照视频教程学习. bootstrap 基本模板 <!D ...
- fedora22 无法联网的情况下rpm安装gcc5.1
前天发生件很不幸的事.我在给ubuntu14.04安装NVIDIA显卡驱动的时候,想清空下一个目录,什么目录我也忘了,当时我正好切到root身份(平常我很少切root的),命令格式如下 rm -fr ...
- 图片延迟加载插件jquery.lazyload.js的使用方法
最新版的jquery.lazyload.js已不再是伪的延迟加载了 一.请按照基本使用方法说明设置 //载入JavaScript 文件 <script src="jquery.js&q ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- Lodash,你正在使用的JavaScript库
JavaScript工具库lodash发布了3.5版,成为了npm包仓库中依赖最多的库.它正在摆脱屌丝身份,成为开发者的不二之选. lodash一开始是Underscore.js库的一个fork,因为 ...
- 【HTTP】HTTP access control (CORS)
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Cross-site HTTP requests are H ...
- Delphi 在任务栏隐藏程序图标
Delphi 在任务栏隐藏程序图标 方法一:1.修改工程文件中的“Application.MainFormOnTaskbar := True;”为“Application.MainFormOnTask ...
- 通过Delphi获得qq安装路径
procedure TForm1.Button2Click(Sender: TObject); var Reg:TRegistry; Val:TStrings; ii:System.Integer; ...