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-2586 How far away ?(lca+bfs+dfs+线段树)
题目链接: How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 《java编程思想》读后笔记:二,吸血鬼数字
书本p75中一道读后练习思考题,题目如下: 吸血鬼数字是指位数为偶数的数字,可以有一对数字相乘得到,而这对数字各包含成绩的一半位数的数字,其中从最初的数字中选取的数字可以任意排序.一两个0结尾的数字是 ...
- 关于禁用bootstrap响应式解决方法
前几天接到一个任务,用bootstrap框架写几个静态页面,由于长时间专注于后台开发,所以,花费了3天时间,才终于写完了页面,其中,来回的修改,把遇到的问题说一下. 用bootstarp做完页面后,组 ...
- django 模板中通过变量替代key取字典内容
模板中通过变量替代key取字典内容 templatetags/├── get_item.py├── __init__.py ###get_item.py # coding=utf-8 from dja ...
- java反射中Class对象详解和类名.class, class.forName(), getClass()区别
1.获得类型类 可以看到,对象a是A的一个实例,A是某一个类,在if语句中使用a.getClass()返回的结果正是类A的类型类,在Java中表示一个特定类型的类型类可以用“类型.class”的方式获 ...
- 「NOIP2006」「LuoguP1064」 金明的预算方案(分组背包
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NNN元钱就行” ...
- 【LeetCode】017. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 媒体格式分析之flv -- 基于FFMPEG
本来是应该先写一个媒体文件格式的简单讲解的,还没来得及写,以后再写.今天就先根据ffmpeg的flv.c的flv_demux这个结构体来讲解一下当前比较流行的媒体格式flv. FLV 是FLASH V ...
- Synchronized之二:synchronized的实现原理
Java提供了synchronized关键字来支持内在锁.Synchronized关键字可以放在方法的前面.对象的前面.类的前面. 当线程调用同步方法时,它自动获得这个方法所在对象的内在锁,并且方法返 ...
- Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...