hdu_4497GCD and LCM(合数分解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497
GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2151 Accepted Submission(s): 955
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.
6 72
7 33
0
//合数分解
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn = ;
int prime[maxn];
bool pri[maxn];
int cnt;
void init()
{
cnt = ;
pri[] = pri[] = ;
//prime[0] = 2;
for(int i = ; i < maxn; i++){
if(!pri[i]){ prime[cnt++] = i;
for(int j = i+i; j < maxn; j+=i)
{
pri[j]=;
}
}
}
return;
}
ll m1[],m2[];
ll c1[],c2[];
int main()
{
init();
int T;
ll G,L;
scanf("%d",&T);
while(T--)
{
memset(m1,,sizeof(m1));
memset(c1,,sizeof(c1));
memset(m2,,sizeof(m2));
memset(c2,,sizeof(c2));
scanf("%lld%lld",&G,&L);
int tm = ;
bool in = ;
bool fl = ;
//printf("%d\n",prime[1]);
if(L%G) fl = ;
for(int i = ; i< cnt; i++){
while(prime[i]<=L&&L%prime[i]==){
L = L/prime[i];
m2[tm] = prime[i];
c2[tm]++;
in = ;
}
if(in) tm++;
in = ;
}
if(L!=){
m2[tm] = L;
c2[tm] = ;
tm++;
}
for(int i = ; i < tm; i++){
while(m2[i]<=G&&G%m2[i]==){
G = G/m2[i];
m1[i] = m2[i];
c1[i]++;
}
in = ;
}
if(G!=){
fl = ;
}
/*puts("haha");
for(int i = 0; i < tm; i++){
printf("m1[%d]=%d; m2[%d]=%d;\n",i,m1[i],i,m2[i]);
printf("c1[%d]=%d; c2[%d]=%d;\n",i,c1[i],i,c2[i]);
}
*/
if(fl==) {puts(""); continue;}
ll ans = ;
for(int i = ; i < tm; i++){
//需要特判当c1[i] =c2[i]的情况
ll E;
if(c1[i]==c2[i]) E = ;
else E = c2[i]-c1[i]-;
/*
也可以用排列组合的想法,每次找到两个数包含首尾的两个值,然后中间的一个在从所有可能中选取一个。
这样最后的公式就是A(3,2)*(c2[i]-c1[i]-1);
所以
if(c2[i]>c1[i]) ans = ans*6*(c2[i]-c1[i]-1);
*/
ans = ans*((c2[i]-c1[i]+)*(c2[i]-c1[i]+)*(c2[i]-c1[i]+)-*(c2[i]-c1[i])*(c2[i]-c1[i])*(c2[i]-c1[i])+E*E*E);
}
printf("%lld\n",ans);
}
return ;
}
hdu_4497GCD and LCM(合数分解)的更多相关文章
- LightOJ 1236 Pairs Forming LCM 合数分解
题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...
- HDU 4497 GCD and LCM (合数分解)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- HDU 4610 Cards (合数分解,枚举)
Cards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 5317 合数分解+预处理
RGCDQ Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Perfect Pth Powers pku-1730(筛+合数分解)
题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26, 然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...
- pku1365 Prime Land (数论,合数分解模板)
题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h. 先把合数分解模板乖乖放上: ; ans != ; ++i) { ) { num ...
- UVA 10791 Minimum Sum LCM(分解质因数)
最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...
- hdu4497 GCD and LCM ——素数分解+计数
link:http://acm.hdu.edu.cn/showproblem.php?pid=4497 如果G%L != 0,说明一定无解. 把K = G / L质数分解,G / L = p1^t1 ...
随机推荐
- Python学习日记:day5-------dict字典
#字典dict------->唯一的映射类型 1.数据类型的划分 数据类型划分为可变数据类型和不可变数据类型. 不可变数据类型:tupe(元组).bool.int.str 可 ...
- Angular 报错 Can't bind to 'formGroup' since it isn't a known property of 'form'
错误描述 当form表单加FormGroup属性时报错 Can't bind to 'formGroup' since it isn't a known property of 'form' < ...
- Xamarin截取/删除emoji表情bug解决方案
大家都知道,一个英文=1字节,一个汉字2字节,而一个emoji表情=4个字节,在有这三种混用的时候,比如app聊天界面,那么删除和截取便成了很头痛的事情. 问题描述 截取导致乱码,如下图: 解决方案 ...
- 【Tomcat】重新打war包
Extract war in tomcat/webapps #!/bin/bash #----------------------------------------------- # FileNam ...
- Linux(CentOS6.5)修改默认yum源为国内的阿里云、网易yum源
官方的yum源在国内访问效果不佳. 需要改为国内比较好的阿里云或者网易的yum源 修改方式: echo 备份当前的yum源 mv /etc/yum.repos.d /etc/yum.repos.d.b ...
- [SDOI2009]E&D
题目描述 小E 与小W 进行一项名为“E&D”游戏. 游戏的规则如下: 桌子上有2n 堆石子,编号为1..2n.其中,为了方便起见,我们将第2k-1 堆与第2k 堆 (1 ≤ k ≤ n)视为 ...
- 使用@contextmanager装饰器实现上下文管理器
通常来说,实现上下文管理器,需要编写一个带有__enter__和 __exit__的类,类似这样: class ListTransaction: def __init__(self, orig_lis ...
- vue2.0 如何在hash模式下实现微信分享
最近又把vue的demo拿出来整理下,正好要做"微信分享"功能,于是遇到新的问题: 由于hash模式下,带有"#",导致微信分享的签证无效:当改成history ...
- eclipse的各种错误和解决方法
1.cannot import xxx because the project name is in use
- Tomcat localhost 8080打不开
最近发现一个问题,使用tomcat部署项目时,项目可以正常访问,但是localhost 8080打不开,总是出现404 为了找回那只可爱的tom,我先做了个测试 在tomcat的webapps文件夹下 ...