计蒜客 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 样例输入: ...
随机推荐
- iphone弹出窗口效果的制作(Core animation, CALayer)
效果类似人人网微薄客户端的弹出效果 static CGFloat kTransitionDuration = 0.3; - (void)initView { UIWindow *window = [U ...
- PMP用语集
AC actual cost 实际成本 ACWP actual cost of work performed 已完工作实际成本 BAC budget at completion 完工预算 BCWP b ...
- phonegap入门–1 Android 开发环境搭建
一.JDK 安装JDK,安装包中包含了JDK和JRE两部分,建议将它们安装在同一个盘符下面. 配置环境变量: 1.右键点击我的电脑,选择属性,点击高级选项卡,选择环境变量. 2.找到Path变量名(无 ...
- 使用dshow抓取摄像头数据时,回调函数时间为0的问题
在使用dshow抓取摄像头数据,调用dshow的回调函数,如果发现SampleTime一直为0,如下图 那极有可能是使用RenderStream函数连接Filter时,指定的第一个参数为 PIN_CA ...
- NFS 简介
一.NFS 简介 1. NFS(Network File System)网络文件系统,它允许网络中的计算机之间通过 TCP/IP 网络共享资源2. NFS 应用场景:A,B,C 三台机器上需要保证被访 ...
- gradle 两种更新方法
第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper.properties 替换 distributionUrl= ...
- Struts2(二)工作原理
一.概述 1.struts框架本身分为三个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 2.struts2工作的基本流程: 客户端初始化一个指 ...
- Hibernate系列之核心开发接口
一.概述 所有的hibernate应用中都会访问5个核心接口,它们分别是: Configuration:配置hibernate,创建SessionFactory对象 SessionFactory:初始 ...
- JBuilder+struts一个常见异常
[org.apache.commons.digester.Digester]-[ERROR] Parse Error at line 3 column 22: The content of eleme ...
- JWNL的配置使用
JWNL是什么? JWNL is an API for accessing WordNet-style relational dictionaries. It also provides functi ...