[POJ 3734] Blocks (矩阵高速幂、组合数学)
2016-02-29 16:48
106 查看
Blocks
Description
Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some
myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.
Input
The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.
Output
For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.
Sample Input
Sample Output
Source
PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Simon
题目大意:
有一排砖。数量为N。现要将砖所有染上色。有红、蓝、绿、黄四种颜色。
要求被染成红色和绿色的砖块数量必须为偶数,问一共同拥有多少种染色方案。(因为答案较大。模10007)
解题思路:
这道题有两种思路,一个是採用dp的思想,然后转化为矩阵。
还有一个是组合数学的思想。
dp:
用dp
[4]来表示N块砖块的染色情况,一共同拥有四种状态。
1. dp
[0] :表示N块中红色绿色的数量均为偶数。
2. dp
[1] :表示N块中红色为偶数,绿色为奇数。
3. dp
[2] :表示N块中红色为奇数,绿色为偶数。
4. dp
[3]
:表示N块中红色绿色的数量均为奇数。
而状态转移方程为:
dp[N+1][0] = 2 * dp
[0] + 1 * dp
[1] + 1 * dp
[2] + 0 * dp
[3]
dp[N+1][1] = 1 * dp
[0] + 2 * dp
[1] + 0 * dp
[2] + 1 * dp
[3]
dp[N+1][2] = 1 * dp
[0] + 0 * dp
[1]
+ 2 * dp
[2] + 1 * dp
[3]
dp[N+1][3]
= 0 * dp
[0] + 1 * dp
[1] + 1 * dp
[2] + 2 * dp
[3]
上述的转移方程能够转化为矩阵:
|2 1 1 0|
|1 2 0 1|
|1 0 2 1|
|0 1 1 2|
代码:
组合数学:
假设没有限制,一共同拥有4 ^ n 次。如今考虑有 k 块被染为红色或绿色,且在k块中。一定有红色或绿色或两者均为奇数的情况。将这些情况减去,即是想要的答案。(1<= k <= n)
从n块中选择k块。为c(n, k)。 而从k块中选择不符合的情况染色,须要对k进行奇偶讨论。
假设k为奇数。红色和绿色的数量为一奇一偶:2 * (c(k, 1) + c(k, 3) + c(k, 5) +……)* c(n, k) * 2^(n - k) (当中要乘以2,是由于能够分别选择红、绿色为奇数)
假设k为偶数,红色和绿色的数量所有为奇数:(c(k, 1) + c(k,
3) + c(k, 5) +……)* c(n, k) * 2^(n - k) (这里不须要乘以2)
而 c(k,
1) + c(k, 3) + c(k, 5) +…… = 2^(k - 1)
所以,最后的表达式为:
4^n - 2^n*c(n, 1) - 2^(n - 1)*c(n, 2) - 2^n*c(n, 3) - 2^(n-1)*c(n, 4)-…… = 4^n - 2^n*2^(n-1) - 2^(n-1)*(2^(n-1)-1) = 4^(n-1) + 2^(n-1)
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3997 | Accepted: 1775 |
Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some
myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.
Input
The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.
Output
For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.
Sample Input
2 1 2
Sample Output
2 6
Source
PKU Campus 2009 (POJ Monthly Contest – 2009.05.17), Simon
题目大意:
有一排砖。数量为N。现要将砖所有染上色。有红、蓝、绿、黄四种颜色。
要求被染成红色和绿色的砖块数量必须为偶数,问一共同拥有多少种染色方案。(因为答案较大。模10007)
解题思路:
这道题有两种思路,一个是採用dp的思想,然后转化为矩阵。
还有一个是组合数学的思想。
dp:
用dp
[4]来表示N块砖块的染色情况,一共同拥有四种状态。
1. dp
[0] :表示N块中红色绿色的数量均为偶数。
2. dp
[1] :表示N块中红色为偶数,绿色为奇数。
3. dp
[2] :表示N块中红色为奇数,绿色为偶数。
4. dp
[3]
:表示N块中红色绿色的数量均为奇数。
而状态转移方程为:
dp[N+1][0] = 2 * dp
[0] + 1 * dp
[1] + 1 * dp
[2] + 0 * dp
[3]
dp[N+1][1] = 1 * dp
[0] + 2 * dp
[1] + 0 * dp
[2] + 1 * dp
[3]
dp[N+1][2] = 1 * dp
[0] + 0 * dp
[1]
+ 2 * dp
[2] + 1 * dp
[3]
dp[N+1][3]
= 0 * dp
[0] + 1 * dp
[1] + 1 * dp
[2] + 2 * dp
[3]
上述的转移方程能够转化为矩阵:
|2 1 1 0|
|1 2 0 1|
|1 0 2 1|
|0 1 1 2|
代码:
/* ID: wuqi9395@126.com PROG: LANG: C++ */ #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<fstream> #include<cstring> #include<ctype.h> #include<iostream> #include<algorithm> #define INF (1<<30) #define PI acos(-1.0) #define mem(a, b) memset(a, b, sizeof(a)) #define For(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; const int maxn = 5; const int maxm = 5; const int mod = 10007; struct Matrix { int n, m; ll a[maxn][maxm]; void clear() { n = m = 0; memset(a, 0, sizeof(a)); } Matrix operator * (const Matrix &b) const { Matrix tmp; tmp.clear(); tmp.n = n; tmp.m = b.m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (!a[i][j]) continue; //稀疏矩阵乘法优化 for (int k = 0; k < b.m; k++) { tmp.a[i][k] += a[i][j] * b.a[j][k]; tmp.a[i][k] %= mod; } } return tmp; } }; int n; Matrix Matrix_pow(Matrix A, int k) { Matrix res; res.clear(); res.n = res.m = 4; for (int i = 0; i < 4; i++) res.a[i][i] = 1; while(k) { if (k & 1) res = res * A; k >>= 1; A = A * A; } return res; } int main () { int t; scanf("%d", &t); Matrix A; A.clear(); A.n = A.m = 4; A.a[0][0] = 2; A.a[0][1] = 1; A.a[0][2] = 1; A.a[0][3] = 0; A.a[1][0] = 1; A.a[1][1] = 2; A.a[1][2] = 0; A.a[1][3] = 1; A.a[2][0] = 1; A.a[2][1] = 0; A.a[2][2] = 2; A.a[2][3] = 1; A.a[3][0] = 0; A.a[3][1] = 1; A.a[3][2] = 1; A.a[3][3] = 2; while(t--) { scanf("%d", &n); Matrix res = Matrix_pow(A, n); printf("%d\n", res.a[0][0]); } return 0; }
组合数学:
假设没有限制,一共同拥有4 ^ n 次。如今考虑有 k 块被染为红色或绿色,且在k块中。一定有红色或绿色或两者均为奇数的情况。将这些情况减去,即是想要的答案。(1<= k <= n)
从n块中选择k块。为c(n, k)。 而从k块中选择不符合的情况染色,须要对k进行奇偶讨论。
假设k为奇数。红色和绿色的数量为一奇一偶:2 * (c(k, 1) + c(k, 3) + c(k, 5) +……)* c(n, k) * 2^(n - k) (当中要乘以2,是由于能够分别选择红、绿色为奇数)
假设k为偶数,红色和绿色的数量所有为奇数:(c(k, 1) + c(k,
3) + c(k, 5) +……)* c(n, k) * 2^(n - k) (这里不须要乘以2)
而 c(k,
1) + c(k, 3) + c(k, 5) +…… = 2^(k - 1)
所以,最后的表达式为:
4^n - 2^n*c(n, 1) - 2^(n - 1)*c(n, 2) - 2^n*c(n, 3) - 2^(n-1)*c(n, 4)-…… = 4^n - 2^n*2^(n-1) - 2^(n-1)*(2^(n-1)-1) = 4^(n-1) + 2^(n-1)
代码:
/* ID: wuqi9395@126.com PROG: LANG: C++ */ #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<fstream> #include<cstring> #include<ctype.h> #include<iostream> #include<algorithm> #define INF (1<<30) #define PI acos(-1.0) #define mem(a, b) memset(a, b, sizeof(a)) #define For(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; const int mod = 10007; int multi_pow(int a, int k, int mod) { int ans = 1; while(k) { if (k & 1) ans = (ans * a) % mod; k >>= 1; a = (a * a) % mod; } return ans; } int main () { int t, n; scanf("%d", &t); while(t--) { scanf("%d", &n); int ans = multi_pow(2, n - 1, mod); ans = (ans * ans) % mod + ans; printf("%d\n", ans % mod); } return 0; }
相关文章推荐
- Jenkins 六: 构建中执行shell或者 windows的批处理程序
- bzoj 1057: [ZJOI2007]棋盘制作 单调栈
- cad2013
- 关于oracle数据库误删表空间文件后无法登陆sqlplus的解决方法
- 日期类的使用方法
- 基于PetShop的数据可视化网站(五):关于登录的理解
- fresco 导致的无法加载 so 问题(Android studio)
- java 浅堆和深堆
- 通过Python利用saltstack进行生成服务器资产清单
- 自定义控件用属性实现selector效果
- 2007:Part Number System of a USA networking company
- 通过Python利用saltstack进行生成服务器资产清单
- osg 自己写地面操作器
- 二进制补码(好文章)
- JSP_004_Cookie介绍03—Cookie的domain(域)
- dephi WaitForMultipleObjects 用法
- js刷新页面不回到顶部
- ZooKeeper学习之-Zookeeper简单介绍(一)
- Android系统对话框
- python 中关于struct处理二进制数据