A. Hard to prepare

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^k 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 .

题目链接:

https://www.jisuanke.com/contest/1557?view=challenges

题意:

n个数字,每个数字范围\([0, 2^k-1]\),问有多少种不同的序列满足对于所有相邻的两个数字,它们异或值不能为\(2^k-1\),其中第一个数字和最后一个数字也算相邻

思路:

很容易想到,第1个数有\(2^k\)种选择,第2个数到第n-1个数都有\(2^k-1\)种选择,第n个数有\(2^k-2\)种选择

所以答案就是\(2^k*(2^k-2)*(2^k-1)^{n-2}\)

但是这样会出现漏算:在第1个数和第n-1个数相同的情况下,第n个数有\(2^k-1\)种选择, 而并非\(2^k-2\)种

然后仔细分析可以发现,漏算的情况你可以把第1个数和第n-1个数当成同一个数,这样序列长度就变成n-2了,问题相同,只是数据规模变小,递归解决即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int t;
const ll mod = 1e9 + 7;
ll n, k;
ll base;
ll solve(ll x)
{
if (x == 2)
{
return base * (base - 1) % mod;
} else if (x == 1)
{
return base;
}
ll res = base * powmod(base - 1ll, x - 2, mod) % mod * max(base - 2ll, 0ll) % mod ;
res += solve(x - 2) % mod ;
res %= mod;
return res;
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
scanf("%d", &t);
while (t--)
{
scanf("%lld %lld", &n, &k);
base = powmod(2ll, k, mod);
printf("%lld\n", solve(n) );
}
return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

最新文章

  1. iOS开发小技巧--iOS键盘 inputView 和 inputAccessoryView
  2. jquery 判断时间
  3. ASP.NET中 WebForm 窗体控件使用及总结【转】
  4. [JAVA]在linux中设置JDK环境,ZendStudio,Eclipse
  5. Mysql query log
  6. MySQLdb
  7. 第三章TP-Link 703N OpenWrt设置网络
  8. (高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1-----&gt;10进制-----&gt;ba2)
  9. Yii 通过composer 安装的方法
  10. 字符设备驱动4: ioremap
  11. Petroglyph访问:中间件游戏
  12. springmvc(四) springmvc的数据校验的实现
  13. jQuery中的常用内容总结(三)
  14. BZOJ4361 isn 树状数组、DP、容斥
  15. 传入mybatis的xml为Long型时报There is no getter for property named &#39;VARCHAR&#39; in
  16. Individual Project复审
  17. SG 大法(Sprague-Grundy函数)
  18. 传输层——TCP报文头介绍
  19. 【DB2】SQL优化
  20. Lamda表达式的参数捕获,太酷了

热门文章

  1. Flutter 一些常用第三方库、插件
  2. 安装Windows和Ubuntu双系统
  3. 封装transform函数(设置和获取transform的属性和属性值)
  4. MySQL(四)InnoDB中一棵B+树能存多少行数据
  5. PHP,Excel导出换行
  6. js里typeof和instanceof和箭头表达式要注意的地方,以及其他
  7. juc-locks包
  8. ubuntu16.04 下Mongo数据库搭建
  9. java当中JDBC当中请给出一个sql server的dataSource的helloworld例子
  10. Zuul【基础配置】