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 ...
随机推荐
- 百度蜘蛛IP地址到底代表什么含义?
百度蜘蛛IP地址到底代表什么含义,是不是不同的ip地址所代表的含义不一样呢?对权重和抓取是否有影响?哪些是无效的蜘蛛,哪些是站长工具的蜘蛛,那些又是百度自己真正的蜘蛛?百度蜘蛛,是百度搜索引擎的一个自 ...
- UE4.18.3 C++项目无法打开C++类问题(VS2017)
升级VS2017后,突然出现UE4创建C++项目无法打开VS问题.经过测试为VS2017升级后C++的桌面开发工具集采用了VC++2017 v141工具集,而UE4对该工具集尚未兼容,故找不到vs打开 ...
- BZOJ 2287 【POJ Challenge】消失之物(DP+容斥)
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 986 Solved: 572[Submit][S ...
- 如何在ssh远程linux服务器时不需要输入密码
目的: 期望A服务器在对B服务器执行ssh或者scp等命令的时候不需要输入密码 实现方法: 1.通过安装sshpass服务 2.通过密钥验证的方式 操作过程: 一.通过sshpass的方式达到密码非交 ...
- 一片非常有趣的文章 三分钟读懂TT猫分布式、微服务和集群之路
原文http://www.cnblogs.com/smallSevens/p/7501932.html#3782600 三分钟读懂TT猫分布式.微服务和集群之路 针对新手入门的普及,有过大型网站技 ...
- ORACLE数据库字符集处理
简介: ORACLE数据库字符集,即Oracle全球化支持(Globalization Support),或即国家语言支持(NLS)其作用是用本国语言和格式来存储.处理和检索数据.利用全球化支持,OR ...
- JAVA SSL
http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#InstallationAndCustom ...
- Web长连接推送
http://www.workerman.net/web-sender http://wahahachuang5.iteye.com/blog/2311313
- word break相关问题的解法
https://leetcode.com/problems/word-break/?tab=Description 以及 https://leetcode.com/problems/concatena ...
- [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对照度、亮度处理
前一篇文章讲述了Android拍照.截图.保存并显示在ImageView控件中,该篇文章继续讲述Android图像处理技术,主要操作包含:通过打开相冊里的图片,使用Matrix对图像进行缩放. ...