问题描述
  我们把一个数称为有趣的,当且仅当:
  1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次。
  2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前。
  3. 最高位数字不为0。
  因此,符合我们定义的最小的有趣的数是2013。除此以外,4位的有趣的数还有两个:2031和2301。
  请计算恰好有n位的有趣的数的个数。由于答案可能非常大,只需要输出答案除以1000000007的余数。
输入格式
  输入只有一行,包括恰好一个正整数n (4 ≤ n ≤ 1000)。
输出格式
  输出只有一行,包括恰好n 位的整数中有趣的数的个数除以1000000007的余数。
样例输入
4
样例输出
3
 
析:先说第一种方法,数位DP:
首先只有0123,并且有约束条件,那么就只有6种状态,{2},{20},{201},{23},{203},{2031},然后依次对每一位进行计算即可。
第二种,状压DP:
因为只有4种数字,所以可能用状压DP,1表示0, 2表示1, 4表示2,8表示3.剩下的就简单了。
第三种,组合数学+暴力枚举:
先枚举01的数量,那么最少2个,最多n-2个,由于0是不能放在首位,所以就有c(n-1, i),i 表示有多少个01,然后再考虑有多种方法,
应该有 n-i 种方法,因为0的数量是从1个到 i-1 个,同理,23就有 n-i-1 种,然后乘起来就好。
第四种,推公式:
这个方法和第三种一样的,只不过是把第三种简化了,然后成一个递推公式,an=(n*n-5n+4)*2^(n-3)+n-1。
第五种,矩阵快速幂:
对每一个组成数字进行分析,字符串必定以2开头,把字符串中字符放入一个集合,我们可以统计长度为N,字符集合为S的合法字符串个数,
然后把它们的关系表示成一个矩阵,就OK了。
 
代码如下:
数位DP:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline LL Max(LL a, LL b){ return a < b ? b : a; }
inline LL Min(LL a, LL b){ return a > b ? b : a; }
inline int Max(int a, int b){ return a < b ? b : a; }
inline int Min(int a, int b){ return a > b ? b : a; }
LL dp[maxn][5]; int main(){
while(cin >> n){
dp[4][0] = 7; dp[4][1] = 5; dp[4][2] = 3; dp[4][3] = 9; dp[4][4] = 3;
for(int i = 5; i <= n; ++i){
dp[i][0] = (1 + 2 * dp[i-1][0]) % mod;
dp[i][1] = (dp[i-1][0] + 2 * dp[i-1][1]) % mod;
dp[i][2] = (1 + dp[i-1][2]) % mod;
dp[i][3] = (dp[i-1][0] + dp[i-1][2] + 2 * dp[i-1][3]) % mod;
dp[i][4] = (dp[i-1][1] + dp[i-1][3] + 2 * dp[i-1][4]) % mod;
}
cout << dp[n][4] << endl;
}
}

状压DP:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL dp[maxn][(1<<4)+1]; int main(){
while(cin >> n){
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 0; i < n; ++i){
for(int j = 0; j < 16; ++j){
if(!dp[i][j]) continue;
if(!(j & 2) && i) dp[i+1][j|1] = (dp[i+1][j|1] + dp[i][j]) % mod;
dp[i+1][j|2] = (dp[i+1][j|2] + dp[i][j]) % mod;
if(!(j & 8)) dp[i+1][j|4] = (dp[i+1][j|4] + dp[i][j]) % mod;
dp[i+1][j|8] = (dp[i+1][j|8] + dp[i][j]) % mod;
}
}
cout << dp[n][15] << endl;
}
return 0;
}

组合数学+暴力枚举:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL c[maxn][maxn];
void init(){
for(int i = 0; i < n; ++i) c[i][0] = c[i][i] = 1;
for(int i = 2; i < n; ++i)
for(int j = 1; j < i; ++j)
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod;
} int main(){
while(cin >> n){
init();
LL ans = 0;
for(int i = 2; i < n-1; ++i)
ans = (ans + (c[n-1][i] * (i-1) * (n-i-1)) % mod) % mod;
cout << ans << endl;
}
return 0;
}

