徐州网络赛A-Hard To Prepare【dp】【位运算】【快速幂】
After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a Nogaku show during the feast. To enjoy the show, every audience has to wear a Nogaku mask, and seat around as a circle.
There are N guests Reimu serves. Kokoro has 2^k2k masks numbered from 0,1,\cdots,0,1,⋯, 2^k - 12k−1, and every guest wears one of the masks. The masks have dark power of Dark Nogaku, and to prevent guests from being hurt by the power, two guests seating aside must ensure that if their masks are numbered ii and jj , then ii XNOR jj must be positive. (two guests can wear the same mask). XNOR means ~(ii^jj) and every number has kk bits. (11 XNOR 1 = 11=1, 00 XNOR 0 = 10=1, 11 XNOR 0 = 00=0)
You may have seen 《A Summer Day's dream》, a doujin Animation of Touhou Project. Things go like the anime, Suika activated her ability, and the feast will loop for infinite times. This really troubles Reimu: to not make her customers feel bored, she must prepare enough numbers of different Nogaku scenes. Reimu find that each time the same guest will seat on the same seat, and She just have to prepare a new scene for a specific mask distribution. Two distribution plans are considered different, if any guest wears different masks.
In order to save faiths for Shrine, Reimu have to calculate that to make guests not bored, how many different Nogaku scenes does Reimu and Kokoro have to prepare. Due to the number may be too large, Reimu only want to get the answer modules 1e9+71e9+7 . Reimu did never attend Terakoya, so she doesn't know how to calculate in module. So Reimu wishes you to help her figure out the answer, and she promises that after you succeed she will give you a balloon as a gift.
Input
First line one number TT , the number of testcases; (T \le 20)(T≤20) .
Next TT lines each contains two numbers, NN and k(0<N, k \le 1e6)k(0<N,k≤1e6) .
Output
For each testcase output one line with a single number of scenes Reimu and Kokoro have to prepare, the answer modules 1e9+71e9+7 .
样例输入复制
2
3 1
4 2
样例输出复制
2
84
题目来源
题意:
给定n,k
有n个人围城圆,2^k个面具【从0开始编号】 要求相邻的人拿到的面具编号的同或值不为0
问方案数
思路:
只要任意一位相同同或值就不为0 即对于一个确定的i j如果是i的反码同或就会是0 只有这一种情况不能取
所以第一个人有m种情况 之后的n-2个人就有m-1种情况
到了第n-1个人的时候因为是一个圈 所以第n个人和第1个以及第n-1个都是相邻的
要分情况讨论
如果第1个和第n-1个是不同的 那么第n个人就m-2种可能 总的可能是 m*(m-1)^(n - 2) * (m - 2)
如果第1个和第n-1个是相同的 那么第n个人有m-1种可能 总的可能是 (m - 1) * (前n-2个人的可能)
两者相加即为答案
线性dp即可
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <cstring>
#include <set>
#include <map> using namespace std; typedef long long LL;
int t;
LL n, k;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
LL pow2[maxn], dp[maxn]; LL quick_pow(LL a, LL b)
{
LL res = ;
while (b) {
if (b & ) {
res = res * a % mod;
}
a = a * a % mod;
b >>= ;
}
return res % mod;
} void table()
{
for (int i = ; i < maxn; i++) {
pow2[i] = quick_pow(, i);
}
} int main()
{
//table();
cin >> t;
while (t--) {
memset(dp, , sizeof(dp));
scanf("%lld%lld", &n, &k);
LL m = quick_pow(2ll, k);
dp[] = m % mod;
dp[] = m * (m - ) % mod;
for (int i = ; i <= n; i++) {
dp[i] = (dp[i] + dp[i - ]) % mod;
dp[i] = (dp[i] + quick_pow(m - , i - ) % mod * m % mod * (m - ) % mod) % mod;
}
printf("%lld\n", dp[n]);
}
}
徐州网络赛A-Hard To Prepare【dp】【位运算】【快速幂】的更多相关文章
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- ICPC 2019 徐州网络赛
ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- [徐州网络赛]Longest subsequence
[徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...
- 基于DP+位运算的RMQ算法
来源:http://blog.csdn.net/y990041769/article/details/38405063 RMQ算法,是一个快速求区间最值的离线算法,预处理时间复杂度O(n*log(n) ...
- 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2745 Solved: 1694[Submit][Statu ...
- 【POJ2778】DNA Sequence 【AC自动机,dp,矩阵快速幂】
题意 题目给出m(m<=10)个仅仅由A,T,C,G组成的单词(单词长度不超过10),然后给出一个整数n(n<=2000000000),问你用这四个字母组成一个长度为n的长文本,有多少种组 ...
- 【BZOJ 2323】 2323: [ZJOI2011]细胞 (DP+矩阵乘法+快速幂*)
2323: [ZJOI2011]细胞 Description 2222年,人类在银河系外的某颗星球上发现了生命,并且携带了一个细胞回到了地球.经过反复研究,人类已经完全掌握了这类细胞的发展规律: 这种 ...
- [BZOJ1151][CTSC2007]动物园zoo 解题报告|DP|位运算
Description 最近一直在为了学习算法而做题,这道题是初一小神犇让我看的.感觉挺不错于是写了写. 这道题如果是一条线的话我们可以构造一个DP f[i,j]表示以i为起点,i,i+1...i+4 ...
- CF1151F Sonya and Informatics(概率期望,DP,矩阵快速幂)
明明是水题结果没切掉……降智了…… 首先令 $c$ 为序列中 $0$ 的个数,那么排序后序列肯定是前面 $c$ 个 $0$,后面 $n-c$ 个 $1$. 那么就能上 DP 了.(居然卡在这里……) ...
随机推荐
- 教你如何架设linux邮件服务器postfix
检查linux是否有安装postfix和dovecot 检查命令如下: Rpm -qa |grep postfix; Rpm –qa |grep dovecot; 如果没有显示任何数据,表明没有安 ...
- ScreenPointToRay - 近视口到屏幕的射线
正如题目所说,ScreenPointToRay可以计算从Camera的近视口nearClip向前发射一条射线到屏幕上的点的坐标. 函数原型为: public Ray ScreenPointToRay( ...
- glsl 多重纹理
#include"glsl.h" void SHADER::drawBox() { glBegin(GL_QUADS); // Front Face glNormal3f( 0.0 ...
- Sharepoint 2013 Workflow Error
问题: (1)提示“reload the page and then start the workflow”错误 (2)提示“Unable to properly communicate with t ...
- 做BS开发,你应该知道的一些东西
界面和用户体验(Interface and User Experience) 知道各大浏览器执行Web标准的情况,保证你的站点在主要浏览器上都能正常运行.你至少要测试以下引擎:Gecko(用于Fire ...
- OpenCV学习:Mat结构中的数据共享机制
使用Mat类,内存管理变得简单,不再像使用IplImage那样需要自己申请和释放内存. Mat是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩 ...
- 百度地图api ak值
http://api.map.baidu.com/geocoder/v2/?ak=859d16285fd000feec89e9032513f8bb&callback=renderReverse ...
- Appium - WebView測试(Android)
Appium - WebView測试 作者: Max.Bai 时间: 2015/07 Appium - WebView測试(Android) 如今App都是混合型的.有原生的也包括WebView的,a ...
- mybatis由浅入深day01_8.2resultMap
8.2 resultMap mybatis中使用resultMap完成高级输出结果映射. resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可 ...
- day22<IO流+>
IO流(序列流) IO流(序列流整合多个) IO流(内存输出流) IO流(内存输出流之黑马面试题) IO流(对象操作流ObjecOutputStream) IO流(对象操作流ObjectInputSt ...