牛客挑战赛58

比赛地址

牛客挑战赛58

给定 \(n, m\) ,求有多少对长度为 \(n\) 的序列 \(a, b\) 合法。
两个序列 \(a, b\) 合法被定义为:
\(a_{1}\left|a_{2}\right| \cdots \mid a_{n} \geqslant b_{1} \oplus b_{2} \oplus \cdots b_{n}, \forall i, a_{i}, b_{i} \in\left[0,2^{m}\right)\)
其中 | 表示按位或, \(\oplus\) 表示按位异或。

输入描述:

两个以空格分隔的正整数 \(n, m\).

输出描述:

一个非负整数, 表示答案对 \(10^{9}+7\) 取模后的值。

示例1

输入

3 3

输出

233472

备注:

保证 \(n,m\leqslant 10^6\).

解题思路

思维

由高位到低位考虑不同位的位置:
假设不同位在第 \(i\) 位,即要求左边第 \(i\) 位为 \(1\),右边为 \(0\),左边为 \(1\) 则有 \(2^n-1\) 种方案,右边为 \(0\) 可以先随便填 \(n-1\),最后一个可以决定这位为 \(0\),即有 \(2^{n-1}\) 种方案,\(i-1\) 位前面的数可以随便填,即对于 \(i-1\) 后面的数有 \(2^{2n}\) 种方案,\(i\) 前面的数要求一样,同理有 \(2^{2n-1}\) 种方案,故左边大于右边有 \((2^n-1)\times 2^{n-1}\times 2^{2n} \times 2^{2n-1}\),而左右相等有 \((2^{2n-1})^m\) 种方案

  • 时间复杂度:\(O(logn+mlogm)\)

代码

// %%%Skyqwq
#include <bits/stdc++.h>
 
#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int mod=1e9+7;
int n,m;
int ksm(int a,int b,int p)
{
	int res=1;
	for(;b;b>>=1)
	{
		if(b&1)res=1ll*res*a%p;
		a=1ll*a*a%p;
	}
	return res;
}
signed main()
{
    cin>>n>>m;
    int t=ksm(2,2*n-1,mod);
    int res=ksm(t,m,mod);
    int tt=ksm(2,n-1,mod)*(ksm(2,n,mod)-1+mod)%mod;
    int ttt=ksm(2,2*n,mod);
    for(int i=1;i<=m;i++)
    	res=(res+ksm(t,m-i,mod)*tt%mod*ksm(ttt,i-1,mod))%mod;
    cout<<res;
    return 0;
}
posted @ 2022-03-19 12:04  zyy2001  阅读(44)  评论(0编辑  收藏  举报