ACM-ICPC 2018 徐州赛区网络预赛 A. Hard to prepare(递推)

 A. Hard to prepare 题库链接

  •  262144K

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个人坐成一圈,每个人都戴着一个面具,面具有0-2^k-1种编号,每相邻的两个人的面具的编号的二进制表示中必须存在至少一位相同,问总共有多少种排列方法。

思路:

我们可以把这个圈从某一处裁开,使之变成一条线,令长度为n的直线上编号两两之间有相同的二进制位的排列方法为line(n),编号两两之间有相同的二进制位但是首位的二进制表示完全不同,记为linedf(n)。则当圆的周长为n时的答案就是

line(n)-linedf(n)

再来推导line(n),由于只需要与前一个人的面具的编号不是完全不相同就可以了。因此

line(n)=line(n-1)*(2^k-1)

再来推导linedf(n),由于linedf(n)指的是第n个人和第一个人完全不相同的情况,因此,第n-1位不能与第一位相同,那么linedf(n)=line(n-1)-第n-1位与第一位完全相同的情况数。不难发现,第一位与x位完全相同就能使1到x-1连成一个环,因此第n-1位与第一位完全相同的情况数就是line(n-2)-linedf(n-2)。所以:

linedf(n)=line(n-1)-(line(n-2)-linedf(n-2));

有了这些式子,这个题目就解决了

//circle(n) = line(n) - lineDf(n);
//line(n) = line(n - 1) * (P2 - 1);
//lineDf(n) = line(n - 1) - line(n - 2) + lineDf(n - 2);
//
//line(1) = P2;
//lineDf(1) = lineDf(2) = 0;
#include <bits/stdc++.h>

typedef long long LL;

const LL MOD = 1e9 + 7;
const LL MAXN = 1e6 + 5;

inline LL quickPowMod(LL a, LL b)
{
    LL sum = 1;
    a = a % MOD;

    while (b) {
        if (b % 2)
            sum = 1ll * sum * a % MOD;
        b /= 2;
        a = 1ll * a * a % MOD;
    }
    return sum;
}

inline LL solve(LL n, LL k)
{
//	if (k == 1) return 2;
	LL p2 = quickPowMod(2, k);
	
	static LL line[MAXN], lineDf[MAXN];
	line[1] = line[0] = p2;
	for (LL i = 2; i <= n; i++)
	{
		line[i] = 1ll * line[i - 1] * (p2 + MOD - 1) % MOD;
		lineDf[i] = (1ll * line[i - 1] - line[i - 2] + lineDf[i - 2]) % MOD;
//		std::cout << line[i] << "---" << lineDf[i] << std::endl;
	}
	return (line[n] + MOD - lineDf[n]) % MOD;
}

int main()
{
	LL T;
	scanf("%d", &T);
	while (T--)
	{
		LL n, k;
		scanf("%lld%lld", &n, &k);
		printf("%lld\n", solve(n, k));
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值