Usaco Training 2.4.5 Fractions to decimals

Source: Internet
Author: User

Well... With a theorem, for a simplest fraction n/d,d= (2^x) * (5^y) *m,m≠1, then its loop length is the smallest L that makes the 10^l mod m==1, the non-cyclic length is max (x, y)

Then the question is nothing.

76 characters a line-break comparison pit, I used the stringstream ...

1#include <iostream>2#include <sstream>3#include <cstdio>4#include <iomanip>5#include <algorithm>6#include <string>7 using namespacestd;8typedefLong LongLL;9 strings;Ten intPow_mod (intAintBintMoD) One { ALLBase=a,ans=1; -      while(b) -     { the         if(b&1) ans=ans*Base%MoD; -         Base=Base*Base%MoD; -b>>=1; -     } +     return(int) ans; - } + intgcdintAintb) A { at     returnb==0? A:GCD (b,a%b); - } - intX,y,m,n,d;//n/d d=2^x * 5^y * M - intMain () - { -Freopen ("fracdec.in","R", stdin); inFreopen ("Fracdec.out","W", stdout); - StringStream SS; toCin>>n>>D; +     intg=gcd (n,d); -N/=g;d/=G; them=D; *      while(m>1&& (m&1)==0) m>>=1,++x; $      while(m>1&& (m%5)==0) m/=5,++y;Panax Notoginseng     if(m==1)  -     { the         if(n%d==0) ss<<n/d<<". 0"; +         ElseSs<<setprecision (max (x, y)) <<setiosflags (iOS::fixed) << (Double) n/(Double) D; A     } the     Else +     { -         inti; $          for(i=1;; ++i)if(Pow_mod (Ten, i,m) = =1) Break; $         //printf ("%d\n", I); -ss<<n/d<<'.'; -n%=D; the          for(intj=0; J<max (x, y) +i;++j) -         {Wuyi             if(J==max (x, y)) ss<<'('; then*=Ten; -ss<<n/D; Wun%=D; -         } Aboutss<<')'; $     } -Ss>>s; -     intsz=s.length (); -      for(intI=0; i<sz;++i) A     { +         if(i% the==0&& i!=0) Putchar (Ten); the Putchar (S[i]); -     } $Putchar (Ten); the     return 0; the}
View Code

Usaco Training 2.4.5 Fractions to decimals

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.