当前位置: 首页 > news >正文

MZOI 20251011【CSP-】模拟 T2 序列区间

好题集第四篇。

题意 给一个长度为 $n$ 的正整数序列 $a$ 和一个常数 $k$,求 $a$ 中有多少对 $(l,r)$ 满足 $\frac{\prod_{i=l}^{r}}{\sum_{i=l}^{r}}=k$。

这里用 \(mina\) 表示数组中的最小值,用 \(maxa\) 表示数组中的最大值。

对于前 \(30\%\) 的测试点,\(n\le10\)\(maxa\le10\)

对于前 \(60\%\) 的测试点,\(n\le100\)

对于前 \(80\%\) 的测试点,\(mina\ge2\)

对于 \(100\%\) 的测试点,\(n\le 2\times10^5\)\(k\le10^5\)\(maxa\le10^8\)\(mina\ge1\)

Hint 1 想想对于 $60$ 分的数据的做法。
Hint 2 考虑 $80$ 分的数据,能不能发现“人类智慧”?
Hint 3 考虑 $100$ 分的数据,对于有 $1$ 出现的情况,如何处理这些 $1$?
Solution 设一段区间的乘积为 $tim$,区间和为 $sum$。

我们来看一堆 \(1\) 连续的情况。

假如现在区间 \([l,r]\) 全部都是 \(1\),那么 \(tim=1\)\(sum=r-l+1\)。假设现在我们后面又多了 \(cnt\)\(1\),那么现在如果它要计入贡献的话就要满足 \(tim=k\times(sum+cnt)\),也就是 \(1=k\times(sum+cnt)\)。于是我们得到 \(cnt\) 的范围: \(1\le cnt\le\frac{1}{k}-sum\)。所以在这个范围内的 \(cnt\) 就可以计数。

但是直接枚举每一个 \(1\),复杂度就会变成 \(O(n^2)\),如何优化呢?

我们可以维护一个 \(rt\) 数组,来记录这一段连续的 \(1\) 的右端点。所以我们遇到这些 \(1\) 就可以一起处理,而不是直接遍历,从而把复杂度降下来。

总时间复杂度均摊 \(O(n)\)

Code
#include <bits/stdc++.h>
#define loop(i,a,b) for(int i=(a);~i;i=(b))
#define eb emplace_back
#define pb push_back
#define print(x,c) write(x),putchar(c),flush()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr int N = 2e5 + 15;
constexpr ll INF = 2e18;namespace FAST_IO {
#define IOSIZE 300000char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)template <typename T> inline void read (T &x) {	x = 0; T f = 1;char ch = getchar ();while (!isdigit (ch)) {if (ch == '-') f = -1; ch = getchar ();}while (isdigit (ch)) {x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar ();} x *= f;}template <> inline void read (double &x) { x = 0; int f = 1;char ch = getchar ();while (!isdigit (ch)) { if (ch == '-') f = -1; ch = getchar ();} while (isdigit (ch)) x = x * 10 + (ch - '0'), ch = getchar ();if (ch == '.') {ch = getchar (); for (double t = 0.1; isdigit (ch); t *= 0.1) x += t * (ch - '0'), ch = getchar ();}x *= f;}inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }template <typename T, typename ...Args> inline void read (T &x, Args &...args) {read(x); read(args...);}template <typename T> inline void write (T x) {	if (x < 0) putchar ('-'), x = -x; if (x > 9) write (x / 10); putchar (x % 10 + 48);}inline void write(char *x) { while (*x) putchar(*x++); }inline void write(const char *x) { while (*x) putchar(*x++); }inline void flush() { if (p3 != obuf) { fwrite(obuf, 1, p3 - obuf, stdout);p3 = obuf;}}
}
using namespace FAST_IO;int n;
ll k;
int a[N];
ll ans;
int rt[N];int main () {read (n, k);for (int i = 1; i <= n; ++ i) {read (a[i]);}for (int i = n; i >= 1; -- i) {if (a[i] == 1) {rt[i] = (a[i + 1] == 1) ? rt[i + 1] : i;} else rt[i] = i;}for (int l = 1; l <= n; ++ l) {ll sum = 0, tim = 1;for (int r = l; r <= n; ) {if (a[r] == 1) {int cnt = rt[r] - r + 1;if (tim % k == 0) {ll tmp = tim / k - sum;if (tmp >= 1 && tmp <= cnt) ++ ans;}sum += cnt;r = rt[r] + 1;} else {if (tim * a[r] > INF) break;tim *= a[r];sum += a[r];if (tim == k * sum) ++ ans;++ r;}}}print (ans, '\n');return 0;
}
http://www.zskr.cn/news/18831.html

相关文章:

  • 完整教程:后端进阶-性能优化
  • Java的各类定时任务实现
  • 03:运算符
  • python静态类型之any
  • 2025 年最新金蝶云服务商推荐榜单:聚焦铂金伙伴技术实力与万级客户口碑,助力企业数字化转型精准选型上海金蝶云服务商推荐
  • 使用 C++ 和 minizip 实现 ZIP 压缩解压工具
  • 西部数码使用外部dns服务器怎么配置解析
  • 一看就懂,Oracle认证体系中的OCP中级认证
  • 2025 年试验机生产厂家最新推荐榜单:聚焦优质企业,助力精准选购高低温等各类试验设备弹簧拉压/弹簧疲劳/高频弹簧疲劳/U型弹簧专用试验机厂家推荐
  • IIS/如何查看IIS上部署网站的实时连接数
  • 拼叨叨砍价系统:实体店低成本引流的营销利器
  • SLS Copilot 实践:基于 SLS 灵活构建 LLM 应用的数据基础设施
  • 超高密度2kW GaN基低压电机驱动器的设计 - 实践
  • 在AI技术唾手可得的时代,挖掘新需求成为核心竞争力——某知名AI代理框架需求洞察
  • 02:基础数据类型
  • 接口测试全流程实战:从工具到架构的深度解析
  • C# Send and receive big file via stream
  • 2、python get请求
  • 可解释AI技术解析与模型监控实践
  • 开源多场景问答社区论坛Apache Answer本地部署并发布至公网使用 - 实践
  • 2025异型钢厂家最新推荐榜:定制化生产与卓越品质引领者
  • 2025热喷铝厂家最新推荐榜:品质卓越与专业服务的行业首选!
  • 2025方钢厂家,异型钢厂家,六角钢厂家最新推荐榜:品质与性价比兼备的首选品牌!
  • QuickLook软件!一款鼠标单击PDF即能显示内容的软件!
  • iOS 26 崩溃日志深度指南,如何收集、符号化、定位与监控 - 实践
  • 2020CSP-J2比赛记录题解
  • 让博客园设置支持PlantUml画图
  • 光谱相机的未来趋势 - 详解
  • Hall定理学习笔记
  • 面向对象抽象,接口多态综合-动物模拟系统