hdu4497——GCD and LCM(数论&容斥原理or排列组合)

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2710    Accepted Submission(s): 1186


Problem Description
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
 

Input
First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
 

Output
For each test case, print one line with the number of solutions satisfying the conditions above.
 

Sample Input
  
  
2 6 72 7 33
 

Sample Output
  
  
72 0
 

Source

 容斥原理:http://www.cppblog.com/vici/archive/2011/09/05/155103.html

题意:已知l,g其中g=gcd(x,y,z),l=lcm(x,y,z),问有x,y,z多少种组合使得关系成立。
        题解:已知x%g=y%g=z%g=0,l%x=l%y=l%z=0,所以l%g=0。这个可以判定是否存在(x,y,z)符合条件。

又l/g=p1^a1*p2^a2*p3^a3*...pk^ak.(pi为素数)

x/g=p1^b1*p2^b2*...*pk^(bk);同理得到y/g,z/g,指数为ci和di.

所以min(bi,ci,di)=0,不然最大公约数是pi^min(ai,bi,ci)*g>g,同理得到max(bi,ci,di)=ai。

之后就是怎么安排(bi,ci,di),其中一个为0,一个为ai,另一个任意0<=k<=ai。使用容斥原理求:

对pi指数的所有可能方案数:fi=所有的方式(ai+1)^3-不含0的情况ai^3-不含ai的情况ai^3+重复删去的即不含0,也不含ai的情况(ai-1)^3

(因为不含0和不含ai的情况是不允许的,所以需要用容斥去重)

所以答案就是ans=∑fi(1<=i<=k,fi=(ai+1)^3-2*ai^3+(ai-1)^3)


另一思路(排列组合):

如果L%G != 0,则结果为0;给L/G素因子分解,如果某一个因子p有k个,则有三种情况:只有一个数中有p^k;只有两个数中p^k;两个数中有因子p,也就是至少一个有p,至少一个没有p,但是除了以上两种情况。这三种情况的个数分别为:A(3, 1);A(3, 1);A(3, 2)*(k-1),做和整理得到6*k。通俗一点就是有两个数重复的情况下排列数只有三种,要么重复0,要么重复ai 所以3+3还是六种,答案每次乘6,乘ai次

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0xffffffff//0x3f3f3f3f
#define mod 997


const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[1000][1000];
int pa[M*10],rankk[M];
int head[M*6],vis[M*100];
int dis[M*100];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
//vector<int> g[M*10];
ll dp[50][1<<10][50];
int has[10500];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};


void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
struct node
{
    int v,w;
    node(int vv,int ww)
    {
        v=vv;
        w=ww;
    }
};
vector<int> g[M*100];
char str[100005];
int ind[2550];
int bit[50];
int K;


int main()
{
    int i,j,k,t;
    int l,g;
    int ans;
    getpri();
    scanf("%d",&t);
    while(t--)
    {
        ans=1;
        scanf("%d%d",&g,&l);
        if(l%g)
        {
            printf("0\n");
            continue;
        }
        n=l/g;
        for(i=0; prime[i]*prime[i]<=n; i++)
        {
            if(n%prime[i]==0)
            {
                k=0;
                while(n%prime[i]==0)
                {
                    n/=prime[i];
                    k++;  //计算k的种数
                }
                ans*=(k+1)*(k+1)*(k+1)-2*k*k*k+(k-1)*(k-1)*(k-1);
            }
        }
    if(n!=1)ans*=6; //指数ai=1时,这时就是一个(全排列 (0 0 1)+全排列(0 1 1)=3+3=6)
    printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值