计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]
题目链接:https://nanti.jisuanke.com/t/31453
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, 2^k - 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 $i$ and $j$ , then $i$ XNOR $j$ must be positive. (two guests can wear the same mask). XNOR means ~($i$^$j$) and every number has $k$ bits. ($1$ XNOR $1$ = 1, $0$ XNOR $0$ = $1$, $1$ XNOR $0$ = $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+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 $T$ , the number of test cases; $(T \le 20)$.
Next $T$ lines each contains two numbers, $N$ and $k(0<N, k \le 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$.
题意:
有 $n$ 个人围成一圈,并且有 $2^k$ 个数($0$ 到 $2^k - 1$),每个人可以选择一个数(可以选择一样的数),
要求:假设任意相邻的两个人的数字为 $i$ 和 $j$,必须满足 $i$ XNOR $j > 0$(XNOR代表同或),
请给出这 $n$ 个人挑选数字的方案数(答案 $\bmod 1e9 + 7$)。
题解:
首先所有数字都为正,所以按位同或的结果必然为非负的,那么考虑同或结果不是正数的——同或等于 $0$,
我们知道按位同或,只要有一位一样(都为 $0$ 或者都为 $1$),就不会等于零,所以对于任意一个数 $i$,有且仅有一个数 $j = \sim i$ 使得 $i$ XNOR $j = 0$($\sim$ 代表按位取反)。
记$m = 2^k$,那么,显然第 $1$ 个人有 $m$ 种选择,第 $2$ 个人到第 $n-1$ 个人都有 $m-1$ 种选择,第 $n$ 个人只有 $m-2$ 种选择,此时方案数为 $m\left( {m - 1} \right)^{n - 2} \left( {m - 2} \right)$;
但显然有漏算,因为第 $n$ 个人只有 $m-2$ 种选择是他自以为只有 $m-2$ 种选择,而实际上存在一些个情况使得他可以有 $m-1$ 种选择,那什么时候可以比原来多一个选择呢?
显然,就是当第 $1$ 个人和第 $n-1$ 个人选择了相同的数字的时候,第 $n$ 个人可以多一个选择,而第 $n$ 个人多一个选择,就相应的多了一个方案。
换句话说,第 $1$ 个人和第 $n-1$ 个人选择了相同的数字的情况有多少种,就再加上去几个方案。
那么第 $1$ 个人和第 $n-1$ 个人选择了相同的数字的情况有多少种呢?
由于第 $1$ 个人和第 $n-1$ 个人永远选择相同的数字,可以把这两个左右端点等效成一个点,
那么就从第 $1$ 个人到第 $n-1$ 个人的链型问题变成了第 $1$ 个人到第 $n-2$ 个人的一个环形问题了,
也就是要求,$n-2$ 个人围成一圈,每个人从 $m$ 个数字里选择一个,满足相邻数字同或为正的条件下,有多少种选择方案。
这一看,不就是题目的要求吗,无非是从 $n$ 个人变成了 $n-2$ 个人罢了,所以,我们可以用递归的方式求解。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; const ll MOD=1e9+;
ll fpow(ll a,ll b)
{
ll r=,base=a%MOD;
while(b)
{
if(b&) r*=base,r%=MOD;
base*=base;
base%=MOD;
b>>=;
}
return r;
} int n;
ll m,k; ll f(int n)
{
if(n==) return m % MOD;
if(n==) return m * (m-) % MOD;
return ( m * fpow(m-,n-) % MOD * (m-) % MOD + f(n-) % MOD ) % MOD;
} int main()
{
int T;
for(cin>>T;T;T--)
{
cin>>n>>k;
m=fpow(,k);
cout<<f(n)<<endl;
}
}
计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]的更多相关文章
- 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]
题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...
- 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]
题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...
- 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...
- 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]
题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...
- 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]
题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...
- 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]
题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...
- 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]
题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...
- 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...
- 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]
题目链接:https://nanti.jisuanke.com/t/30994 样例输入: 5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4 样例输出: 55 样例输入: ...
随机推荐
- c++Valgrind内存检测工具---19
原创博文,转载请标明出处--周学伟 http://www.cnblogs.com/zxouxuewei/ 一.Valgrind 概述 Valgrind是一套Linux下,开放源代码(GPL V2)的 ...
- SPREAD for Windows Forms 下箭头追加行
''' <summary> ''' 下矢印の動作クラス ''' </summary> ''' <remarks></remarks> Public Cl ...
- POJ1159——Palindrome
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53647 Accepted: 18522 Desc ...
- Linux下profile与bashrc的区别
/etc/profile./etc/bashrc.~/.bash_profile.~/.bashrc很容易混淆,他们之间有什么区别?它们的作用到底是什么?/etc/profile: 用来设置系统环境参 ...
- iOS protocbuf安装使用
protobuf文件地址:https://github.com/google/protobuf 1.问题/usr/local.bak/lib /usr/local.bak/man /usr/local ...
- 如何编写一个SQL注入工具
0x01 前言 一直在思考如何编写一个自动化注入工具,这款工具不用太复杂,但是可以用最简单.最直接的方式来获取数据库信息,根据自定义构造的payload来绕过防护,这样子就可以. 0x02 SQL注 ...
- Perl socket编程
In this article, let us discuss how to write Perl socket programming using the inbuilt socket module ...
- 第二十篇:不为客户连接创建子进程的并发回射服务器(poll实现)
前言 在上文中,我使用select函数实现了不为客户连接创建子进程的并发回射服务器( 点此进入 ).但其中有个细节确实有点麻烦,那就是还得设置一个client数组用来标记select监听描述符集中被设 ...
- Kafka manager安装 (支持0.10以后版本consumer)
下载地址: https://pan.baidu.com/s/1jIE3YL4 步骤: 1. 解压kafka-manager-1.3.2.1.zip 2. cd kafka-manager-1.3.2 ...
- N76E003之ISP
Flash存储器支持硬件编程和应用编程(IAP).如果产品在研发阶段或产品需要更新软固件时,硬件编程就显得不太方便,采用在系统编程(ISP)方式,可使这一过程变得方便.执行ISP不需要将控制器从系统板 ...