跳到正文

题解

题解:AT_arc135_b [ARC135B] Sum of Three Terms

Lele_Programmer约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年1月28日
收录
2026年10月12日
更新
2026年10月28日
篇幅
约 5 分钟
获赞
2

版本

  1. v1当前

AT_arc135_b 题解

思路

∵Si=ai+ai+1+ai+2,Si+1=ai+1+ai+2+ai+3\because S_i = a_i + a_{i+ 1} + a_{i+2}, S_{i+1} = a_{i+ 1} + a_{i+2} + a_{i+3} ∴Si+1−Si=ai+3−ai\therefore S_{i+1}-S_i = a_{i+3}-a_i

因此只需要确定 a1,a2,a3a_1,a_2,a_3 的值,后面的所有值就都可以确定了。

分别以下标 1,2,31,2,3 为起点,可以发现每次往后 33 个位置的增量成链式分布,且互不影响。对于每一条链,找出起点所能够填的最小的自然数,使得链中每一个数都为大于 00。对 33 条链都这样做一遍,如果初始 a1+a2+a3>S1a_1+a_2+a_3>S_1,则无解,因为这三个起点的值都不能减少。将其中任意一者不断自增,直到 a1+a2+a3=S1a_1+a_2+a_3=S_1,输出 aa 序列即可。

代码

C++
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define FRR(file) freopen(file,"r",stdin)
#define FRW(file) freopen(file,"w",stdout)
#define TIMESTAMP cerr<<fixed<<setprecision(3)<<clock()*1.0/CLOCKS_PER_SEC<<"s"<<endl;
#define _rep(i,a,b) for (int i=(a);i<=(b);++i)
#define _reps(i,a,b,c) for (int i=(a);i<=(b);c)
#define _rrep(i,a,b) for (int i=(a);i>=(b);--i)
#define _rreps(i,a,b,c) for (int i=(a);i>=(b);c)
#define _iter(i,a) for (auto i=a.begin();i!=a.end();++i)
#define _graph(i,u) for (int i=h[u];~i;i=ne[i])
#define rint register int
#define LL long long
#define i32 signed
#define i64 long long
#define i128 __int128
#define u32 unsigned
#define u64 unsigned long long
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
typedef pair<double,double> pdd;

namespace IO {
    template<typename T> inline void read(T& x) {
        int s=1; char c=getchar(); x=0;
        while (!isdigit(c)) { if (c=='-') s=-1; c=getchar(); }
        while (isdigit(c)) x=x*10+(c-'0'),c=getchar();
        x*=s;
    }
    template<typename T> inline void write(T x) {
        if (x<0) putchar('-'),x=-x;
        if (x/10) write(x/10);
        putchar('0'+(x%10));
    }
    template<typename T> inline void writesp(T x) { write(x); putchar(' '); }
    template<typename T> inline void writeln(T x) { write(x); putchar(10); }
};

using namespace IO;

const int N=300005;
const int inf=2.1e9;

int n;
int s[N],c[N];
i64 a[N];

int main() {
    read(n);
    _rep(i,1,n) read(s[i]);
    _rep(i,1,n-1) c[i]=s[i+1]-s[i];
    _rep(i,1,3) {
        int cur=c[i];
        int mn=c[i];
        int j=i;
        while (j<=n) {
            j+=3;
            cur+=c[j];
            mn=min(mn,cur);
        }
        a[i]=max(-mn,0);
    }
    if (a[1]+a[2]+a[3]>s[1]) {
        puts("No");
        return 0;
    }
    puts("Yes");
    a[1]+=s[1]-(a[1]+a[2]+a[3]);
    _rep(i,1,n) {
        a[i+3]=a[i]+c[i];
        writesp(a[i]);
    }
    writesp(a[n+1]);
    writesp(a[n+2]);
    return 0;
}
原文
存档评论1 条抓取于