题目链接: 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. STL hash function的模板特化

    哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...

  2. repr_str方法

    该方法可以改变字符串的显示格式 class School: def __init__(self,name,addr,type): self.name = name self.addr = addr s ...

  3. [19/05/05-星期日] JDBC(Java DataBase Connectivity,java数据库连接)_mysql基本知识

    一.概念 (1).是一种开放源代码的关系型数据库管理系统(RDBMS,Relational Database Management System):目前有很多大公司(新浪.京东.阿里)使用: (2). ...

  4. [2019杭电多校第七场][hdu6656]Kejin Player

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意为从i级花费a元有p的概率升到i+1级,有1-p的概率降到x级(x<i),查询从L级升 ...

  5. jQuery 的attr()与css()的区别

    attr是attribute的缩写,意思是标签属性. css是,样式,意思是元素的style样式的 我的理解是: attr是操作元素的   属性 css是操作元素的  style属性 前者可以修改&l ...

  6. 数据挖掘 workfolw 总结

    个人将数据挖掘的流程简单表示为“ 数据 → 特征 → 模型 ”.   首先,明确问题的性质和任务(分类.回归.聚类.推荐.排序.关联分析.异常检测等): 其次,理解数据(含义.类型.值的范围),并通过 ...

  7. VirtualStringTree常用类和属性

    重要的类:TBaseVirtualTree = class(TCustomControl)TCustomVirtualStringTree = class(TBaseVirtualTree)TVirt ...

  8. 21、前端知识点--html5和css3新特性汇总

    跳转到该链接 新特性汇总版: https://www.cnblogs.com/donve/p/10697745.html HTML5和CSS3的新特性(浓缩好记版) https://blog.csdn ...

  9. 360CTF Re wp

    这比赛唯一的一道Re

  10. openSSH学习笔记(一)

    OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现.SSH协议族可以用来进行远程控制, 或在计算机之间传送文件.而实现此功能的传统方式,如telnet(终端仿真协议). rc ...