[CF1007B]Pave the Parallelepiped[组合计数+状态压缩]
题意
\(t\) 组询问,给你 \(A, B, C\) ,问有多少组三元组 \((a, b, c)\) 满足他们任意排列后有: \(a|A,\ b|B,\ c|C\) 。
\(A,B,C,t\leq 10^5\)
分析
我们把三个数的所有因子用 \(2^3 - 1\) 个状态表示这个数是 \(A,B,C\) 中的哪几个数字的因子。
按照从小到大的顺序枚举3个数对应的集合,首先保证能够找到一种对应方式(每个数对应是谁的因子),相同的数集利用插板法计算方案避免重复。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<queue>
#define For(s) for(int s=1;s<8;s++)
using namespace std;
const int N=1e5 + 7;
typedef long long LL;
int n,T;
int sz[8],A[8],f[8],b[8]={0,1,1,2,1,2,2,3};
LL ans,tmp;
int gcd(int a,int b){
if(!b) return a;
return gcd(b,a%b);
}
int get(int x){
int res=0;
for(int i=1;i<=sqrt(x);i++)if(x%i==0){
res++;
if(i*i!=x) res++;
}
return res;
}
bool check(int a,int b,int c){
if((a&1) && (b&2) && (c&4)
return true;
if((a&1) && (c&2) && (b&4))
return true;
if((b&1) && (a&2) && (c&4))
return true;
if((b&1) && (c&2) && (a&4))
return true;
if((c&1) && (a&2) && (b&4))
return true;
if((c&1) && (b&2) && (a&4))
return true;
return false;
}
LL C(int n,int m){
if (m == 0) return 1;
if (m == 1) return n;
if (m == 2) return 1ll * n * (n - 1) / 2;
if (m == 3) return 1ll * n * (n - 1) * (n - 2) / 6;
}
void work(){
scanf("%d%d%d",&A[0],&A[1],&A[2]);
ans=0; memset(sz,0,sizeof(sz));
memset(f,0,sizeof(f));
For(S){
for(int i=0;i<3;i++) if(S>>i&1){
if(!f[S]) f[S]=A[i];
else f[S]=gcd(f[S],A[i]);
}
f[S]=get(f[S]);
}
For(s) For(S)if((S&s)==s){
int cnt=b[S]-b[s];
sz[s]+=f[S]*(cnt&1?-1:1);
}
For(s1)for(int s2=s1;s2<8;s2++)for(int s3=s2;s3<8;s3++){
if(check(s1,s2,s3)){
tmp=1;int cnt=1,cho=233;
if(s1==s2) cho=s1,cnt++;
if(s2==s3) cho=s2,cnt++;
if(s1^cho) tmp*=sz[s1];
if(s2^cho) tmp*=sz[s2];
if(s3^cho) tmp*=sz[s3];
if(cnt^1) tmp*=C(sz[cho]+cnt-1,cnt);
ans+=tmp;
}
}
printf("%lld\n",ans);
}
int main(){
scanf("%d",&T);
while(T--) work();
return 0;
}
[CF1007B]Pave the Parallelepiped[组合计数+状态压缩]的更多相关文章
- 2021蓝桥杯省赛C++A组试题E 回路计数 状态压缩DP详细版
2021蓝桥杯省赛C++A组试题E 回路计数 状态压缩DP 题目描述 蓝桥学院由21栋教学楼组成,教学楼编号1到21.对于两栋教学楼a和b,当a和b互质时,a和b之间有一条走廊直接相连,两个方向皆可通 ...
- CF1007B Pave the Parallelepiped 容斥原理
Pave the Parallelepiped time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- HDU 4921 Map DFS+状态压缩+乘法计数
算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ3254Corn Fields(状态压缩DP入门)
题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...
- HDU4628+状态压缩DP
/* 状态压缩DP dp[ i ]:达到i状态的最小step. 题意:每次可以去掉一个回文串,求最少几步能取完. */ #include<stdio.h> #include<stri ...
- hdu4670(树上点分治+状态压缩)
树上路径的f(u,v)=路径上所有点的乘积. 树上每个点的权值都是由给定的k个素数组合而成的,如果f(u,v)是立方数,那么就说明f(u,v)是可行的方案. 问有多少种可行的方案. f(u,v)可是用 ...
- 状态压缩dp入门
poj1321 http://poj.org/problem?id=1321 我们可以把棋盘的每一行看做是一个状态,如果某一列放置了棋子,那么就标记为1,否则就标记为0.然后把它看成是一个二进制数,然 ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
随机推荐
- JavaScript动画:offset和匀速动画详解(含轮播图的实现)
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. offset简介 我们知道,三大家族包括:offset/scroll/c ...
- LeetCode题解之Binary Number with Alternating Bits
1.题目描述 2.问题分析 将数值转换为二进制,然后将前面的 0 去掉,再遍历一边二进制字符串,对每个字符和其后部的字符进行比较. 3.代码 bool hasAlternatingBits(int n ...
- 使用MonkeyTest对Android客户端进行压力测试
目录 monkey命令简介 monkey命令参数说明 自动化实例 如何通过日志定位问题 1.monkey命令简介 Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它 ...
- 03-01_WebLogic一些概念名词
WebLogic一些概念名词 域(Domain) 管理服务器(Administrative Server) 被管服务器(Managed Server,受管服务器) 集群(Cluster) 机器(Mac ...
- Will Georgia Tech's $7K online M.S. in computer science program make the grade?
https://newatlas.com/georgia-tech--graduate-computer-science-degree-mooc/28763/ Georgia Tech to offe ...
- PHPStorm 中配置 XDebug
1.下载 Xdebug ps : php版本和xdebug版本一定要相对应 如果不知道下载哪个版本,将phpinfo网页的源代码拷贝到https://xdebug.org/wizard.php,然后按 ...
- 数据库迁移之从oracle 到 MySQL最简单的方法
数据库迁移之从oracle 到 MySQL最简单的方法 因工作需要将oracle数据库换到MySQL数据库,数据量比较大,百万级别的数据,表也比较多,有没有一种既快捷又安全的方法呢?答案是肯定的,下面 ...
- centos httpd开启https服务并申请免费https
安装httpd yum -y install httpd httpd配置文件路径 /etc/httpd/conf/httpd.conf 安装OpenSSL yum install mod_ssl o ...
- Hash问题----Hash强碰撞
包含内容:hellowword,byeworld文件md5,pdf1,2的sha1值. 等待笔记...
- Spring boot结合mybatis开发的报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),经过排查确定是没有找到xml的原因 ...