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
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.
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.
2 6 72 7 33
72 0
容斥原理: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;
}