POJ 3358-period of an Infinite Binary Expansion (Euler function + Euler theorem)

Source: Internet
Author: User
Tags greatest common divisor

Period of an Infinite Binary ExpansionTime limit:MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 3358Appoint Description:System Crawler (2015-04-08)

Description

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

{x} = 0. a1a2 ... ar (aR+1ar+2 ... a R+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+1a< /c22>r+2 ... a R+s is the period of {x}.

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

For example, x = 1/10 = 0.0001100110011 (00110011)W and 0001100110011 are a preperiod and 00110011 is a PE Riod of 1/10.

However, we can write 1/10 also as 1/10 = 0.0 (0011)W and 0 are 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 of the line are 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 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: bis case #4:7,11

Test instructions: The length and starting position of the Loop section for a fraction of 2 decimal fractions.

Ideas: Address-------> Click to open the link. This problem re a morning, almost depressed to die, and then later to find Euler's function from the screening method to the direct method of the time on AC, to now also do not understand why screening method does not.

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0); LL y[10010];        ll Cnt;int gcd (ll a,ll B)//For greatest common divisor {while (b!=0) {int r=b;        B=a%b;    c=0; } return A;    ll Modexp (ll a,ll b,ll MoD)//Quick power modulo {ll res=1;        while (b>0) {a=a%mod;        if (b&1) {res=res*a%mod;        } b=b>>1;    A=a*a%mod; } return res;    ll Euler (ll N)//direct method to find Euler function {ll m=floor (sqrt (n+0.5));    LL ans=n;            for (LL i=2;i<=m;i++) {if (n%i==0) {ans=ans/i* (i-1);            while (n%i==0) {n/=i;    }}} if (n>1) ans=ans/n* (n-1); return ans;}    void FAC (LL x)//for approximate {cnt=0;    LL m=sqrt (x+0.5); for (LL i=1;i<=m;i++) {if (x%i==0) {y[cnt++]=i;        y[cnt++]=x/i;    }}}int Main () {LL a,b,i,j,k;    LL icase=1;    LL d,t,q;    LL Res;        while (~SCANF ("%lld/%lld", &a,&b)) {printf ("Case #%lld:", icase++);            if (a==0) {printf ("1,1\n");        Continue        } D=GCD (A, b);        a=a/d;        b=b/d;        T=1;            while (b%2==0) {b=b>>1;        t++;        } LL Xx=euler (b);        FAC (XX);        Sort (y,y+cnt);                for (i=0;i<cnt;i++) {if (Modexp (2,y[i],b) ==1) {res=y[i];            Break    }} printf ("%lld,%lld\n", t,res); } return 0;}


POJ 3358-period of an Infinite Binary Expansion (Euler function + Euler theorem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.