Gym 100917M Matrix, The

题目链接: http://codeforces.com/gym/100917/problem/M

----------------------------------------------------------------------------

每次写$dp$都因为思路还不成熟就上了导致经常写出状态冗余的代码

这题看数据范围显然是状压$dp$

然后注意优雅地使用位运算来减少代码长度就好了

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 int ans[12], way[1 << 10];
 7 long long f[11][1 << 10][1 << 10];
 8 int n, a, b, q, cnt;
 9 long long dfs(int r, int sta, int mask)
10 {
11     long long &re = f[r][sta][mask];
12     if(re != -1)
13         return re;
14     if(r == n)
15         return re = (mask == (1 << n) - 1);
16     re = 0;
17     for(int i = 1; i <= cnt; ++i)
18         if(!((~sta) & way[i] & mask))
19             re += dfs(r + 1, way[i], way[i] | mask);
20     return re;
21 }
22 bool dfs2(long long x, int r, int sta, int mask)
23 {
24     if(r == n)
25         return 1;
26     for(int i = 1; i <= cnt; ++i)
27         if(!((~sta) & way[i] & mask))
28         {
29             if(x > f[r + 1][way[i]][way[i] | mask])
30                 x -= f[r + 1][way[i]][way[i] | mask];
31             else
32             {
33                 ans[r + 1] = way[i];
34                 return dfs2(x, r + 1, way[i], way[i] | mask);
35             }
36         }
37     return 0;
38 }
39 int calc1(int x)
40 {
41     int re = 0;
42     for(; x; x -= x & -x, ++re);
43     return re;
44 }
45 void output(int x)
46 {
47     for(int i = n - 1; i >= 0; --i)
48         printf("%d", (x & (1 << i)) != 0);
49     puts("");
50 }
51 int main()
52 {
53     memset(f, -1, sizeof f);
54     scanf("%d%d%d%d", &n, &a, &b, &q);
55     for(int i = 1; i < (1 << n); ++i)
56     {
57         int tmp = calc1(i);
58         if(tmp >= a && tmp <= b)
59             way[++cnt] = i;
60     }
61     long long x;
62     dfs(0, 0, 0);
63     while(q--)
64     {
65         scanf("%lld", &x);
66         if(dfs2(x, 0, 0, 0))
67         {
68             for(int i = 1; i <= n; ++i)
69                  output(ans[i]);
70         }
71         else
72             puts("No such matrix.");
73         puts("");
74     }
75     return 0;
76 }
时间: 2024-10-08 17:53:53

Gym 100917M Matrix, The的相关文章

Gym 101666K King of the Waves(dfs)

Gym 101666K King of the Waves Description You are organising a king of the hill tournament, the Buenos Aires Paddleboarding Competition (BAPC), with n participants. In a king of the hill tournament, one person starts as a "king" and is then chal

hdu 5015 233 Matrix (矩阵快速幂)

题意: 有一种矩阵,它的第一行是这样一些数:a  0,0 = 0, a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333... 除此之外,在这个矩阵里, 我们有 a i,j = a i-1,j +a i,j-1( i,j ≠ 0).现在给你 a 1,0,a 2,0,...,a n,0, 你能告诉我a n,m 是多少吗? n,m(n ≤ 10,m ≤ 10 9)输出 a n,m mod 10000007. 思路:首先我们观察n和m的取值范围,会发现n非常小而m却非常大,如果

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

【数组】Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路: 本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵.同理,我们也是逐个环

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难得的一次AC! 虽然感觉题

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

HDU 2686 Matrix(最大费用流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 1005 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer numbe

UVA 11992(Fast Matrix Operations-线段树区间加&amp;改)[Template:SegmentTree]

Fast Matrix Operations There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operati