徐州网络赛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 了.(居然卡在这里……) ...
随机推荐
- C struct的内存对齐
说明:如果你看到了这篇,请略过. struct是复合类型. 其中的成员在内存中的分布都是对齐的. 这个对齐的意思是,struct的sizeof运算结果必定是其最大类型长度的整数倍. --注意,如果st ...
- C++ 类的头文件、实现、使用
再次吐槽下C++Primer这本书,啰哩啰嗦,废话太多.如果我来翻译的话,绝对删减一堆没用的---仅限于发牢骚. 不知道是否经典的做法 类中的成员声明在头文件中,定义(我更喜欢叫实现)在源文件中,使用 ...
- 第二百八十八节,MySQL数据库-索引、limit分页、执行计划、慢日志查询
MySQL数据库-索引.limit分页.执行计划.慢日志查询 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获 ...
- 详解JNDI的lookup资源引用 java:/comp/env
ENC的概念: The application component environment is referred to as the ENC, the enterprise naming c ...
- chrome 版本升级到56,报错Your connection is not private NET::ERR_CERT_WEAK_SIGNATURE_ALGORITHM
ubuntu14.04 解决方法: $ sudo apt-get install libnss3-1d ref: http://stackoverflow.com/questions/42085055 ...
- input文本框与图片的对齐
<input name="demo" style="width:100px;vertical-align:top" /> <img src=& ...
- oracle数据备份
from:http://www.docin.com/p-728428621.html
- Python 解压缩Zip和Rar文件到指定目录
#__author__ = 'Joker'# -*- coding:utf-8 -*-import urllibimport osimport os.pathimport zipfilefrom zi ...
- SVN常用命令与分支操作
1.基本操作 1.0 创建版本库: Svnadmin create /data/repos 2.0 修改配置文件 Auth文件 [groups] admin=shguo [/] @ad ...
- linux--GCC简单用法
gcc是linux下最常用的一款c编译器,对应于CPP 有相应的g++工具,debug有gdb,只是还不会用. 个人感觉gcc确实是个好东西,完全可以直接在gedit下编程然后写个shell脚本用gc ...