HDU 4497 Prime sieve, composite decomposition

Source: Internet
Author: User

Test instructions: Give you two numbers, G and L, they are a group of numbers of gcd (greatest common divisor) and LCM (least common multiple), find the number of combinations that satisfy the condition, each combination has three numbers, the sorting is different, also is different combination.

L:P1^T1 * P2^t2 ... * pi^ti

G:Q1^S1 * Q2^s2 ... * qi^si

(PI and qii are prime Numbers II)

All the numbers appearing in G (greatest common divisor) have to appear in L (least common multiple), or L mod g=0, and someone asks why? In fact, think about it clearly, if the a,b,c least common multiple can be a,b,c any one divisible, and their greatest common divisor must be divisible a,b,c any one, of course, l%g=0, if l%g!=0 show no match the number of conditions, the direct output 0 is good.

There is a set of numbers (A,B,C)

A = P1^i1 * p2^i2 * p3^i3 * ...

b = p1^j1 * p2^j2 * p3^j3 * ...

c = p1^k1 * P2^K2 * P3^K3 * ...

(Pi is prime)

Their g=p1^min (I1,J1,K1) *p2^min (I2,JI2,K2) * ...

Their L=p1^max (I1,J1,K1) *p1^max (I2,J2,K2) * ....

Make k=l/g;

So k=p1^x1*p2^x2 ...

Now we can make a,b,c them three is G, now to let their least common multiple is L, for P1 a,b,c at least one number to be multiplied by p1^x1 to ensure that the P1 exponent of L, but also let their greatest common divisor equals g, at least one P1 exponent unchanged (that is, multiplied by p1^0), The other one is multiplied by p1^t1 (0<=T1<=X1), can guarantee the exponent of L in P1, in order to push class each pi to satisfy this condition, so each PI combination number has C (x1+1,1) *a (3,3) I, also considering

Ti=0, and Ti=xi, the two cases are ranked in the number of 3, so for each pi should be (3+3) +c (xi+1-2,1) *a (3,3) =6+ (x1-1) *6=6*x1, the last of the pi is multiplied.

#include <stdio.h>
#include <cstring>
#include <iostream>
#define LL Long Long
using namespace Std;
LL flag[100000];
LL prime[100000];
LL fa[100000];
ll Cnt=0;   
void factor (int nn)//Gets the prime power exponent
{
int cas=0;
for (int i=0;i<cnt&&prime[i]*prime[i]<=nn;i++)//about why PRIME[I]*PRIME[I]<=NN, if there is a larger prime number, prime[i]*       Prime[i]>nn (prinme[i]* larger prime number)
{
while (nn%prime[i]==0)
{
fa[cas]++;
Nn/=prime[i];     
}
if (Fa[cas])
cas++;

if (nn>1)
Fa[cas]=1;
}

int main ()
{
memset (flag,1,sizeof (flag));
for (int i=2;i<100000;i++)//prime sieve, the principle is to filter out multiples of each prime number, from small to large, but there are faster screening method, we can go online search a search
{
if (flag)
{
for (int j=i+i;j<100000;j+=i)
{
flag[j]=0;
}
}
}
memset (prime,0,sizeof (prime));
for (int i=2;i<100000;i++)
{
if (flag)
{
Prime[cnt++]=i;
}
}
ll T,g,l;
scanf ("%lld", &t);
while (t--)
{
memset (fa,0,sizeof (FA));
scanf ("%lld%lld", &g,&l);

ll k=l/g;

if (l%g!=0)
{
printf ("0\n");
Continue
}
Factor (k);
ll Sum=1;
for (int i=0;; i++)
{
if (!fa[i])
{
Break
}
Sum*=fa[i];
sum*=6;
}
printf ("%d\n", sum);
}
return 0;

}

HDU 4497 Prime sieve, composite decomposition

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.