推公式:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL quick_pow(LL a, LL b){
LL ans = 1;
while(b){
if(b & 1) ans = (ans * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ans;
} int main(){
while(cin >> n){
LL ans = (n * n - 5 * n + 4) % mod;
ans = (ans * quick_pow(2LL, n-3) + n - 1) % mod;
cout << ans << endl;
}
return 0;
}

矩阵快速幂:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Matrix{
LL a[6][6];
}; Matrix mul(Matrix x, Matrix y){
Matrix ans;
memset(ans.a, 0, sizeof ans.a);
for(int i = 0; i < 6; ++i){
for(int j = 0; j < 6; ++j){
for(int k = 0; k < 6; ++k){
ans.a[i][j] += (x.a[i][k] * y.a[k][j]) % mod;
}
ans.a[i][j] %= mod;
}
}
return ans;
} Matrix quick_pow(Matrix x, int b){
Matrix ans;
memset(ans.a, 0, sizeof ans.a);
for(int i = 0; i < 6; ++i)
ans.a[i][i] = 1;
while(b){
if(b & 1) ans = mul(ans, x);
b >>= 1;
x = mul(x, x);
}
return ans;
} int main(){
Matrix x, y;
memset(x.a, 0, sizeof x.a);
memset(y.a, 0, sizeof y.a);
x.a[3][3] = x.a[0][0] = x.a[1][0] = x.a[2][1] = x.a[3][0] = x.a[4][1] = x.a[4][3] = x.a[5][2] = x.a[5][4] = x.a[5][5] = 1;
x.a[1][1] = x.a[4][4] = x.a[5][5] = x.a[2][2] = 2;
y.a[0][0] = 1;
while(cin >> n){
Matrix ans = mul(quick_pow(x, n-1), y);
cout << ans.a[5][0] << endl;
}
return 0;
}

CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)的更多相关文章

  1. BZOJ_2734_[HNOI2012]集合选数_构造+状压DP

    BZOJ_2734_[HNOI2012]集合选数_构造+状压DP 题意:<集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x ...

  2. 【BZOJ】1076 [SCOI2008]奖励关 期望DP+状压DP

    [题意]n种宝物,k关游戏,每关游戏给出一种宝物,可捡可不捡.每种宝物有一个价值(有负数).每个宝物有前提宝物列表,必须在前面的关卡取得列表宝物才能捡起这个宝物,求期望收益.k<=100,n&l ...

  3. hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)

    传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...

  4. HDU 1565&1569 方格取数系列(状压DP或者最大流)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. [转]状态压缩dp(状压dp)

    状态压缩动态规划(简称状压dp)是另一类非常典型的动态规划,通常使用在NP问题的小规模求解中,虽然是指数级别的复杂度,但速度比搜索快,其思想非常值得借鉴. 为了更好的理解状压dp,首先介绍位运算相关的 ...

  6. 状态压缩dp 状压dp 详解

    说到状压dp,一般和二进制少不了关系(还常和博弈论结合起来考,这个坑我挖了还没填qwq),二进制是个好东西啊,所以二进制的各种运算是前置知识,不了解的话走下面链接进百度百科 https://baike ...

  7. 洛谷 P3343 - [ZJOI2015]地震后的幻想乡(朴素状压 DP/状压 DP+微积分)

    题面传送门 鸽子 tzc 竟然来补题解了,奇迹奇迹( 神仙题 %%%%%%%%%%%% 解法 1: 首先一件很明显的事情是这个最小值可以通过类似 Kruskal 求最小生成树的方法求得.我们将所有边按 ...

  8. 51nod 1673 树有几多愁(链表维护树形DP+状压DP)

    题意 lyk有一棵树,它想给这棵树重标号. 重标号后,这棵树的所有叶子节点的值为它到根的路径上的编号最小的点的编号. 这棵树的烦恼值为所有叶子节点的值的乘积. lyk想让这棵树的烦恼值最大,你只需输出 ...

  9. BZOJ3836 [Poi2014]Tourism 【树形dp +状压dp】

    题目链接 BZOJ3836 题解 显然这是个\(NP\)完全问题,此题的解决全仗任意两点间不存在节点数超过10的简单路径的性质 这意味着什么呢? \(dfs\)树深度不超过\(10\) \(10\)很 ...

随机推荐

  1. Android 源代码自动编译packages/apps

    /*************************************************************************** * Android 源代码自动编译packag ...

  2. HDU 5319 Painter (模拟)

    题意: 一个画家画出一张,有3种颜色的笔,R.G.B.R看成'\',B看成'/',G看成这两种的重叠(即叉形).给的是一个矩阵,矩阵中只有4种符号,除了3种颜色还有'.',代表没有涂色.问最小耗费多少 ...

  3. vim 常用命令总结

    https://www.ibm.com/developerworks/cn/linux/l-cn-tip-vim/ 1 文档开头末尾 [[ 开头 ]]末尾 2 删除全部 :%d 3 剪切.复制.粘贴 ...

  4. 在Ubuntu中用root帐号登录

    一.其实我个人认为这没有多大必要,因为当你需要 root 的权限时,使用 sudo 便可以了.如果你实在需要在 Ubuntu 中启用 root 帐号的话,那么不妨执行下面的操作: 1.重新设置 roo ...

  5. DOS攻击和DDOS攻击有啥区别啊

    DDOS是DOS攻击中的一种方法. DoS:是Denial of Service的简称,即拒绝服务,不是DOS操作系统,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务. ...

  6. Code Hard or Go Home

    介绍Webkit的渊源  http://hypercritical.co/2013/04/12/code-hard-or-go-home

  7. 迅影QQ视频查看v2.0 源码

    骗了1200多位朋友,实在惭愧,现在公开我自己的源码实现.本人新人,代码很烂,请凑合看吧O(∩_∩)O~ Form1.cs using System; using System.Text.Regula ...

  8. OpenGl从零开始之坐标变换(下)

    这节主要来理解投影变换和视口变换的使用. 1.正射投影:glOrtho 函数原型: void glOrtho(GLdouble left,GLdouble right,GLdouble bottom, ...

  9. Linker scripts之MEMORY

    1 MEMORY command The MEMORY command describes the location and size of blocks of memory in the targe ...

  10. 使用Log.isLoggable方法

    在Audio Debug过程中想打开AudioService.java文件中的log,比如想打开setmode这段log: if (DEBUG_MODE) { Log.v(TAG, "set ...