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) == ...
随机推荐
- hdu 4968 Improving the GPA (水 暴力枚举)
题目链接 题意:给平均成绩和科目数,求可能的最大学分和最小学分. 分析: 枚举一下,可以达到复杂度可以达到10^4,我下面的代码是10^5,可以把最后一个循环撤掉. 刚开始以为枚举档次的话是5^10, ...
- Nlog Layout
Nlog.config <targets> <target type="Console" name="trace" layout=&q ...
- HNOI2008越狱(快速幂)
快速幂水过,贴一下模版. ; var x,y,n,m:int64; function power(num,times:int64):int64; var temp:int64; begin then ...
- ASP.NET缓存OutputCache和Response.Cache之C#后台设置
一.ASPX页面缓存页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam=&qu ...
- DbgPrint输出格式 Unicodestring
DbgPrint输出格式 Unicodestring 1) 直接打印字符串. DbgPrint("Hello World!"); 2) 空结尾的字符串,你可以用普通得C语法表示字 ...
- 【C#学习笔记】类型转换
using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { " ...
- 最简单的视音频播放示例2:GDI播放YUV, RGB
前一篇文章对“Simplest Media Play”工程作了概括性介绍.后续几篇文章打算详细介绍每个子工程中的几种技术.在记录Direct3D,OpenGL这两种相对复杂的技术之前,打算先记录一种和 ...
- RecordWriter接口解析
RecordWriter是将Map/Reduce结果(Key-Value)输出到文件系统中. /** * <code>RecordWriter</code> writes th ...
- Android访问权限大全
android.permission.ACCESS_CHECKIN_PROPERTIES 允许读写访问”properties”表在checkin数据库中,改值可以修改上传( Allows read/w ...
- C++类与对象
[1]类的内存问题 类是抽象的,不占用内存,而对象是具体的,占用 存储空间.在一开始时弄清对象和类的关系是十分 重要的.[2]类的声明 如果在类的定义中既不指定private也不指定public,则系 ...