题目链接: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 (约数的状压思想)的更多相关文章

  1. 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 ...

  2. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  3. 1236 - Pairs Forming LCM

    1236 - Pairs Forming LCM   Find the result of the following code: long long pairsFormLCM( int n ) {  ...

  4. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  5. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

  6. LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

  7. 1236 - Pairs Forming LCM -- LightOj1236 (LCM)

    http://lightoj.com/volume_showproblem.php?problem=1236 题目大意: 给你一个数n,让你求1到n之间的数(a,b && a<= ...

  8. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  9. 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) == ...

随机推荐

  1. MySQL select into 和 SQL select into

    现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去,虽然可以用以下语句进行复制,总觉得不爽,希望各位帮助下我,谢谢.  answer 01: create table d ...

  2. Jenkins User on Apt-Get Install Installation

    转自:http://stackoverflow.com/questions/6234016/jenkins-user-on-apt-get-install-installation QUESTION: ...

  3. uva live 6170

    Esspe-Peasee Esspe-Peasee is an ancient game played by children throughout the land of Acmania. The ...

  4. Samba 4.x.x全版本存在命令执行漏洞

    Samba 4.0.0到4.1.10版本的nmbd(the NetBIOS name services daemon)被发现存在远程命令执行漏洞.CVE编号为CVE-2014-3560.目前官方已经发 ...

  5. <pages validateRequest="false"/>在.net4.0中无效的问题

    再web.config中设置<pages validateRequest="false"/>在.net4.0中无效的问题 解决方案: <system.web> ...

  6. 8 种 NoSQL 数据库系统对比

    导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型NoSQL数据库的文章. 虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只 ...

  7. python模拟http请求2

    发现了一个非常好用的第三方module:requests,模拟接口非常简单. 详细了解请移步:http://docs.python-requests.org/en/latest/ 非常不错 #!cod ...

  8. 关于ecshop中jquery与js冲突解决的方案

    ECShop把AJAX事件和JSON解析的模块放在common/transport.js之中,可以说它也有自己封装的一套工具,这其实是很正常的.   但恰恰的,在封装JSON各种方法的同时对objec ...

  9. HDU 5379 Mahjong tree

    题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...

  10. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...