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 ...
随机推荐
- async和enterproxy控制并发数量
聊聊并发与并行 并发我们经常提及之,不管是web server,app并发无处不在,操作系统中,指一个时间段中几个程序处于已经启动运行到完毕之间,且这几个程序都是在同一处理机上运行,并且任一个时间点只 ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [置顶]
xamarin android自定义spinner
以前弄的一个下拉框时自带的spinner,感觉好丑,实际效果实在满足不了基本的UI界面要求,还是自己动手丰衣足食,看了网上关于android中自定义spinner的文章,感觉实现原理还是比较简单,所以 ...
- Servlet与Jsp的结合使用实现信息管理系统一
PS:1:先介绍一下什么是Servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地 ...
- JavaScript的简单入门
一.导读 简介:JavaScript简称js,是基于对象和事件驱动的脚本语言,主要运用于客户端.原名LiveScript,本身和Java没有任何关系,但语法上很类似. 特点:交互性(它可以做的就是信息 ...
- requests爬取网页的通用框架
概述 代码编写完成时间:2017.12.28 写文章时间:2017.12.29 看完中国大学MOOC上的爬虫教程后,觉得自己之前的学习完全是野蛮生长,决定把之前学的东西再梳理一遍,主要是觉得自己写的程 ...
- 怎么为WebStorm更换主题 修改字体样式
这篇文章主要用于帮助大家解决怎么为webstorm换theme. 首先,到选择一个自己喜欢的皮肤,Webstorm皮肤网址: http://phpstorm-themes.com/ 然后,选中你喜欢的 ...
- 关于Python的函数(Method)与方法(Function)
先上结论: 函数(function)是Python中一个可调用对象(callable), 方法(method)是一种特殊的函数. 一个可调用对象是方法和函数,和这个对象无关,仅和这个对象是否与类或实例 ...
- SpringMVC 支持使用Servlet原生API作为目标方法的参数
具体支持一下类型: * HttpServletRequest * HttpServletResponse * HttpSession * java.security.Pricipal * Locale ...
- Nginx 解决WebApi跨域二次请求以及Vue单页面问题
一.前言 由于项目是前后端分离,API接口与Web前端 部署在不同站点当中,因此在前文当中WebApi Ajax 跨域请求解决方法(CORS实现)使用跨域处理方式处理而不用Jsonp的方式. 但是在一 ...