A simple number of topics in the finals of the Guangzhou Engineering School Competition

Source: Internet
Author: User
Tags greatest common divisor

Topic Link: F: I am a good person, the main topic: give you two number N and M, ask you how many pairs of positive integers to greatest common divisor is n, least common multiple is M.

Because 1 <= N, M <= 10000000000, the violence law definitely timed out, the game I also thought of the N and M decomposition, factorization and corresponding index stored in the map, the idea is correct, can not think of some details of the problem is not considered thoughtful, in no judgment N and M whether coprime Before blindly to decompose them, the result of course is WA a number of times, I am still struggling to ponder whether the idea is wrong, until today and wencong discussion to find this flaw. With the simple decomposition method has been able to within the time limit range, and then I use the prime sieve method to generate a prime number and then to decompose N and m significantly faster:

The code is as follows:

1#include <cstdio>2#include <cstring>3#include <string>4#include <cmath>5#include <cstdlib>6#include <vector>7#include <map>8#include <queue>9#include <algorithm>Ten#include <iostream> One using namespacestd; AtypedefLong LongLL; - ConstLL inf=0x3fffffff; - ConstLL maxn=100005; the  - BOOLVIS[MAXN]; - intPrime_num; -LL prime[10004]; + intInit (LL n=MAXN) - { +     intC=0; A      for(LL i=2; i<=n; ++i) at         if(!Vis[i]) { -prime[++c]=i; -              for(LL j= i*i; j<=n; j+=i) vis[j]=1; -         } -     returnprime_num=C; - } in  - voidFactor (LL N, map<ll,ll> &m) to { +LL tmp= sqrt (n+0.5), I; -      for(i=1; I<=prime_num && prime[i]<=tmp; ++i) the         if(n%prime[i]==0){ *             intnum=1; $              while((N/=prime[i])%prime[i]==0)  ++num;Panax Notoginsengm[prime[i]]=num; -         } the     if(n!=1) m[n]=1; + } A  the intMain () + { -     intT; $ LL x, y; $scanf"%d",&t); - Init (); -      while(t--){ thescanf"%lld%lld",&x,&y); -         if(x==y) {WuyiPuts"1"); the             Continue; -         } Wu         if(x>y) Swap (x, y); -         if(y%x) { AboutPuts"0"); $             Continue; -         } -Map<ll,ll>m1,m2; - factor (X,M1); A factor (Y,M2); +Map<ll,ll>:: iterator it; the  -LL ans=1; $          for(it= m2.begin (); It!=m2.end (); + +it) the             if(It->second! = M1[it->first]) ans<<=1; theprintf"%lld\n",ans>>1); the     } the     return 0; -}
View Code

A simple number of topics in the finals of the Guangzhou Engineering School Competition

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.