3358 Period of an Infinite Binary Expansion 给出十进制数p和q,将p/q表示成二进制小数,并求出二进制小数的循环节起始位置和循环节的长度 欧拉函数

Period of an Infinite Binary Expansion
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1461 Accepted: 313

Description

Let {x} = 0.a1a2a3... be the binary representation of the fractional part of a rational number z. Suppose that {x} is periodic then, we can write

{x} = 0.a1a2...ar(ar+1ar+2...ar+s)w

for some integers r and s with r ≥ 0 and s > 0. Also, (ar+1ar+2...ar+s)wdenotes a nonterminating and repeating binary subsequence of {x}.

The subsequence x1 = a1a2 ... aris called the preperiod of {x} and x2 = ar+1ar+2 ... ar+s is the period of {x}.

Suppose that |x1| and |x2| are chosen as small as possible then x1 is called the least preperiod and x2 is called the least period of {x}.

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

However, we can write 1/10 also as 1/10 = 0.0(0011)w and 0 is the least preperiod and 0011 is the least period of 1/10.

The least period of 1/10 starts at the 2nd bit to the right of the binary point and the the length of the least period is 4.

Write a program that finds the position of the first bit of the least period and the length of the least period where the preperiod is also the minimum of a positive rational number less than 1.

Input

Each line is test case. It represents a rational number p/q where p and q are integers, p ≥ 0 and q > 0.

Output

Each line corresponds to a single test case. It represents a pair where the first number is the position of the first bit of the least period and the the second number is the length of the least period of the rational number.

Sample Input

1/10 
1/5 
101/120 
121/1472

Sample Output

Case #1: 2,4 
Case #2: 1,4 
Case #3: 4,4 
Case #4: 7,11

 

解析:我们可以观察一下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'')由于q''和2互质,所以必定有解,而且这个是属于高次同余方程,有经典的方法可以做,大家可以自己去查阅资料解决。来自J_Factory的学习空间

这个题目是求两个数相除p/q,结果的小数部分用二进制表示,当q不是2的幂时,这个二进制是个无线循环的01串。
下面是个模拟小数部分按二进制表示,可以发现二进制传一定会有循环,因为p=p%q,既然是循环,又是模运行,这和p模q的阶有关,p%q的阶一定是q的欧拉函数的因子。这样转换成一个模方程:p*2^n = x(mod q),当然p和q要互素,2和q互素。在计算前把q中的2去掉,p,q同除最大公因数。然后从1开始枚举,所以的欧拉数的因子。

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值