前缀和
非常认真地观察代码之后我们会发现实际上这是一个多维前缀和问题,c就记录前缀和。考虑前缀和的逆操作,即可复原出原数组。
提示:k维前缀和可以看作k-1维前缀和的前缀和,从而递归下去
#include<cstdio>
#define R register
#define N 1000005
using namespace std;
namespace runzhe2000
{
typedef long long ll;
inline ll read()
{
R ll r = 0; R char c = getchar(); R bool p = 0;
for(; c < '0' || c > '9'; c = getchar()) c == '-' ? p = 1 : 0;
for(; c >='0' && c <='9'; r = r*10+c-'0', c = getchar());
return p?-r:r;
}
int n, m, a[N], tmpa[N];
int c[N], pre[N];
void main()
{
n = read(); R int tmpn = 0;
for(R int i = 0; i < n; i++)
{
R int v = read(); tmpa[tmpn++] = v;
if(v == 1) {n--; i--; continue;}
else a[i] = v;
}
m = read(); a[n++] = 2000000000;// fuck
for(R int i = 0; i < m; i++)
c[i] = read();
pre[0] = 1;
for(R int i = 1; i < n; i++)
if((pre[i] = (ll)a[i-1] * pre[i-1]) > m) {n = i; break;}
for(R int j = 0; j < n; j++)
{
R int aj = a[j], pj = pre[j], *d = c - pj, zzakx = m%pj, ygakg = m/pj%aj;
for(R int i = m-1; i >= 0; --i)
{
(zzakx ? --zzakx : (zzakx = pj - 1, ygakg ? --ygakg : ygakg = aj - 1));
ygakg ? c[i] -= d[i] : 0;
}
}
printf("%d\n",tmpn); for(R int i = 0; i < tmpn; i++) printf("%d ",tmpa[i]); puts("");
printf("%d\n",m); for(R int i = 0; i < m; i++) printf("%d ",c[i]); puts("");
}
}
int main()
{
runzhe2000::main();
}