2018 徐州 icpc 网络赛 A 递推or数学公式

探讨一场无限循环的盛宴中,如何通过不同Nogaku场景的排列组合防止观众感到厌倦的问题。需要计算特定条件下可能的场景数量,并提供两种算法实现方案。

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 徐州赛区网络预赛

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
const int p=1e9+7; 
using namespace std;
typedef long long LL;  
int n,m,k;
LL quick_mod(LL a,LL b)
{
	LL ans=1;
	a%=p;
	while(b)
	  {
	  	if(b&1) {ans=ans*a%p;b--;}
	  	b>>=1;a=a*a%p;
	  }
	return ans;
}
int main()
{
	int T_T,cas=0;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&m);
		LL A=quick_mod(2,m);
		LL ans=A*quick_mod(A-1,n-1)%p,x=A-1,y=1,z=0,t1,t2;
		for(int i=2;i<=n-2;i++)
		{
			t1=(x-y+p)%p;
			t2=(x-z+p)%p;
			y=t2;z=t1;
		  	x=x*(A-1)%p;
		} 
		if(n>2)
		{
		ans=(ans-A*(x-y)%p)%p;
		ans=(ans+p)%p;
	    }//特判
		printf("%lld\n",ans);
	}
}

 

另外一种O(t*log(n))的方法:用m种颜色着色圆的n个扇形的方法总数

偷了一波其它队的代码(wyj,zkq,lzk)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;

ll qpow(ll a, ll n) {
	ll res = 1;
	while (n)
	{
		if (n & 1)
			res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res % mod;
}

int main() {
	int T;
	scanf("%d", &T);
	ll n, k;
	while (T--)
	{
		ll ans = 0;
		scanf("%lld%lld", &n, &k);
		ll num = qpow(2, k);
		ans = qpow(num - 1, n) + (num * qpow(2, mod - 2) % mod - 1) % mod * (1 + qpow(-1, n)) % mod + 1;
		ans %= mod;
		//printf("%lf\n", pow(num - 1, n) + 1 + (num / 2 - 1)*(1 + pow(-1, n)));
		printf("%lld\n", (ans + mod) % mod);
	}
}

 

转载于:https://www.cnblogs.com/The-Pines-of-Star/p/9878819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值