我是靠谱客的博主 忧心银耳汤,最近开发中收集的这篇文章主要介绍2018ACM-ICPC徐州赛区网络赛: A. Hard to prepare(递推) A. Hard to prepare,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

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

 

题意:

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

 

思路:

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

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

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

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

递归即可

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
LL Pow(LL a, LL b)
{
	LL ans = 1;
	while(b)
	{
		if(b%2)
			ans = ans*a%mod;
		a = a*a%mod;
		b /= 2;
	}
	return ans;
}
LL er[1000005] = {1}, ans[1000005];
LL Gao(int n, int m)
{
	LL ans;
	if(n==2)
		return er[m]*(er[m]-1)%mod;
	if(n==1)
		return er[m];
	ans = (er[m]*Pow(er[m]-1, n-2)%mod*max(er[m]-2, 0ll)%mod+Gao(n-2, m))%mod;
	return ans;
}
int main(void)
{
	int T, n, m, i;
	for(i=1;i<=1000002;i++)
		er[i] = er[i-1]*2%mod;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &m);
		printf("%lldn", Gao(n, m));
	}
	return 0;
}

 

最后

以上就是忧心银耳汤为你收集整理的2018ACM-ICPC徐州赛区网络赛: A. Hard to prepare(递推) A. Hard to prepare的全部内容,希望文章能够帮你解决2018ACM-ICPC徐州赛区网络赛: A. Hard to prepare(递推) A. Hard to prepare所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部