Poj3358: Euler's Theorem

Source: Internet
Author: User
Tags decimal to binary

It is also a question which is determined by Euler .. Well, the key is to build an equation and pay attention to some simplification techniques.

Question:

Given a cyclic decimal number generated by P/Q, find the minimum cyclic section in the binary representation of this cyclic decimal number and not the prefix of the cyclic section.

Ideas:

Decimal to binary. We should take the remainder by 2 and set the cycle section with the length of Y starting from the decimal point X,

First, convert P/Q to the simplest score. At this time, p and q are mutually qualitative.

Then it must satisfy the same remainder equation p * 2 ^ x = p * 2 ^ (x + y) mod q

Sort out q | p * 2 ^ x * (2 ^ y-1) q | 2 ^ x * (2 ^ y-1) due to the mutual quality of p and q)

At this time, because 2 ^ Y-1 is an odd number, then there is a division formula we can know that the number of the factor of 2 in Q is X, so we can deal with Q to get X, change Q to Q/(2 ^ X );

Finally, we can obtain the homogeneous equation 2 ^ y = 1 (mod q)

Use Euler's theory to understand this same-remainder equation.

The Code is as follows:

#include <iostream>#include <stdio.h>#include<string.h>#include<algorithm>#include<string>#include<ctype.h>using namespace std;#define MAXN 10000long long gcd(long long a,long long b){    return b?gcd(b,a%b):a;}long long phi(long long n){    long long res=n;    for(int i=2;i*i<=n;i++)    {        if(n%i==0)        {            res=res-res/i;            while(n%i==0)            {                n/=i;            }        }    }    if(n>1)        res=res-res/n;    return res;}long long multi(long long a,long long b,long long m)//a*b%m{    long long res=0;    while(b>0)    {        if(b&1)            res=(res+a)%m;        b>>=1;        a=(a<<1)%m;    }    return res;}long long quickmod(long long a,long long b,long long m) //a^b%m{    long long res=1;    while(b>0)    {        if(b&1)            res=multi(res,a,m);        b>>=1;        a=multi(a,a,m);    }    return res;}int main(){    long long p,q,x,y;    int cas=0;    while(scanf("%I64d/%I64d",&p,&q)!=EOF)    {        if(p==0)        {            puts("1,1");            continue;        }        cas++;        long long t=gcd(p,q);        x=1;        p/=t;q/=t;        while(q%2==0)        {            q/=2;x++;        }        long long m=phi(q);        y=m;        for(long long i=2;i*i<=m;i++)        {            if(m%i==0)            {                while(m%i==0)                    m/=i;                while(y%i==0)                {                    y/=i;                    if(quickmod(2,y,q)!=1)                    {                        y*=i;                        break;                    }                }            }        }        printf("Case #%d: %I64d,%I64d\n",cas,x,y);    }    return 0;}

 

Poj3358: Euler's 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.