D. Blocks 数学题
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 integerN(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 https://www.bnuoj.com/bnuoj/contest_show.php?cid=8507#problem/101988 一开始打表找规律,无果。
然后分析一下,开始的时候主要怕是2个红,然后绿色的可以4个,6个....这样分类很麻烦。
于是就转换思路。
枚举红色 + 绿色一共有多少个
枚举值就是0、2、4、6.....
我也会想到这样枚举会超时,但是先写公式出来,看看能不能化简,
每一次C(n, k)中,就是红色 + 绿色一共有k个,其中再枚举红色的个数,剩下的就会是绿色的个数了。
C(k, 0) + C(k, 2) + C(k, 4) .... 这个是标准的二项式系数奇次项的和。是2^(k - 1)
然后枚举红色 + 绿色一共有k个后,剩下的n - k个每个有两种选择,2^(n - k),于是一合并,就是2^(n - 1)
提取
2^(n - 1)
然后公式就出来了。 2^n + 2^(n - 1) * (2^(n - 1) - 1)
后面那个减1是因为没有2^0
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> int n;
LL ans = ;
int f[];
const int MOD = ;
void dfs(int cur) {
if (cur == n + ) {
int one = , tow = ;
for (int i = ; i <= n; ++i) {
one += f[i] == ;
tow += f[i] == ;
}
if (one % == && tow % == ) ans++;
// ans %= MOD;
return;
}
for (int i = ; i <= ; ++i) {
f[cur] = i;
dfs(cur + );
}
}
int quick_pow(int a, int b) {
int ans = ;
int base = a;
while (b) {
if (b & ) {
ans *= base;
if (ans >= MOD) ans %= MOD;
// ans %= MOD;
}
b >>= ;
base *= base;
if (base >= MOD) base %= MOD;
// base %= MOD;
}
return ans;
}
void work() {
// for (n = 1; n <=14; ++n) {
// ans = 0;
// dfs(1);
//// cout << ans % 10007 << endl;
// printf("n == %d %I64d\n", n, ans);
// }
int n;
scanf("%d", &n);
int ans = (quick_pow(, n) + (quick_pow(, n - ) * ((quick_pow(, n - ) + MOD - ) % MOD)) % MOD ) % MOD;
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
cin >> t;
while (t--) work();
return ;
}
D. Blocks 数学题的更多相关文章
- 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM
刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 开发该选择Blocks还是Delegates
前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...
- poj 1390 Blocks
poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...
- Java 同步代码块 - Synchronized Blocks
java锁实现原理: http://blog.csdn.net/endlu/article/details/51249156 The synchronized keyword can be used ...
- 区块 Blocks
Structure / Blocks / Demonstrate block regions
- 使用Code::blocks在windows下写网络程序
使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...
- Code::Blocks配置GTK+2和GTK+3
Code::Blocks配置GTK+2和GTK+3 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根 ...
- [翻译]理解Ruby中的blocks,Procs和lambda
原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...
随机推荐
- hdu-5802 Windows 10(贪心)
题目链接: Windows 10 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- bzoj 2626: JZPFAR k-D树
题目大意: 平面上n个点,每次给出一个点,求这个点的k远点 题解: 什么叫做k远点呢... 1 2 3 4 5中5是第一远,4是第二远... 看来我语文学的不好 那么我们直接上k-D Tree求k邻近 ...
- 描述怎样通过flask+redis+sqlalchemy等工具,开发restful api
flask开发restful api系列(8)-再谈项目结构 摘要: 进一步介绍flask的项目结构,使整个项目结构一目了然.阅读全文 posted @ 2016-06-06 13:54 月儿弯弯02 ...
- Java多线程加强
一.传统多线程 public void start() Causes this thread to begin execution; the Java Virtual Machine calls th ...
- 乱写的一个SQL框架
闲来没事,看了mybatis的实现形式,就心血来潮的自己弄了一个仿照mybatis的框架,性能肯定不好,而且有很多问题,但是是一次有益的尝试 1.基本配置文件 <!--加载数据源--> & ...
- AndroidStudio启动时不自动打开项目
取消勾选Reopen last project on startup选项 点击 OK 就行了
- 转载:数据库应用开发工具Toad使用笔记
由于网上TOAD中文教程很少,在网上摘抄了此文章便于学习,感谢原创者. TOAD使用笔记 1.把鼠标停在sql所在行,然后ctrl+Enter直接执行当前sql. 2.解决Toad对中文显示乱码问题( ...
- submatrix
given a 2-d matrix with 0 or 1 values largest square of all 1's dynamic programming, dp[i][j] = 1 + ...
- Linux之vim常用扩展操作
多窗口编辑 批量注释和自定义注释 显示行号 1.多窗口编辑 2.批量注释和自定义注释 3.显示行号(临时生效) 命令行模式下输入: set nu 显示行号 set nonu 不显示行号
- [poj]1050 To the Max dp
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...