P8865 [NOIP2022] 种花 题解
前言#
去年多测不清空导致即便 CCF 放过了我的
当时看清空数组后能过 CCF 数据就没再管。
时隔
思路#
想到计算方案的话可以维护两个数组
考虑以每个点为
那么当前点
当前行肯定不能算,下一行是
同样考虑以每个点为
那么当前点
最后那一块就是
然后我们就得到了一个
解法#
发现全是
发现再对
然后我们可以利用前缀和
同理对于
/*
* @Author: Aisaka_Taiga
* @Date: 2023-10-26 21:02:05
* @LastEditTime: 2023-10-26 22:22:57
* @LastEditors: Aisaka_Taiga
* @FilePath: \Desktop\P8865.cpp
* The heart is higher than the sky, and life is thinner than paper.
*/
#include <bits/stdc++.h>
#define int long long
#define P 998244353
#define N 1100
using namespace std;
inline int read()
{
int x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();}
while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * f;
}
int n, m, c, f, a[N][N], c1[N][N], c2[N][N], c3[N][N], c5[N][N];
//c1每个点向右的零的个数,c2每个点向下的零的个数, c3每一列c2的前缀和,c5表示f形的下半部分当前点合法的数量。
//1h写了n^3的,80分
//1.5h终于把优化后的给写完了。
inline void qk()
{
for(int i = 0; i <= n; i ++)
for(int j = 0; j <= m; j ++)
a[i][j] = c1[i][j] = c2[i][j] = c3[i][j] = c5[i][j] = 0;
return ;
}
inline void work()
{
qk();
int ans1 = 0, ans2 = 0;
n = read(), m = read(), c = read(), f = read();
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
char cc;
cin >> cc;
a[i][j] = cc - '0';
}
}
for(int i = 1; i <= n; i ++)
{
int t = 0;
for(int j = m; j >= 1; j --)
{
if(a[i][j] == 0) t ++;
else t = 0;
c1[i][j] = (t == 0 ? 1 : t);
}
}
for(int i = 1; i <= m; i ++)
{
int t = 0;
for(int j = n; j >= 1; j --)
{
if(a[j][i] == 0) t ++;
else t = 0;
c2[j][i] = (t == 0 ? 1 : t);
}
}
for(int j = 1; j <= m; j ++)
for(int i = 1; i <= n; i ++)
c3[i][j] = (c3[i - 1][j] + c1[i][j] - 1) % P,
c5[i][j] = (c5[i - 1][j] + (c1[i][j] - 1) * (c2[i][j] - 1)) % P;
for(int i = 1; i <= n - 2; i ++)
{
for(int j = 1; j <= m - 1; j ++)
{
if(c2[i][j] < 3) continue;
ans1 = (ans1 + ((c1[i][j] - 1) * (c3[i + c2[i][j] - 1][j] - c3[i + 1][j])) % P) % P;
if(c2[i][j] < 4) continue;
ans2 = (ans2 + ((c1[i][j] - 1) * (c5[i + c2[i][j] - 1][j] - c5[i + 1][j])) % P) % P;
}
}
cout << ans1 * c << " " << ans2 * f << endl;
return ;
}
signed main()
{
int T = read(), iid = read();
while(T --) work();
return 0;
}
作者: 北烛青澜
出处:https://www.cnblogs.com/Multitree/p/17790684.html
本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战