HDU44979 GCD and LCM (素因子分解+计数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497
题意:
求有多少种(x,y,z)使得最小公倍数为l,最大公约数为g
分析:
我们将l,g进行素因子分解;
非常明显当g有l没有的素因子 和g的某一个因子的次数大于l的这个因子的次数的时候答案为0;
然后是有答案的情况下,我们设g中某一个因子数的次数为num1,l中这个因子的次数为num2;
那么在决定x,y,z在这个因子上的次数时我们要这样考虑,至少有一个为num1,至少有一个为
num2,然后依据容斥原理能够得出这样的情况的方案数
代码许下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
using namespace std; int G[2][50],L[2][50]; int gcd(int a,int b)
{
if(b) return gcd(b,a%b);
return a;
}
int main()
{
int t,g,l;
scanf("%d",&t);
while(t--){
scanf("%d%d",&g,&l);
int cnt1=0,cnt2=0;
memset(G,0,sizeof(G));
memset(L,0,sizeof(L));
map<int ,int >mp1;
map<int ,int >mp2;
for(int i=2;i<=g;i++){
if(g%i==0){
G[0][cnt1]=i;
while(g%i==0){
G[1][cnt1]++;
g/=i;
}
cnt1++;
}
}
if(g>1){G[0][cnt1]=g;G[1][cnt1++]=1;}
for(int i=2;i<=l;i++){
if(l%i==0){
L[0][cnt2]=i;
while(l%i==0){
L[1][cnt2]++;
l/=i;
}
cnt2++;
}
}
if(l>1) {L[0][cnt2]=l;L[1][cnt2++]++;}
bool flag=0;
for(int i=0;i<cnt1;i++)
mp1[G[0][i]]=G[1][i];
for(int i=0;i<cnt2;i++)
mp2[L[0][i]]=L[1][i];
for(int i=0;i<cnt1;i++){
if(mp1[G[0][i]]>mp2[G[0][i]])
flag=1;
}
if(flag){ puts("0"); continue;}
long long ans=1;
//cout<<cnt1<<" "<<cnt2<<endl;
/*****
cout<<"*******"<<endl;
for(int i=0;i<cnt1;i++)
cout<<G[0][i]<<" "<<G[1][i]<<endl;
cout<<"*******"<<endl;
for(int i=0;i<cnt2;i++)
cout<<L[0][i]<<" "<<L[1][i]<<endl;
cout<<"*******"<<endl;
******/
for(int i=0;i<cnt2;i++){
int num1=mp1[L[0][i]];
int num2=mp2[L[0][i]];
if(num1==num2) continue;
long long tmp = (num2-num1+1)*(num2-num1+1)*(num2-num1+1);
tmp-=2*(num2-num1)*(num2-num1)*(num2-num1);
tmp+=(num2-num1-1)*(num2-num1-1)*(num2-num1-1);
ans*=tmp;
cout<<"tmp "<<tmp<<endl;
}
cout<<ans<<endl;
}
return 0;
}
HDU44979 GCD and LCM (素因子分解+计数)的更多相关文章
- HDU 4497 GCD and LCM 素因子分解+ gcd 和 lcm
题意: 给两个数,lll 和 ggg,为x , y , z,的最小公倍数和最大公约数,求出x , y , z 的值有多少种可能性 思路: 将x , y , z进行素因子分解 素因子的幂次 x a1 a ...
- 数论——算数基本定理 - HDU 4497 GCD and LCM
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- HDOJ 4497 GCD and LCM
组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu 4497 GCD and LCM 数学
GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...
- GCD 与 LCM UVA - 11388
题目链接: https://cn.vjudge.net/problem/23709/origin 本题其实有坑 数据大小太大, 2的32次方,故而一定是取巧的算法,暴力不可能过的 思路是最大公因数的倍 ...
- 简单数论总结1——gcd与lcm
并不重要的前言 最近学习了一些数论知识,但是自己都不懂自己到底学了些什么qwq,在这里把知识一并总结起来. 也不是很难的gcd和lcm 显而易见的结论: 为什么呢? 根据唯一分解定理: a和b都可被分 ...
- poj 2429 GCD & LCM Inverse 【java】+【数学】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9928 Accepted: ...
- HDU 4497 GCD and LCM (合数分解)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- hdu4497 GCD and LCM
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
随机推荐
- 优动漫PAINT画树教程
依次解析画树要点!让画树不再是难事~ 优动漫PAINT下载:http://wm.makeding.com/iclk/?zoneid=18597
- c++类模板初探
#include <iostream> #include <string> using namespace std; // 你提交的代码将嵌入到这里 ; template &l ...
- 路飞学城Python-Day11
[44.函数-生成器] 需求:有一个列表 [0,1,2,3,4,5,6,7,8,9],对这个列表循环+1 li = [0,1,2,3,4,5,6,7,8,9] li = map(lambda x:x+ ...
- caioj 1152 快速求模 (快速幂)
(1)开long long,不然中间结果会溢出 (2)注意一开始的初始化,保险一点. #include<cstdio> #include<cctype> #include< ...
- 什么是面向对象以及其意义,prototpye原型
什么是面向对象: 使用对象时,只关注对象提供的功能,不关注其内部的细节 例如:jquery 什么是对象: 对象是一个整体对外提供一些操作,比如 收音机 面向对象编程OOP的特点: 1.抽象:把主要的特 ...
- phpstorm 激活方法
1.本地破解激活(推荐) 下载JetbrainsCrack-2.5.6.jar 链接: http://pan.baidu.com/s/1miPpE2k 密码: w3yc 放到phpstorm安装目录下 ...
- Qt之图形(渐变填充)
简述 QGradient可以和QBrush组合使用,来指定渐变填充. Qt目前支持三种类型的渐变填充: QLinearGradient:显示从起点到终点的渐变. QRadialGradient:以圆心 ...
- poj-2758 Checking the Text
题意: 给定一个字符串,要求维护两种操作: I:在字符串中插入一个字符: Q:询问某两个位置開始的LCP. 插入操作<=200,字符串长度<=5w,查询操作<=2w: 题解: 第一道 ...
- 海思 3520D 移植Qt4.5.3 一
一.移植Qt4.5.3 1.获得 Qt4.5.3 的源码Qt4.5.3源码的原始包 qt-embedded-opensource-src-4.5.3.tar.gz 将其复制到 /opt 下, ...
- bzoj1211: [HNOI2004]树的计数(prufer序列+组合数学)
1211: [HNOI2004]树的计数 题目:传送门 题解: 今天刚学prufer序列,先打几道简单题 首先我们知道prufer序列和一颗无根树是一一对应的,那么对于任意一个节点,假设这个节点的度数 ...