牛客挑战赛58

比赛地址

牛客挑战赛58

给定 n,m ,求有多少对长度为 n 的序列 a,b 合法。
两个序列 a,b 合法被定义为:
a1|a2|anb1b2bn,i,ai,bi[0,2m)
其中 | 表示按位或, 表示按位异或。

输入描述:

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

输出描述:

一个非负整数, 表示答案对 109+7 取模后的值。

示例1

输入

3 3

输出

233472

备注:

保证 n,m106.

解题思路

思维

由高位到低位考虑不同位的位置:
假设不同位在第 i 位,即要求左边第 i 位为 1,右边为 0,左边为 1 则有 2n1 种方案,右边为 0 可以先随便填 n1,最后一个可以决定这位为 0,即有 2n1 种方案,i1 位前面的数可以随便填,即对于 i1 后面的数有 22n 种方案,i 前面的数要求一样,同理有 22n1 种方案,故左边大于右边有 (2n1)×2n1×22n×22n1,而左右相等有 (22n1)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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16025940.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示