ACM-ICPC 2018 徐州赛区网络预赛 - 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 2k2^k2k masks numbered from 0,1,⋯,0,1,\cdots,0,1,⋯, 2k−12^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 iii and jjj , then iii XNOR jjj must be positive. (two guests can wear the same mask). XNOR means ~(iii^jjj) and every number has kkk bits. (111 XNOR 1=11 = 11=1, 000 XNOR 0=10 = 10=1, 111 XNOR 0=00 = 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+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 TTT , the number of testcases; (T≤20)(T \le 20)(T≤20) .

Next TTT lines each contains two numbers, NNN and k(0<N,k≤1e6)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+7 .

样例输入复制

2
3 1
4 2

样例输出复制

2
84

题目来源

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

思路:题目意思,给你一个环让(环上有n个人),你在0到2^k-1这个范围内挑选数,使得任意相连的两个数字同或值为正,经过简单分析发现,第一个人有2^k种选法,第二个人有2^k-1种选法,第三个人有2^k-1种选法......但是最后一个人有2^k-2种选法,因为它和两个人相连。其实可以转换为着色问题。

所以有递推公式:

但是要注意如果n为奇数时,会少2^k种情况。

然后的话,递推和快速幂都能写。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long int ll;
ll mod=1e9+7,n,m=1,k,dp[1000005];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		m=1;
		scanf("%lld%lld",&n,&k);
		for(int i=1;i<=k;i++){
			m=m*2%mod;
		}
		dp[1]=0;
		dp[2]=m*(m-1)%mod;
		for(int i=3;i<=n;i++){
			dp[i]=((m-2)*dp[i-1]%mod+(m-1)*dp[i-2]%mod)%mod;
		}
		if(n&1) printf("%lld\n",(dp[n]+m)%mod);
		else printf("%lld\n",dp[n]);
	}
}

猜你喜欢

转载自blog.csdn.net/DaDaguai001/article/details/83756286