计蒜客 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 样例输入: ...
随机推荐
- Track and Follow an Object----4
原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ ntroduction: 在本示例中,我们将探索包含Kinect摄像头的自主行为. 这 ...
- Linux+Redis实战教程_day03_1、Redis-LinkedList【重点】
1.redis-LinkedList[重点] Java List : 数组ArrayList 链表LinkedList 为什么redis选取了链表? Redis操作中,最多的操作是进行元素的增删 使用 ...
- iOS NSError
写在前面 在iOS开发中,NSError的使用非常常见,使用也比较简单,也正因为简单,所以对这一部分知识不甚注重.但是近期在做app底层网络封装时发现了一些问题.我使用的网络框架是AFNetworki ...
- Android开发-- 简单对话框
final Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.appicns_folder_sma ...
- debug-stripped.ap_' specified for property 'resourceFile' does not exist
1.关闭 Instant Run 2. 关闭混淆(混淆的问题) buildTypes { release { minifyEnabled true shrinkResources true progu ...
- Handler基本用法
片断一:mHandler = new Handler();mRunnable = new Runnable() { @Override public void run() { currentPosit ...
- Mac下Intellij IDea发布JavaWeb项目 详解二 (新建Module)
Step3 添加两个module 3.1 右键[WebWorkSpace]-[New]-[Module] 3.2 重复 准备工作1:新建第一个JavaWeb项目[1.6-1.11]的操作,建好一个名为 ...
- Java 实现文件批量重命名亲测可用(精简版)
package com.cmge.utils; import java.io.*; import java.util.*; import java.util.Map.Entry; import com ...
- cxGrid使用汇总4
1. CxGrid汇总功能 ① OptionsView-Footer设置为True,显示页脚 ② CxGrid的Summary选项卡定义要汇总的列和字段名及汇总方式,Footer选项卡定义 ...
- 怎样更改SQL Server 2008的身份验证方式
大家都知道sql server 有两种登录验证方式,即sql server验证方式和windows验证方式,但是sql server默认的是windows登录验证方式,我们如何启用sql server ...