当前位置:网站首页>2/11 matrix fast power +dp+ bisection
2/11 matrix fast power +dp+ bisection
2022-07-06 04:03:00 【Zhong Zhongzhong】
Matrix fast power template problem :
It can solve the problem of large data scale , But there are recursion formulas
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll n,k;
struct Matrix
{
static const int N=102; // Open matrix
ll a[N][N];
Matrix(ll e=0) // Matrix Qing 0
{
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
a[i][j]=e*(i==j);
}
Matrix mul(Matrix A,Matrix B) // Multiplication of matrices A*B
{
Matrix ans(0); // Initialize all to 0 Matrix
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
for (int k=1;k<=n;k++)
{
// Analog multiplication
ans.a[i][j]=(ans.a[i][j]+A.a[i][k]*B.a[k][j])%mod;
}
}
}
return ans; // return
}
Matrix ksm(Matrix A,ll b){
Matrix ans(1); // Initialize all to 1 Matrix
while (b)
{
if (b&1)ans=mul(ans,A); // Fast power
A=mul(A,A);
b>>=1;
}
return ans;
}
};
int main()
{
scanf("%lld%lld",&n,&k);
Matrix tmp;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>tmp.a[i][j];
}
tmp=tmp.ksm(tmp,k);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<tmp.a[i][j]<<" ";
cout<<endl;
}
return 0;
}
https://www.luogu.com.cn/problem/P1103
The difficulty is
Determine the meaning of the array
and
Design state transition equation
#include <bits/stdc++.h>
using namespace std;
const int mod=1e6+7;
const int inf=0x3f3f3f3f;
int f[105][105],n,k,m,a[105],minn=inf;
struct book
{
int h,w;
}e[105];
bool cmp(book e1,book e2)
{
return e1.h<e2.h;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d%d",&e[i].h,&e[i].w);
sort(e+1,e+n+1,cmp);
memset(f,inf,sizeof(f));
for(int i=1;i<=n;i++)
{
f[i][1]=0;
}
for(int i=2;i<=n;i++) // Indicates which book to add
{
for(int j=1;j<i;j++) // And j This book is adjacent
{
for(int len=2;len<=min(i,n-k);len++) // The number of books currently selected
f[i][len]=min(f[i][len],f[j][len-1]+abs(e[i].w-e[j].w));
}
}
for(int i=n-k;i<=n;i++)
minn=min(minn,f[i][n-k]);
cout<<minn<<endl;
return 0;
}
https://www.luogu.com.cn/problem/P1182
The maximum is the smallest
while(l<r)
{
int mid=(l+r)>>1;
if(check(mid)) // The quantity distributed exceeds the demand , return 1
l=mid+1;
else
r=mid;
}
The minimum is the maximum :
while(l<r)
{
int mid=(l+r+1)>>1;
if(check(mid)) // The quantity divided is less than the demand , return 1
r=mid-1;
else
l=mid;
}
#include <bits/stdc++.h>
using namespace std;
const int mod=1e6+7;
const int inf=0x3f3f3f3f;
int n,m,a[100005],l,r;
int check(int x)
{
int sum=0,num=0;
for(int i=1;i<=n;i++)
{
if(sum+a[i]<=x)
sum+=a[i];
else
sum=a[i],num++;
}
return num>=m; // The number of points is greater than the required number of questions , explain x It's worth less
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
l=max(l,a[i]);
r+=a[i];
}
while(l<r) // The template with the minimum maximum
{
int mid=(l+r)>>1;
if(check(mid))
l=mid+1;
else
r=mid;
}
cout<<l<<endl;
return 0;
}
边栏推荐
- Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
- Align items and align content in flex layout
- [introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
- P7735-[noi2021] heavy and heavy edges [tree chain dissection, line segment tree]
- [disassembly] a visual air fryer. By the way, analyze the internal circuit
- MySQL about self growth
- 【FPGA教程案例12】基于vivado核的复数乘法器设计与实现
- Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
- Proof of Stirling formula
- ESP32_ FreeRTOS_ Arduino_ 1_ Create task
猜你喜欢

《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动

【按鍵消抖】基於FPGA的按鍵消抖模塊開發

C mouse event and keyboard event of C (XXVIII)

Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did

Basic knowledge of binary tree, BFC, DFS

An article will give you a comprehensive understanding of the internal and external components of "computer"
![[Massey] Massey font format and typesetting requirements](/img/27/6b641551d6d8699683972f40f3b8e5.jpg)
[Massey] Massey font format and typesetting requirements

Flask learning and project practice 9: WTF form verification
![[analysis of variance] single factor analysis and multi factor analysis](/img/92/5337d0ef6e487d1af2f56cb3a3268a.jpg)
[analysis of variance] single factor analysis and multi factor analysis
![[FPGA tutorial case 11] design and implementation of divider based on vivado core](/img/39/f337510c2647d365603a8485583a20.png)
[FPGA tutorial case 11] design and implementation of divider based on vivado core
随机推荐
[meisai] meisai thesis reference template
Python book learning notes - Chapter 09 section 01 create and use classes
如何修改表中的字段约束条件(类型,default, null等)
Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
How to execute an SQL statement in MySQL
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
判断当天是当月的第几周
How do we make money in agriculture, rural areas and farmers? 100% for reference
Basic concepts of LTE user experience
Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
What is the difference between gateway address and IP address in tcp/ip protocol?
Alibaba testers use UI automated testing to achieve element positioning
Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
C (thirty) C combobox listview TreeView
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
User perceived monitoring experience
2.13 weekly report
Interface idempotency
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
Scalpel like analysis of JVM -- this article takes you to peek into the secrets of JVM