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 ...
随机推荐
- 【Linux】Linux学习笔记(完结)
前言 在工作中发现Linux系统的重要性,于是计划重温下Linux,顺便记录笔记方便之后查阅. 磁盘分区 在Linux系统中,每个设备都被当成一个文件来对待:如IDE接口的硬盘文件名为/dev/hd[ ...
- 向map中追加元素
public class Demo01 { public static void main(String[] args) { String mapKey = "a"; Map< ...
- dd 命令详解
作用: dd 是一个Unix和类Unix系统中的命令, 主要功能为转换和赋值文件.在Unix和类Unix系统上, 硬件的设备驱动(如硬盘) 和特殊设备文件(如/dev/zero, /dev/rando ...
- whereis 命令详解
whereis 作用:whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 参数:-b 定位可 ...
- hiberation4 获取session
T t; Configuration cfg = new Configuration(); cfg.configure(); ServiceRegistry serviceRegistry = new ...
- Python文件读写 - 文件r+ a+ open读写实际表现[示例]
先说结论: 文件r+ open: 1. write()不能实现插入写,它总是覆盖写或附加写: 2. 如果文件一打开即write(),则从开头覆盖写; 3. 如果文件一打开,用f.seek()指定文件指 ...
- HTML5发布的意义
解决文档结构混乱 以前的文档结构过度依赖div,HTML5推出了多种语义化标签,使得文档更利于阅读器等理解,更利于SEO优化. 解决浏览器之间的兼容性问题 市场上浏览器种类繁多,每个浏览器厂商都在做自 ...
- python字符集的转换(mysql数据乱码的处理)
本文参考:http://blog.csdn.net/crazyhacking/article/details/39375535 chardet模块:http://blog.csdn.net/tianz ...
- css实现椭圆、半椭圆
一.自适应的椭圆 1. 椭圆 css .ellipse{ width: 250px; height: 150px; margin: 50px; background: #FFD900; border- ...
- Upgrade with the Gradle Wrapper, gradlew升级
springboot 2.0需要gradle 1+, 而自动构建的都是3.+,手动升级如下 Upgrade with the Gradle Wrapper If your existing Gradl ...