hihoCoder1284 Slim Chance (unique decomposition theorem + numerator)

Source: Internet
Author: User

Topic link #1284: Opportunity slim time limit: 5000ms single point time limit: 1000ms memory limit: 256MB description

Small hi recently in pursuit of a math girl little Z. Small z is actually want to reject him, but can not find good rhetoric, so put forward the request: for a given two positive integers n and m, small hi randomly selected an n ' approximate n ', small z randomly selected a m's approximate m ', if n ' and M ' equal, she promised small hi.

Small z let small hi to write this random program, then she review had no problem, you can draw the lottery. But small hi wrote, but more and more feel the opportunity is slim. So the question is, what is the odds that little hi can catch a little z?

Input

Each input file contains only a single set of test data.

The first behavior of each set of test data is two positive integers n and M, meaning as described earlier.

For 40% of data, meet 1<=n,m<=10^6

For 100% of data, meet 1<=n,m<=10^12

Output

For each set of test data, the output of two coprime positive integers A and B (with a per B indicates the chance that small hi can catch small z).

Sample input
3 2
Sample output
4 1

Analysis: to N and m decomposition, get N and M of each prime number of exponent en[],em[], and then take the Convention, that is, each exponent of the small to get the new et[], sum (et[])/SUM (en[]) * SUM (em[]) is asked, is to find one in EN, Find one in Em, 1/(Sum[en] * Sum[em]), altogether have sum[et] each
The title level is 2, then I wa several times, re several times =_= ...
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5typedefLong LongLL;6 Const intMax =10000000+5;//open to 6 times re7 BOOLFlag[max +5];8 intPrime[max +5], tot;9 intEn[max +5], Em[max +5], Et[max +5];Ten voidget_prime () One { Amemset (Flag,false,sizeof(flag)); -tot =0; -      for(inti =2; I <= Max;i + +) the     { -         if(!Flag[i]) -         { -Prime[++tot] =i; +              for(intj = i; J <= max/i; J + +) -Flag[i * j] =true; +         } A     } at } - voidGet_fact (LL N,int*temp) - { -memset (temp,0,sizeof(temp)); -      for(inti =1; I <= tot; i++) -     { in         if(Prime[i] >N) -              Break; to         if(n% prime[i] = =0) +         { -              while(n% prime[i] = =0) the             { *n = n/Prime[i]; $temp[prime[i]]++;Panax Notoginseng             } -         } the     } +     if(N >1) Atemp[n]++; the } +LL Get_sum (inttemp[]) - { $LL sum =1; $      for(inti =1; I <= tot; i++) -     { -         if(Temp[prime[i]]) theSum *= (Temp[prime[i]] +1); -     }Wuyi     returnsum; the } - voidSolve () Wu { -Memset (ET,0,sizeof(ET)); About      for(inti =1; I <= tot; i++) $     { -         if(En[prime[i]) &&Em[prime[i]]) -         { -             intMinn = min (En[prime[i]], em[prime[i]);//take the smallest index AEt[prime[i]] + =Minn; +         } the     } - } $ ll GET_GCD (ll A, ll b) the { the     if(b = =0) the         returnA; the     returnGET_GCD (b, a%b); - } in intMain () the { the get_prime (); About LL N, m; thescanf"%lld%lld", &n, &m); the get_fact (n, en); the get_fact (M, EM); + solve (); -LL Numn =get_sum (en); theLL Numm =get_sum (EM);BayiLL NUMF =get_sum (ET); the  the    //The final result is NUMF/(Numn * numm) - so first to NUMF and Numn numerator, -Then put numerator after the numf and Numm numerator, finally Numn *Numm theLL x1 =NUMF; theLL T1 =get_gcd (Numn, NUMF); theX1 = x1/T1;  theNumn = Numn/T1; -  theLL t2 =get_gcd (Numm, x1); theX1 = x1/T2; theNumm = Numm/T2;94Numn = Numn *Numm; the  theprintf"%lld%lld\n", Numn, X1); the     return 0;98}
View Code

hihoCoder1284 Slim Chance (unique decomposition theorem + numerator)

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.