当前位置: 代码迷 >> 综合 >> 2020牛客多校第二场 F.Fake Maxpooling
  详细解决方案

2020牛客多校第二场 F.Fake Maxpooling

热度:68   发布时间:2024-01-28 08:15:13.0

谜一样的题目 看到有倍增的做法,偷懒的,双向队列维护矩阵的,奇奇怪怪的,

//偷懒的
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
const int maxn=5e3+10;
int a[maxn][maxn];
int n,m,k;
void init(){for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(i==j){a[i][j]=i;continue;}a[i][j]=i*j/__gcd(i,j);}}
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cout.tie(0);cin>>n>>m>>k;init();int ans=0;for(int i=k;i<=n;i++){for(int j=k;j<=m;j++){int tmp=0;for(int ii=0;ii<min(k,10);ii++){for(int jj=0;jj<min(k,10);jj++){tmp=max(tmp,a[i-ii][j-jj]);}}ans+=tmp;}}cout<<ans<<endl;return 0;
}
//双向队列的
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
struct node
{ll ans,val;
};
vector<ll> v[5005];
signed main()
{std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);ll n,m,k,sum = 0;cin >> n >> m >> k;for(int i = 1;i <= n;i++){deque<node> q;for(int j = 1;j <= m;j++){ll Lcm = i*j/(__gcd(i,j));while(!q.empty() && Lcm > q.back().val)q.pop_back();q.push_back({j,Lcm});while(!q.empty() && j-q.front().ans >= k)q.pop_front();if(j >= k)v[i].push_back(q.front().val);}}for(int i = 0;i < m-k+1;i++){deque<node> q;for(int j = 1;j <= n;j++){while(!q.empty() && q.back().val < v[j][i])q.pop_back();q.push_back({j,v[j][i]});while(!q.empty() && j - q.front().ans >= k)q.pop_front();if(j >= k)sum += q.front().val;}}cout << sum << endl;return 0; 
}
```cpp
//奇奇怪怪的
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;const int maxn=5009;
int n,m,k,a[maxn][maxn];
int main(){cin>>n>>m>>k;for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)if(k==1) a[i][j]=i*j/__gcd(i,j);else a[i][j]=max(i*j/__gcd(i,j),max(a[i-1][j],a[i][j-1]));ll ans=0;for(int i=k;i<=n;i++) for(int j=k;j<=m;j++) ans+=a[i][j];cout<<ans;
}
//倍增
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1),eps=1e-8;const int maxm = 5e3+2;int a, b, n,logn;
int grid[maxm][maxm];
int maxv[maxm][maxm];
int gcd(int a,int b)
{return b==0?a:gcd(b,a%b);
}int query (int x, int y){int _max = 0;_max = max(maxv[x][y], max(maxv[x+n-(1<<logn)][y+n-(1<<logn)], max(maxv[x+n-(1<<logn)][y], maxv[x][y+n-(1<<logn)])));//_min = min(minv[x][y], min(minv[x+n-(1<<logn)][y+n-(1<<logn)], min(minv[x+n-(1<<logn)][y], minv[x][y+n-(1<<logn)])));return _max;
}int main ()
{
// freopen("in.txt", "r", stdin);//cin >> a >> b >> n;scanf("%d %d %d",&a,&b,&n);for (int i = 0; i < a; i++)for (int j = 0; j < b; j++) {grid[i][j]=(i+1)*(j+1)/gcd(i+1,j+1);maxv[i][j] = grid[i][j];}for (logn = 0; ((1<<(logn+1)) <= n); logn++);for (int k = 0; k < logn; k++)for (int i = 0; i+(1<<k) < a; i++)for (int j = 0; j+(1<<k) < b; j++) {maxv[i][j] = max(maxv[i][j], max(maxv[i+(1<<k)][j+(1<<k)], max(maxv[i+(1<<k)][j], maxv[i][j+(1<<k)])));//minv[i][j] = min(minv[i][j], min(minv[i+(1<<k)][j+(1<<k)], min(minv[i+(1<<k)][j], minv[i][j+(1<<k)])));}long long sum=0;for (int i = 0; i <= a-n; i++)for (int j = 0; j <= b-n; j++)sum+=query(i,j);printf("%lld\n",sum);return 0;
}