徐州网络赛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 了.(居然卡在这里……) ...
随机推荐
- am335x hid-multitouch.c
am335x在使用电容屏,需要加载hid-multitouch.ko模块 由下面文件生成 kernel/drivers/hid/hid-multitouch.c 内核中编译模块 make module ...
- 有一个TIME的类要求输出分和秒的值
#include <iostream> /* run this program using the console pauser or add your own getch, system ...
- unity之Mathf类
说明:Mathf类主要提供数学计算的函数与常量,包含了所有数学计算时需要用到的函数.所以掌握Mathf类里面的成员变量和成员函数是必要的. 使用Mathf:通常的如果使用一个类中的成员变量或者成员函数 ...
- 关于MySQL的几个命令之load
1.记录当前操作命令
- Android学习笔记——Menu(一)
背景: Android3.0(API level 11)开始,Android设备不再需要专门的菜单键. 随着这种变化,Android app应该取消对传统6项菜单的依赖.取而代之的是提供anction ...
- 二:Java之异常处理
一.异常的概念 异常,也就是非正常情况. 其实.异常本质上是程序上的错误,包含程序逻辑错误和系统错误. 错误在我们编敲代码的过程中会常常发生,包含编译期间和执行期间的错误,在编译期间出现的错误有编译器 ...
- 使用keytool生成密钥对
1.首先要用KeyTool工具来生成私匙库:(-alias别名 –validity 3650表示10年有效) keytool -genkey -alias privatekey -keystore p ...
- MFC-TCP连接代码片段(支援大富的)
BOOL CClientSocketTestDlg::OnInitDialog() { CDialogEx::OnInitDialog(); ........................ // T ...
- 第五篇:浅谈CPU 并行编程和 GPU 并行编程的区别
前言 CPU 的并行编程技术,也是高性能计算中的热点,也是今后要努力学习的方向.那么它和 GPU 并行编程有何区别呢? 本文将做出详细的对比,分析各自的特点,为将来深入学习 CPU 并行编程技术打下铺 ...
- PHP防止跨站表单提交与同站跨页伪造表单的攻击
在以前的防止跨站攻击的时候,使用了验证提交的页面是否是同一个站点,这样可以防止普通的攻击,ereg("blog.qita.in",$_SERVER['HTTP_REFERER']) ...