AcWing 第127场周赛 构造矩阵

构造题目,考虑去除掉最后一行最后一列先进行考虑,假设除了最后一行和最后一列都已经排好了(你可以随便排),那么分析知最后一个数字由限制以外其他都已经确定了,无解的情况是k为-1

并且n,m的奇偶性不同其余均有解 并且方案数就是2**(n-1)*(m-1)%p 发现数很大,欧拉降幂

原式等价于2**(n-1)%(p-1)*(m-1)%(p-1) %p

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N = 2e5+10,p = 1e9+7;
ll n,m,k;

ll qmi(ll a,ll b,ll p){
	ll ans = 1;
	while(b){
		if(b&1)
		 ans = ans*a%p;
		b>>=1;
		a = a*a%p;
	}
	
	return ans;
}
void solve()
{
	cin>>n>>m>>k;
	if(k==-1&&(n%2!=m%2))cout<<-1;
	else{
		ll t = ((n-1)%(p-1))*((m-1)%(p-1));
		cout<<qmi(2,t,p);
	}
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	_ = 1;
	while(_--)solve();
	return 0;
}

贴一个y总的分析图片

当然你写两次快速幂也是一样的,这里也是知道你底数可以随便模这个性质

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N = 2e5+10,p = 1e9+7;
ll n,m,k;

ll qmi(ll a,ll b,ll p){
	ll ans = 1;
	while(b){
		if(b&1)
		 ans = ans*a%p;
		b>>=1;
		a = a*a%p;
	}
	
	return ans;
}
void solve()
{
	cin>>n>>m>>k;
	if(k==-1&&(n%2!=m%2))cout<<0;
	else{
		// ll t = ((n-1)%(p-1))*((m-1)%(p-1));
		// cout<<qmi(2,t,p);
		ll t = qmi(2,m-1,p);
		cout<<qmi(t,n-1,p);
	}
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	_ = 1;
	while(_--)solve();
	return 0;
}
相关推荐
小白狮ww3 分钟前
Retinex 算法 + MATLAB 软件,高效率完成图像去雾处理
开发语言·人工智能·算法·matlab·自然语言处理·图像识别·去雾处理
444A4E1 小时前
C++模板:泛型编程的魔法手册,从入门到“魔改”
c++·编译原理
trust Tomorrow1 小时前
每日一题-力扣-2278. 字母在字符串中的百分比 0331
算法·leetcode
Chiyamin1 小时前
C++面向对象速览(三)
c++
Tadecanlan1 小时前
[C++面试] 智能指针面试点(重点)续4
开发语言·c++·面试
Chiyamin1 小时前
C++面向对象速览(一)
c++
Lecea_L1 小时前
你能在K步内赚最多的钱吗?用Java解锁最大路径收益算法(含AI场景分析)
java·人工智能·算法
Tony881 小时前
热题100 - 394. 字符串解码
java·算法
GOTXX1 小时前
BoostSiteSeeker项目实战
前端·c++·后端·mysql·搜索引擎·项目实战·boost
Lecea_L2 小时前
🔍 找到数组里的“节奏感”:最长等差子序列
java·算法