Blocks(poj3734)组合数学
来源:互联网 发布:软件著作权 英文 编辑:程序博客网 时间:2024/03/28 23:28
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
212
Sample Output
26
如果没有限制,一共有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)
AC代码如下:
#include<iostream>using namespace std;const int M=10007;int pow(int a,int n,int mod){ int ans=1; while(n>0){ if(n&1) ans=(ans*a)%mod; a=(a*a)%mod; n>>=1; } return ans;}int main(){ int t; cin>>t; while(t--){ int n; cin>>n; int ans=pow(2,n-1,M); ans=(ans*ans)%M+ans; ans=ans%M; cout<<ans<<endl; } return 0;}
- Blocks(poj3734)组合数学
- POJ3734-Blocks
- poj3734:Blocks(EGF)
- POJ 3734 Blocks 组合数学
- poj3734--blocks(矩阵快速幂)
- POJ3734 Blocks(矩阵快速幂)
- poj3734 Blocks(指数生成函数)
- poj3734
- poj3734
- poj3734
- poj3734
- [POJ 3734] Blocks (矩阵快速幂、组合数学)
- [POJ 3734] Blocks (矩阵快速幂、组合数学)
- 组合数学
- 组合数学
- 组合数学
- 组合数学
- 组合数学
- sqlite no such table
- # JAVA中集合解析篇一 ArrayList解析
- diffusion simulation
- 最新行政区划代码省市区数据库(2016年7月1日 )
- Latex作者单位的写法—AND 首页脚注
- Blocks(poj3734)组合数学
- js的隐式转换问题
- swift中使用OC的方法;在OC中调用swift方法
- 贰:Modbus常用功能码协议详解
- ECMAScript中的真值和假值
- 占坑[概率dp] poj 2096 Collecting Bugs
- CAAnimation 类
- 模拟实现strcpy函数
- 如何让TinyMCE Advanced编辑器去除换行增加的P标签并获取内容值