poj 2462 Period of an Infinite Binary Expansion

时间:2023-02-12 13:06:09

欧拉定理。根据分数转换成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. KMP POJ 1961 Period

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

  5. POJ 1961 Period&lpar; KMP &rpar;&ast;

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

  6. poj 1961 Period

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

  7. KMP——POJ-3461 Oulipo &amp&semi;&amp&semi; POJ-2752 Seek the Name&comma; Seek the Fame &amp&semi;&amp&semi; POJ-2406 Power Strings &amp&semi;&amp&semi; POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  8. POJ 1961 Period(KMP)

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

  9. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

随机推荐

  1. Atitit 教育与培训学校 的计划策划 v2

    Atitit 教育与培训学校 的计划策划 1. 历史大趋势,教育的普及化1 2. 我们的目标,人人可以接受硕士博士教育,确保全球60亿人的普世化教育1 3. 学校的注册,可以选择在全球费用最优惠的地方 ...

  2. &lbrack;译&rsqb;管理IIS日志的存储

    原文:http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage Overv ...

  3. &lbrack;Bhatia&period;Matrix Analysis&period;Solutions to Exercises and Problems&rsqb;ExI&period;2&period;8

    For any matrix $A$ the series $$\bex \exp A=I+A+\frac{A^2}{2!}+\cdots+\frac{A^n}{n!}+\cdots \eex$$ c ...

  4. AIX filesystemcache引发的Oracle事故

    链接地址: http://www.jydba.net/aix-filesystemcache%e5%bc%95%e5%8f%91%e7%9a%84oracle%e4%ba%8b%e6%95%85/ A ...

  5. Ansible 安装与配置&lpar;一&rpar;

    公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...

  6. &lbrack;html&rsqb;CSS中的margin、border、padding区别

    图解CSS padding.margin.border属性W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层. ...

  7. Xamarin&period;Android Timer使用方法

    首先声明命名空间: using System.Threading; 然后创建Timer对象: Timer timer; 接着实例化timer并且给委托事件: TimerCallback timerDe ...

  8. JVM启动过程

    JVM启动过程包括:加载.连接.初始化 1.加载:就是将class文件加载到内存.详细的说是,将class文件加载到运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封 ...

  9. hive 下篇

    由于spark on hive 问题,导致无法插入数据,暂时使用spark进行hive操作 向分区表插入数据 hive> show partitions customers;OKpartitio ...

  10. Android logcat输出中文乱码

    使用adb的logcat 命令查看系统日志缓冲区的内容,会发现在CMD的界面面,直接输出的中文内容是乱码. 这个问题出现在使用logcat将日志直接打印在当前的DOS窗口的时候会出现:使用logcat ...