欧拉定理。根据分数转换成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的更多相关文章

  1. 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 ...

  2. poj3358 Period of an Infinite Binary Expansion

    Period of an Infinite Binary Expansion 题目大意:给你一个分数,求这个分数二进制表示下从第几位开始循环,并求出最小循环节长度. 注释:int范围内. 想法:这题说 ...

  3. poj3358 Period of an Infinite Binary Expansion 数论有难度

    这道题目感觉好难,根本就是无从下手的感觉,尝试了以前的所有方法,都没有思路,毫无进展,参考了一下别人的思路,感觉学到了新的知识 接下来开始分析 观察1/10这组数据,按照二进制转化法可以得到: 1/1 ...

  4. Period of an Infinite Binary Expansion 题解

    Solution 简单写一下思考过程,比较水的数论题 第一个答案几乎已经是可以背下来的,在此不再赘述 考虑我们已经知道了\((p,q)\),其中\((p \perp q) \wedge (q \per ...

  5. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  6. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  7. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  8. 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中的出现位置或者是次数 这个出现的次数是可 ...

  9. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

随机推荐

  1. MVVM模式应用 之为ApplicationBarIconButton 添加Command操作属性

    在学习MVVM的过程中,总是会遇到挫折,一碰到就是花费好长时间去解决..唉,不求量,只求质. 第一种(已经实践成功): (1)http://bindableapplicationb.codeplex. ...

  2. 【实习记】2014-08-20实习的mini项目总结

        实习项目总结文档 项目介绍 项目逻辑很简单,只有几个页面,只能登录,查看,支付和退款.主要作用是熟悉C++的cgi的web服务开发方式. 项目页面截图 图一:登录页面 图二:买家查看 图三:买 ...

  3. HTML 5结构

    进行总体布局时候,具体可以用的方法. 1.大纲:文档中各内容区块的结构编排. 内容区块可以使用标题元素来展示各级内容区块的标题. 关于内容区块的编排可以分为“显示编排”和“隐式编排”. 显示编排:明确 ...

  4. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  5. http://src.chromium.org/svn/ 定制chrome浏览器教程及源码

    chromium 官网登不进去,最近在学习chrome插件制作,网上教程很多大多没有源码 其实作为开源软件 官方提供了全部源码地址:http://src.chromium.org/svn/ PRESU ...

  6. Django 基础

    Django 的路由系统 在 django 的 URLconf 配置文件 urls.py 中根据一个 URL 对应 views 的一个函数来处理用户的请求. 1.基本的 urls 对应 urlpatt ...

  7. SEMAT[软件工程方法和理论 Software Engineering Method and Theory]

    Agile software development Agile software development is a group of software development methods bas ...

  8. linq 多个left join 和 sql union all -> linq union 方法

     (   from s in Base_SysMenus   join r in Base_RoleRights on s.Menu_Id equals r.Menu_Id into temp   f ...

  9. NET Core+Code First+Docker

    NET Core+Code First+Docker背景介绍 本文将会示范如何在Windows系统下基于ASP.NET Core构建跨平台服务,并通过Docker容器运行发布. 首先说一下为什么选择这 ...

  10. EasyPHP的Apache报错

    今天安装了最新版本的软件:EasyPHP-DevServer-14.1VC11-install.exe 启动报错: 打开Cport软件: 可见80端口被系统占用,导致Apache不能启动. (1)手动 ...