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;
}
相关推荐
橘子汽水1683 小时前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表
huameinan狮子3 小时前
Adaboost算法原理与计算实例
算法
Tri_Function3 小时前
动态规划DP1(c++)
c++
别怪我很水3 小时前
API中提供了VelocityTracker类用于计算触摸事件MotionEvent的速度,而其内部默认使用的方法就是最小二乘法,本 ...
算法·gitee·最小二乘法
清水迎朝阳6 小时前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
Re.不晚6 小时前
挑战做100道力扣算法- DAY1
算法·leetcode·职场和发展
蓝悦无人机6 小时前
《Planning algorithms》读书笔记——第1章 引言
算法·读书笔记·规划算法·lavalle
Sagittarius_A*7 小时前
分组密码基础(二):Feistel 结构与 DES 的设计思想
算法·信息安全·密码学·des·数论
青山木7 小时前
Hot 100 --- 搜索插入位置
java·数据结构·算法·leetcode