题目链接: 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的更多相关文章

  1. [codeforces gym Matrix God]随机矩阵乘法

    题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...

  2. Gym - 101341I:Matrix God(随机算法)

    题意:给出N,以及三个矩阵A,B,C,大小都为N*N.问是否满足A*B=C: N<1000: 思路:由于矩阵乘法的复杂度为O(N^3):而部分验证又不能保证结果正确.我们巧妙地利用矩阵乘法的结合 ...

  3. Binary Strings Gym - 101161G 矩阵快速幂 + 打表

    http://codeforces.com/gym/101161/attachments 这题通过打表,可以知道长度是i的时候的合法方案数. 然后得到f[1] = 2, f[2] = 3, f[3] ...

  4. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

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

  6. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  7. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  8. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

  9. [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 ...

随机推荐

  1. lambda表达式(2)

    转:http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html "Lambda表达式"是一个匿名函数,是一种高效 ...

  2. vue项目 多文件上传并显示在页面上

    <template> <label for="file" class=" btn btn-default" style="borde ...

  3. PHP的设计模式及场景应用介绍

    有大量的文章解释什么是设计模式,如何实现设计模式,网络上不需要再写一篇这样的文章.相反,在本文中我们更多的讨论什么时候用和为什么要用,而不是用哪一个和如何使用. 我将会为这些设计模式描绘不同的场景和案 ...

  4. hdu-4292.food(类dining网络流建图)

    Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Mysql8- Public Key Retrieval is not allowed

    在使用 MySQL 8.0 时重启应用后提示 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public ...

  6. C#判断页面中的多个文本框输入值是否有重复的实现方法

    List<string> list = new List<string>();//首先定义一个泛型数组 //这里假如说有四个文本框 string mainseat = this ...

  7. vue 一个轮播的组件

    当我们进行开发的时候,并不是说所有信息都会在写一个组件中 作为项目的老大,我们要去思考每个文件下面需要放什么业务,分的越细.越合理为好 我们在componts文件下新建一个Banner.vue 组件 ...

  8. Vue+elementui 实现复杂表头和动态增加列的二维表格

    先上完成的效果图:列是根据查询结果增加的 数据格式: 表头的数据取出: data.data.forEach(element => { this.thead.push({ 品名: element. ...

  9. 从1到n整数中1出现的次数(整数中1出现的次数)

    题目 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.AC ...

  10. touch 创建空文件或改变文件的时间戳属性

    1.命令功能 touch 改变文件时间属性或创建空文件. 2.语法格式 touch  [option]  file touch 选项 文件名 3. 选项参数说明 参数 参数说明 -a 仅改变文件的访问 ...