Light oj 1236 - Pairs Forming LCM (约数的状压思想)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236
题意很好懂,就是让你求lcm(i , j)的i与j的对数。
可以先预处理1e7以内的素数,然后用来筛选出能被n整除的所有的素数以及素数的个数,时间复杂度是小于根号的。然后用DFS或者BFS选出所有的约数(不会很大)。
现在要是直接2个for利用gcd筛选lcm(x,y)==n的个数的话肯定超时,所以这里把每个素数看作一个位,比如:2 3 5这3个素因子,那我2可以看作2进制上的第一位(1),3第二位(10)...那一个约数就可以表示素因子相乘 也可以表示成一个二进制数 比如6表示成(11),那么要是两个约数的二进制数的'|'值等于n,那么lcm就等于n。然后处理出每个约数对应的二进制数。注意一点的是约数里的某个素因子不满其最大个数的话 就表示为0。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair <LL , int> P;
const int MAXN = 1e7 + ;
vector <P> G;
vector <LL> res;
bool prime[MAXN];
int p[MAXN / ]; void init() {
prime[] = true;
int cont = ;
for(int i = ; i < MAXN ; i++) {
if(!prime[i]) {
p[++cont] = i;
for(int j = i * ; j < MAXN ; j += i) {
prime[j] = true;
}
}
}
}
/*
DFS
void dfs(int dep , int end , LL num) {
if(dep == end) {
res.push_back(num);
return ;
}
LL temp = (1 << (dep));
for(int i = 0 ; i < G[dep].second ; i++) {
dfs(dep + 1 , end , num);
}
dfs(dep + 1 , end , num + temp);
}
*/
void bfs(int end) {
queue <P> que;
while(!que.empty()) {
que.pop();
}
que.push(P( , ));
while(!que.empty()) {
P temp = que.front();
que.pop();
if(temp.second == end) {
res.push_back(temp.first);
}
else {
for(int i = ; i <= G[temp.second].second ; i++) {
if(i == G[temp.second].second) {
que.push(P(temp.first + ( << temp.second) , temp.second + ));
}
else {
que.push(P(temp.first , temp.second + ));
}
}
}
}
} int main()
{
init();
int t;
LL n;
scanf("%d" , &t);
for(int ca = ; ca <= t ; ca++) {
scanf("%lld" , &n);
printf("Case %d: " , ca);
if(n == ) {
printf("1\n");
continue;
}
res.clear();
G.clear();
for(int i = ; (LL)p[i]*(LL)p[i] <= n ; i++) {
if(n % p[i] == ) {
int cont = ;
while(n % p[i] == ) {
n /= p[i];
cont++;
}
G.push_back(P((LL)p[i] , cont));
}
}
if(n > )
G.push_back(P(n , ));
int ans = ( << G.size()) - , cont = ;
//dfs(0 , G.size() , 0);
bfs(G.size());
for(int i = ; i < res.size() ; i++) {
for(int j = ; j < res.size() ; j++) {
if((res[i] | res[j]) == ans) {
cont++;
}
}
}
printf("%d\n" , cont / + );
}
}
Light oj 1236 - Pairs Forming LCM (约数的状压思想)的更多相关文章
- light oj 1236 - Pairs Forming LCM & uva 12546 - LCM Pair Sum
第一题给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,……em, 则结果为((1+2*e1)*(1+2*e2)……(1+2*em)+1)/2. 代码如下: #include <st ...
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- 1236 - Pairs Forming LCM
1236 - Pairs Forming LCM Find the result of the following code: long long pairsFormLCM( int n ) { ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)
链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...
- 1236 - Pairs Forming LCM -- LightOj1236 (LCM)
http://lightoj.com/volume_showproblem.php?problem=1236 题目大意: 给你一个数n,让你求1到n之间的数(a,b && a<= ...
- LightOJ 1236 Pairs Forming LCM 合数分解
题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...
- LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)
题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...
随机推荐
- Android UI学习 - FrameLayou和布局优化(viewstub)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/308090 Fram ...
- Android开发性能优化大总结
1. 采用硬件加速,在androidmanifest.xml中application添加android:hardwareAccelerated="true".不过这个需要在and ...
- css,html命名规则
css,html命名规则 页头: header 登录条: loginBar 标志: logo 侧栏: sideBar 广告: banner 导航: nav 子导航: subNav 菜单: menu 子 ...
- multipath 安装配置
二. 安装配置 2.1 安装Multipath 查看相关包: [root@rac1 ~]# rpm -qa|grep device-mapper device-mapper-multipath-0.4 ...
- 用Rational Rose来建立数据库表
这里以MS SQL Server2000中已有的一个Northwind库为例,我们命名新的数据库名为NorthwindRose:我们只挑其中的两个表Customers和Employees做示例,另外我 ...
- Android手动画柱状图的例子
效果图如上,网上看到的例子,谨以此文记录一下,以后用到的地方再来翻翻. 核心技术是用Canvas和Paint画长方形. 源码地址:http://download.csdn.net/detail/abc ...
- 在delphi中, reintroduce作用
在delphi中, reintroduce作用 当在子类中重载或者重新声明父类的虚方法时,使用 reintroduce 关键字告知编译器,可以消除警告信息.如: TPar ...
- K2 学习笔记
转:http://www.cnblogs.com/kaixuanpisces/category/149223.html k2 简介 工作流介绍 k2流程设计简介 K2流程设计详细版(图文)一 K2流程 ...
- system函数的总结
最近在看APUE第10章中关于system函数的POSIX.1的实现.关于POSIX.1要求system函数忽略SIGINT和SIGQUIT,并且阻塞信号SIGCHLD的论述,理解得不是很透彻,本文就 ...
- Maximum Random Walk(概率dp)
题意: 走n步,给出每步向左走概率l,向右走概率r,留在原地的概率 1-l-r,求能达到的最远右边距离的期望. 分析: 开始按期望逆求的方式分析,但让求的就是右边界没法退,懵了一会,既然逆着不能求,就 ...