Gym 100917M Matrix, The
题目链接: http://codeforces.com/gym/100917/problem/M
----------------------------------------------------------------------------
每次写$dp$都因为思路还不成熟就上了导致经常写出状态冗余的代码
这题看数据范围显然是状压$dp$
然后注意优雅地使用位运算来减少代码长度就好了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int ans[], way[ << ];
long long f[][ << ][ << ];
int n, a, b, q, cnt;
long long dfs(int r, int sta, int mask)
{
long long &re = f[r][sta][mask];
if(re != -)
return re;
if(r == n)
return re = (mask == ( << n) - );
re = ;
for(int i = ; i <= cnt; ++i)
if(!((~sta) & way[i] & mask))
re += dfs(r + , way[i], way[i] | mask);
return re;
}
bool dfs2(long long x, int r, int sta, int mask)
{
if(r == n)
return ;
for(int i = ; i <= cnt; ++i)
if(!((~sta) & way[i] & mask))
{
if(x > f[r + ][way[i]][way[i] | mask])
x -= f[r + ][way[i]][way[i] | mask];
else
{
ans[r + ] = way[i];
return dfs2(x, r + , way[i], way[i] | mask);
}
}
return ;
}
int calc1(int x)
{
int re = ;
for(; x; x -= x & -x, ++re);
return re;
}
void output(int x)
{
for(int i = n - ; i >= ; --i)
printf("%d", (x & ( << i)) != );
puts("");
}
int main()
{
memset(f, -, sizeof f);
scanf("%d%d%d%d", &n, &a, &b, &q);
for(int i = ; i < ( << n); ++i)
{
int tmp = calc1(i);
if(tmp >= a && tmp <= b)
way[++cnt] = i;
}
long long x;
dfs(, , );
while(q--)
{
scanf("%lld", &x);
if(dfs2(x, , , ))
{
for(int i = ; i <= n; ++i)
output(ans[i]);
}
else
puts("No such matrix.");
puts("");
}
return ;
}
Gym 100917M Matrix, The的更多相关文章
- [codeforces gym Matrix God]随机矩阵乘法
题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...
- Gym - 101341I:Matrix God(随机算法)
题意:给出N,以及三个矩阵A,B,C,大小都为N*N.问是否满足A*B=C: N<1000: 思路:由于矩阵乘法的复杂度为O(N^3):而部分验证又不能保证结果正确.我们巧妙地利用矩阵乘法的结合 ...
- Binary Strings Gym - 101161G 矩阵快速幂 + 打表
http://codeforces.com/gym/101161/attachments 这题通过打表,可以知道长度是i的时候的合法方案数. 然后得到f[1] = 2, f[2] = 3, f[3] ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- Value Iteration Algorithm for MDP
Value-Iteration Algorithm: For each iteration k+1: a. calculate the optimal state-value function for ...
- MVC Html.AntiForgeryToken(); 防止跨站伪造请求(建议所有表单提交都加这个)
视图页面from表单中添加 @Html.AntiForgeryToken(); 然后每个表单提交的时候都会带__RequestVerificationToken 字段 后端控制器验证时添加 [Val ...
- Python学习-第三天-面向对象编程基础
Python学习-第三天-面向对象编程基础 类和对象 简单的说,类是对象的蓝图和模板,而对象是类的实例.这个解释虽然有点像用概念在解释概念,但是从这句话我们至少可以看出,类是抽象的概念,而对象是具体的 ...
- [常用类]StringBuffer 类,以及 StringBuilder 类
线程安全,可变的字符序列. 字符串缓冲区就像一个String ,但可以修改. 在任何时间点,它包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容. 字符串缓冲区可以安全地被多个线程使 ...
- VirtualStringTree常用类和属性
重要的类:TBaseVirtualTree = class(TCustomControl)TCustomVirtualStringTree = class(TBaseVirtualTree)TVirt ...
- LOJ 3175. 「IOI2019」排列鞋子
传送门 考虑如果能确定每个鞋子最终交换到的位置,那么答案容易算出 具体地,如果原位置为 $i$ 的鞋子要交换到 $pos[i]$ 那么最终答案就是 $pos$ 的逆序对数量 如果不懂可以先去写 NOI ...
- Gradle打包问题Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0
前言 使用gradle打包react native的时候,出现了如下报错,下面和大家说一下解决的具体办法 Deprecated Gradle features were used in this bu ...
- java SSM框架 代码生成器 快速开发平台 websocket即时通讯 shiro redis
A代码编辑器,在线模版编辑,仿开发工具编辑器,pdf在线预览,文件转换编码 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器 freemaker模版技术 , ...
- jQuery的加法运算,val()获取的结果相加变成了字符串连接。
加法运算 ?想必大家听到这都会不屑了,加法运算这是多么简单的运算.然而有的时候在jQuery也让人挺头疼的. 常规的是: var num1 = 123; var num2=123; var total ...
- windwos下nginx 配置https并http强制跳转https
windwos下nginx 配置https并http强制跳转https 一.首先配置证书文件 申请证书文件,这里就不做详细过程了,直接看证书文件结果. 这是两个证书的关键文件 打开ngxin下con ...