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