L. Coordinate Paper(CCPC 长春)构造
L. Coordinate Paper
構(gòu)造一個長度為nnn的序列aaa,滿足ai≥0a_i \geq 0ai?≥0,∑i=1nai=s\sum\limits_{i = 1} ^{n} a_i = si=1∑n?ai?=s,對于任意的i∈[1,n?1]i \in [1, n - 1]i∈[1,n?1],都有ai?ai+1=korai+1?ai=1a_i - a_{i + 1} = k\ or\ a_{i + 1} - a{i} = 1ai??ai+1?=k?or?ai+1??ai=1,其中n,s,kn, s, kn,s,k是給定的數(shù)。
假定所有的aia_iai?都滿足ai+1?ai=1a_{i + 1} - a_{i} = 1ai+1??ai?=1,則對于一個給定的a1a_1a1?,有∑i=1nai=n×a1+n(n?1)2\sum\limits_{i = 1} ^{n} a_i = n \times a_1 + \frac{n(n - 1)}2i=1∑n?ai?=n×a1?+2n(n?1)?,考慮ai?ai+1=ka_i - a_{i + 1} = kai??ai+1?=k,∑i=1nai=n×a1+n(n?1)2?x(k+1)\sum\limits_{i = 1} ^{n} a_i = n \times a_1 + \frac{n(n - 1)}2- x (k + 1)i=1∑n?ai?=n×a1?+2n(n?1)??x(k+1)。
考慮對所有數(shù)(modk+1)\pmod {k + 1}(modk+1)下找到一個解,滿足∑i=1nai(modk+1)≡s(modk+1)\sum\limits_{i = 1} ^{n} a_i \pmod {k + 1} \equiv s \pmod {k + 1}i=1∑n?ai?(modk+1)≡s(modk+1)。
考慮同余下的一個最小解,a1,a1+1,…,k,0,1,2,…,k,0,1,2,…a_1, a_1 + 1, \dots, k, 0, 1, 2, \dots, k, 0, 1, 2, \dotsa1?,a1?+1,…,k,0,1,2,…,k,0,1,2,…,如果滿足sum≤sandsum(modk+1)≡s(modk+1)sum \leq s \ and\ sum \pmod{k + 1} \equiv s \pmod{k + 1}sum≤s?and?sum(modk+1)≡s(modk+1),則說明找到一組解。
獲得最小解后,湊得答案,只需每次對最小得加k+1k + 1k+1,這樣可以保證變動之后這個數(shù)比其前方的大111,后面的比其小kkk,滿足要求。
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int n, k, flag;long long s, a[N];vector<int> pos[N];inline int add(int x, int y) {return x + y < k + 1 ? x + y : x + y - k - 1; }void solve() {for (int i = 1; i <= n; i++) {a[i] = add(a[i - 1], i != 1);pos[a[i]].push_back(i);}int x = a[1], len = min(k - x + 1, n), last = n - len;long long sum = 1ll * (x + x + len - 1) * len / 2;sum += 1ll * (last / (k + 1)) * (0 + k) * (k + 1) / 2;last %= k + 1;sum += 1ll * (0 + 0 + last - 1) * last / 2;s -= sum;sum = s / (k + 1);long long tot = sum / n, more = sum % n;for (int i = 1; i <= n; i++) {a[i] += 1ll * (k + 1) * tot;}for (int i = 0; i <= k; i++) {for (auto it : pos[i]) {if (more) {a[it] += k + 1;more--;}}}for (int i = 1; i <= n; i++) {printf("%lld%c", a[i], i == n ? '\n' : ' ');} }bool judge(int x) {int len = min(k - x + 1, n), last = n - len;long long sum = 1ll * (x + x + len - 1) * len / 2;sum += 1ll * (last / (k + 1)) * (0 + k) * (k + 1) / 2;last %= k + 1;sum += 1ll * (0 + 0 + last - 1) * last / 2;return sum <= s && s % (k + 1) == sum % (k + 1); }int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);cin >> n >> k >> s;for (int i = 0; i <= k; i++) {if (judge(i)) {flag = 1;a[0] = i;solve();break;}}if (!flag) {puts("-1");}return 0; }總結(jié)
以上是生活随笔為你收集整理的L. Coordinate Paper(CCPC 长春)构造的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: B.The Tortoise and t
- 下一篇: 股骨短几周确诊为畸形