计蒜客 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 样例输入: ...
随机推荐
- IOS UILineBreakMode的各种情况分析
typedef enum { UILineBreakModeWordWrap = 0, UILineBreakModeCharacterWrap, UILineBreakModeCl ...
- TIMEOUT HANDLING WITH HTTPCLIENT
https://www.thomaslevesque.com/2018/02/25/better-timeout-handling-with-httpclient/ The problem If yo ...
- Tomcat------如何查看80端口是否被占用
1.Window + R,打开“运行”窗口,输入cmd 2.输入netstat -nao,回车,一般来说80端口会被PID为4的程序占用 3.启动任务管理器,点击“查看”->“选择列” 4.勾选 ...
- Aspose------导入Excel
代码: public List<T> ImportExcelToList<T>() { HttpContext context = HttpContext.Current; ) ...
- Java单例模式的应用
单例模式用于保证在程序的运行期间某个类有且仅有一个实例.其优势在于尽可能解决系统资源.通过修改构造方法的访问权限就可以实现单例模式. 代码如下: public class Emperor { priv ...
- 7 -- Spring的基本用法 -- 12... Spring 3.0 提供的表达式语言(SpEL)
7.12 Spring 3.0 提供的表达式语言(SpEL) Spring表达式语言(简称SpEL)是一种与JSP 2 的EL功能类似的表达式语言,它可以在运行时查询和操作对象图.支持方法调用和基本字 ...
- ios开发之--NSNumber的使用
什么是NSNumber? NSArray/NSDictionary中只能存放oc对象,不能存放基本数据类型,如果想把基本数据类型放进去,需要先把基本数据类型转换成OC对象, 代码如下: ; ; flo ...
- iOS 将Excel导入到SQLite3的过程
1.打开Excel表格,另存为.csv文件 2.打开SQLite3,选择File -> Import -> other... 3.在弹出的文件选择框中选择步骤1保存的.cvs文件 4在弹出 ...
- Netty权威指南之NIO通信模型
NIO简介:与Socket和ServerSocket类相对应,NIO提供了SocketChannel和ServerSocketChannel两种不同的套接字通道实现,这两种新通道都支持阻塞和非阻塞两种 ...
- Oracle之表空间基于时间点的恢复
记一次优化过程中:一次误操作,在不影响其他表空间的情况下:采用表空间基于时间点的恢复(TSPITR)方法恢复数据的过程. 1.TSPITR恢复原理 TSPITR目前最方便的方法是使用RMAN进行 ...