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) == ...
随机推荐
- 函数ut_2_log
计算某个数的对数(最大的) 例如 16 计算后为 4 2的4次方为16 例如15 计算后为3 2的3次方为8 /******************************************** ...
- SharedPreferencesUtil
用于缓存一个临时的变量 比如 SharedPreferencesUtil.put(getApplicationContext(), "userImage", user.conten ...
- 自学了三天的SeaJs学习,解决了前端的一些问题,与小伙伴们一起分享一下!
我为什么学习SeaJs? [第一]:为了解决项目中资源文件版本号的问题,以及打包压缩合并等问题. [第二]:好奇心和求知欲.[我发现很多知名网站也都在使用(qq空间, msn, 淘宝等等),而且 Se ...
- LeetCode Reverse Linked List (反置链表)
题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...
- Squid 反向代理加速网站
本实例的域名是 wenjin.cache.ibm.com.cn,通过DNS的轮询 技术,将客户端的请求分发给其中一台 Squid 反向代理服务器处理,如果这台 Squid 缓存了用户的请求资源,则将请 ...
- win7/8下VirtualBox虚拟共享文件夹设置
1. 安装增强功能包(VBoxGuestAdditions) 打开虚拟机,运行ubuntu,在菜单栏选择"设备->安装增强功能",根据提示即可安装成功(成功后也可 以实现 ...
- 【转】使用autolayout常见错误
原文网址:http://www.cnblogs.com/xiaokanfengyu/p/4175091.html 使用autolayout常见错误 1:The view hierarchy is no ...
- Using Open Source Static Libraries in Xcode 4
Using Open Source Static Libraries in Xcode 4 Xcode 4.0.1 allows us to more easily create and use th ...
- 一些网站的Android客户端
实际上就是浏览器(WebView),外面包装上了用户体验更好的外壳
- C# DataGridView的列对象属性探讨 (未完待续)
比较难的几个属性的释义[1]: