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

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

题意:

给定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】【位运算】【快速幂】的更多相关文章

  1. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  2. ICPC 2019 徐州网络赛

    ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...

  3. 计蒜客 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 ...

  4. [徐州网络赛]Longest subsequence

    [徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...

  5. 基于DP+位运算的RMQ算法

    来源:http://blog.csdn.net/y990041769/article/details/38405063 RMQ算法,是一个快速求区间最值的离线算法,预处理时间复杂度O(n*log(n) ...

  6. 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2745  Solved: 1694[Submit][Statu ...

  7. 【POJ2778】DNA Sequence 【AC自动机,dp,矩阵快速幂】

    题意 题目给出m(m<=10)个仅仅由A,T,C,G组成的单词(单词长度不超过10),然后给出一个整数n(n<=2000000000),问你用这四个字母组成一个长度为n的长文本,有多少种组 ...

  8. 【BZOJ 2323】 2323: [ZJOI2011]细胞 (DP+矩阵乘法+快速幂*)

    2323: [ZJOI2011]细胞 Description 2222年,人类在银河系外的某颗星球上发现了生命,并且携带了一个细胞回到了地球.经过反复研究,人类已经完全掌握了这类细胞的发展规律: 这种 ...

  9. [BZOJ1151][CTSC2007]动物园zoo 解题报告|DP|位运算

    Description 最近一直在为了学习算法而做题,这道题是初一小神犇让我看的.感觉挺不错于是写了写. 这道题如果是一条线的话我们可以构造一个DP f[i,j]表示以i为起点,i,i+1...i+4 ...

  10. CF1151F Sonya and Informatics(概率期望,DP,矩阵快速幂)

    明明是水题结果没切掉……降智了…… 首先令 $c$ 为序列中 $0$ 的个数,那么排序后序列肯定是前面 $c$ 个 $0$,后面 $n-c$ 个 $1$. 那么就能上 DP 了.(居然卡在这里……) ...

随机推荐

  1. map正序、逆序排序

    一.按 key 排序 1.map顺序排序(小的在前,大的在后): map<float,string,less<float> > m_aSort;//已float从小到大排序 2 ...

  2. PNG透明兼容IE6的几种方法(转)

    png 透明针对 IE6 一直是件挺麻烦的事情,使用的方法也是各有不同,大多的原理是用 IE 的滤镜来解决的. 语法: filter:progid:DXImageTransform.Microsoft ...

  3. asp.net 后端验证

    using EntryRegistration.Filters; using EntryRegistration.Models.Entity; using System; using System.C ...

  4. php一些常规动态设置与获取

    error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE);ini ...

  5. xpath的数据和节点类型以及XPath中节点匹配的基本方法

    XPath数据类型  XPath可分为四种数据类型:  节点集(node-set)  节点集是通过路径匹配返回的符合条件的一组节点的集合.其它类型的数据不能转换为节点集.  布尔值(boolean)  ...

  6. C#委托和事件定义和使用

    委托 定义委托的语法和定义方法比较相似,只是比方法多了一个关键字delegate ,我们都知道方法就是将类型参数化,所谓的类型参数化就是说该方法接受一个参数,而该参数是某种类型的参数,比如int.st ...

  7. 第四章 Spring.Net 如何管理您的类___对象的手动装配

    前面我们知道了什么是对象,什么是对象工厂,什么是应用程序上下文.这一次我们来看一下对象的装配. Spring.Net 中有多种装配对象的方式,装配这个词可能比较学术化,我们可以理解为对象的创建. Sp ...

  8. 处理unsigned相减错误(BIGINT UNSIGNED value is out of range)

    mysql 当两个字段想减时,如果其中一个或两个字段的类型的unsigned无签名类型,如果想减的值小于0则会报错(BIGINT UNSIGNED value is out of range) 测试: ...

  9. /etc/rc.d/rc.local

    /etc/rc.d/rc.local 用于用户自定义开机启动程序,可以往里写开机要执行的命令或脚本,线上的配置如下: [root@localhost ~]$ cat /etc/rc.d/rc.loca ...

  10. Serlvet学习笔记之一 ——实现servlet的3种方法

    1.配置环境,从tomcat的lib下面引入servlet-api.jar包.