# 【ACM-ICPC 2018 沈阳赛区网络预赛】不太敢自称官方的出题人题解

## A. Gudako and Ritsuka

by Yuki & Asm.Def

//Asm.Def
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100005;
int A, B, M;
LL N, x;
bool p[maxn], beg_A;

void init()
{
scanf("%lld%lld%d%d", &N, &x, &A, &B);
assert(A+B <= 100000);
assert(A+B >= N);
assert(A >= 1 && B >= 1);
M = A+B;
for(int i = 1;i <= M;++i) p[i] = false;
int a;
beg_A = false;
for(int i = 0;i < A;++i)
{
scanf("%d", &a);
p[a] = 1;
if(a == 1) beg_A = true;
}
}

int a[maxn], len;

struct Seg
{
int l, r;
LL len;
};
bool operator < (const Seg &a, const Seg &b)
{
return a.len > b.len;
}

void work()
{
len = 0;
int cnt = 0;
for(int i = 1;i <= M;++i)
{
++cnt;
if(i == M || p[i+1] != p[i])
{
a[len++] = cnt;
cnt = 0;
}
}
bool ans = false;

static LL loc[maxn];
static int nxt[maxn], lst[maxn];
static bool sel[maxn];
priority_queue<Seg> Q[2];

bool L = 0, R = 1;
LL offs[2] = {0};

loc[len] = 0;
nxt[len] = lst[len] = len;//用链表记录当前区间端点

sel[len] = false;
for(int i = len-1;i >= 0;--i)
{
//区间整体右移
offs[L] += a[i] * x;
offs[R] += a[i];
L ^= 1, R ^= 1;

//插入新增的左端点
loc[i] = -offs[L];
nxt[i] = nxt[len];
lst[nxt[i]] = i;
lst[i] = len;
nxt[len] = i;
sel[i] = true;
Q[L].push( (Seg){i, nxt[i], loc[nxt[i]]-loc[i]} );

//合并区间
LL t = offs[R] - offs[L];//R(k)+offs[R] < L(k+1)+offs[L]
Seg tmp;
while(!Q[R].empty() && ((tmp = Q[R].top()).len <= t || !sel[tmp.l] || !sel[tmp.r]) )
{
Q[R].pop();
if(sel[tmp.l] && sel[tmp.r])//合并一对跨立的"(](]",
{
sel[tmp.l] = sel[tmp.r] = false;
nxt[lst[tmp.l]] = nxt[tmp.r];
lst[nxt[tmp.r]] = lst[tmp.l];
Q[L].push( (Seg){lst[tmp.l], nxt[tmp.r], loc[nxt[tmp.r]]-loc[lst[tmp.l]]} );
}
//否则为已被合并的区间，无需处理
}

}

int it = nxt[len];
ans = false;
while(it != len && loc[it]+offs[L] < N)
{
if(nxt[it] == N || loc[nxt[it]]+offs[R] >= N)
{
ans = true;
break;
}
it = nxt[it];
if(it != len) it = nxt[it];
if(it == 0) break;
}
puts((ans ^ beg_A) ? "Ritsuka" : "Gudako");
}

int main()
{
clock_t beg = clock();
int T;
scanf("%d", &T);
while(T--)
{
init();
work();
}
//printf("%.3f sec\n", double(clock()-beg) / CLOCKS_PER_SEC);
return 0;
}


## B. Call of Accepted

$\min(S_1 + S_2)=\min(S_1)+\min(S_2), \max(S_1+S_2)=\max(S_1)+\max(S_2)$

$\min(S_1-S_2) = \min(S_1)-\max(S_2), \max(S_1-S_2) = \max(S_1) - \min(S_2)$

$\min(S_1*S_2)=\min\{\min(S_1)*\min(S_2),\ \min(S_1)*\max(S_2),\ \max(S_1) * \min(S_2),\ \max(S_1)*\max(S_2)\}$

$\max(S_1*S_2)=\max\{\min(S_1)*\min(S_2),\ \min(S_1)*\max(S_2),\ \max(S_1) * \min(S_2),\ \max(S_1)*\max(S_2)\}$

p.s.虽然从实际意义来看d运算不满足结合律，但如果只考虑二元组$<\min(S), \max(S)>$的话，有$<\min(S_1\ {\rm d}\ S_2\ {\rm d}\ \cdots\ {\rm d}\ S_n),\ \max(S_1\ {\rm d}\ S_2\ {\rm d}\ \cdots\ {\rm d}\ S_n)>\ =\ <\min(S_1),\ \max(S_1) \max(S_2) \cdots \max(S_n)>$，是无需考虑${\rm d}$运算的结合顺序的。

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

struct Interval
{
int L, R;
Interval(){}
Interval(int a, int b) : L(a), R(b) {}
void Print(){printf("[%d,%d]\n", L, R);}
};
Interval operator + (const Interval &a, const Interval &b)
{
return Interval(a.L + b.L, a.R + b.R);
}
Interval operator - (const Interval &a, const Interval &b)
{
return Interval(a.L - b.R, a.R - b.L);
}
Interval operator * (const Interval &a, const Interval &b)
{
int mn = min(min(a.L * b.L, a.L * b.R), min(a.R * b.L, a.R * b.R));
int mx = max(max(a.L * b.L, a.L * b.R), max(a.R * b.L, a.R * b.R));
return Interval(mn, mx);
}
Interval f(const Interval &a, const Interval &b)
{
assert(a.L >= 0 && b.L >= 1);
return Interval(a.L, a.R * b.R);
}
int RPNLen, RPNNumLen;
Interval RPNNum[105];
char s[105], RPN[105];

inline int precedence(char ope) {
if (ope == '+') return 1;
if (ope == '-') return 1;
if (ope == '*') return 2;
if (ope == '/') return 2;
if (ope == 'd') return 3;
return 0;
}

void expressionToRPN() {
int stackLen = 0, x = 0;
char stack[105];
RPNLen = 0;
RPNNumLen = 0;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] >= '0' && s[i] <= '9') {
if (i == 0 || s[i-1] < '0' || s[i-1] > '9') {
RPNLen++;
RPN[RPNLen] = 'N';
x = s[i] - '0';
} else {
x = x * 10 - '0' + s[i];
}
if(s[i+1] < '0' || s[i+1] > '9')
RPNNum[++RPNNumLen] = Interval(x, x);
} else if (s[i] == '(') {
stackLen++;
stack[stackLen] = s[i];
} else if (s[i] == ')') {
while (stack[stackLen] != '(') {
RPNLen++;
RPN[RPNLen] = stack[stackLen];
stackLen--;
}
stackLen--;
} else {
while (stackLen > 0 && precedence(s[i]) <= precedence(stack[stackLen])) {
RPNLen++;
RPN[RPNLen] = stack[stackLen];
stackLen--;
}
stackLen++;
stack[stackLen] = s[i];
}
}
while (stackLen > 0) {
RPNLen++;
RPN[RPNLen] = stack[stackLen];
stackLen--;
}
}

Interval CalcRPN() {
int RPNNumCnt = 0, stackLen = 0;
Interval stack[105];
for (int i = 1; i <= RPNLen; i++) {
if (RPN[i] == 'N') {
RPNNumCnt++;
stackLen++;
stack[stackLen] = RPNNum[RPNNumCnt];
} else {
Interval b = stack[stackLen];
stackLen--;
Interval a = stack[stackLen];
stackLen--;
Interval result;
//printf("%c\n", RPN[i]);
//a.Print(), b.Print();
if(RPN[i] == '+') result = a + b;
if(RPN[i] == '-') result = a - b;
if(RPN[i] == '*') result = a * b;
if(RPN[i] == 'd') result = f(a, b);
//result.Print();
stackLen++;
stack[stackLen] = result;
}
}
return stack[stackLen];
}

int main() {
while (scanf("%s", s) == 1) {
expressionToRPN();
Interval ans = CalcRPN();
printf("%d %d\n", ans.L, ans.R);
}
return 0;
}


# Haizs
import re
class Interval:
def __init__(self, l, r):
self.l = l
self.r = r

def __str__(self):
return "%d %d" % (self.l, self.r)

return Interval(self.l + b.l, self.r + b.r)

def __sub__(self, b):
return Interval(self.l - b.r, self.r - b.l)

def __mul__(self, b):
mn = min(min(self.l*b.l, self.r*b.r), min(self.l*b.r, self.r*b.l))
mx = max(max(self.l*b.l, self.r*b.r), max(self.l*b.r, self.r*b.l))
return Interval(mn, mx)

def __pow__(self, b):
return Interval(self.l, self.r * b.r)

a = input()
while a:
a = a.replace("d", "**")
b = re.sub(r"(\d+)", r"Interval(\1,\1)", a)
# print(b)
print(eval(b))
try:
a = input()
except:
break


by catsworld & Asm.Def

## C. Convex Hull

### Solution 1

$ans = \sum_{i=1}^{\sqrt{x}} \mu(i) * \frac{1}{6}(\frac{x}{i^2}+1) * (\frac{x}{i^2}) * (2(\frac{x}{i^2}))+1) * i^4$

$sum(i)=\sum_{j=1}^i j^2$

$Max=\frac{n}{i^2}$

\begin{aligned} &i*i*(\sum_{j=1}^{Max-1}sum(j)) + (n-i*i+1-(Max-1)*i*i)*sum(Max)\\ = &Max*Max*(Max+1)*(Max-1)/12+(n-i*i+1-(Max-1)*i*i)*sum(Max) \end{aligned}

By WYJ2015

### Solution2

$\sum_{i=1}^n gay(i) \cdot (n+1-i) \mod p$

$\sum\limits_{i=1}^{n} gay(i) (n+1-i)$中，$i^2 \cdot (n+1-i)$被计入答案当且仅当i不含有平方因子。不妨考虑对所有i的因子进行容斥，即

\begin{aligned} Ans &= \sum_{x=1}^{[\sqrt{n}]} \mu(x) \sum_{k=1}^{[\frac{n}{x^2}]} (k x^2)^2 * (n+1-k x^2) \mod p\\ &= \sum_{x=1}^{[\sqrt{n}]} \mu(x) \left( (n+1)x^4 \sum_{k=1}^{[\frac{n}{x^2}]} k^2 - x^6 \sum_{k=1}^{[\frac{n}{x^2}]} k^3 \right) \mod p\\ \end{aligned}

\begin{aligned} \sum_{i=1}^n i^2 &= \frac{n(n+1)(2n+1)}{6}\\ \sum_{i=1}^n i^3 &= \left(\frac{n(n+1)}{2}\right)^2 \end{aligned}

by Asm.Def

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
typedef long long LL;

int mu[maxn], P[maxn], pcnt;
bool not_p[maxn];
LL N, mod;

const LL lb = (1LL << 20) - 1;

inline LL mult(LL x, LL y, LL mod)
{
LL a = x >> 20, b = x & lb;
LL c = y >> 20, d = y & lb;
return ( ( ( ( ( (a * c) << 20) + (a * d + b * c) ) % mod) << 20) + b * d) % mod;
}

inline LL S2(LL N)
{
return mult(mult(N, N+1, 6*mod), (2*N+1), 6*mod) / 6;
//return (__int128(N) * (N+1) * (2*N+1) / 6) % mod;
}

inline LL S3(LL N)
{
LL t = mult(N, N+1, mod<<1) >> 1;
//LL t = (__int128(N) * (N+1) / 2) % mod;
return mult(t, t, mod);
}

void init()
{
mu[1] = 1;
for(int i = 2;i < maxn;++i)
{
if(!not_p[i]) P[pcnt++] = i, mu[i] = -1;
for(int j = 0;j < pcnt;++j)
{
if(i * P[j] >= maxn) break;
not_p[i * P[j]] = true;
if(i % P[j] == 0)
{
mu[i * P[j]] = 0;
break;
}
mu[i * P[j]] = -mu[i];
}
}
}

void work()
{
LL ans = 0;
for(int i = 1;LL(i) * i <= N;++i) if(mu[i])
{
LL t = LL(i) * i, t2 = mult(t, t, mod);
ans = (ans + mu[i] * mult( mult(t2,N+1,mod), S2(N/t), mod) ) % mod;
ans = (ans - mu[i] * mult(mult(t2,t,mod), S3(N/t), mod)) % mod;
}
printf("%lld\n", (ans + mod) % mod);
}

int main()
{
init();

while(~scanf("%lld%lld", &N, &mod))
{
work();
}
return 0;
}


by XLC

K短路模板题。由于数据均为随机生成，直接预处理出每个点到终点的最短路后A*搜索即可。

## E. The Cake Is A Lie

by Haizs

#include <bits/stdc++.h>
using namespace std;
const int maxn = 605;
const double eps = 1e-6;
int i, j, t, n, m, k, z, y, x;
double l, r, mid;
int R;
int cmp(double x)
{
if (fabs(x) < eps) return 0;
if (x > 0) return 1;
return -1;
}
struct point
{
double x, y;
point() {}
point(double _x, double _y)
: x(_x), y(_y) {}
void input()
{
scanf("%lf%lf", &x, &y);
}
friend point operator+(const point &a, const point &b)
{
return point(a.x + b.x, a.y + b.y);
}
friend point operator-(const point &a, const point &b)
{
return point(a.x - b.x, a.y - b.y);
}
double norm()
{
return sqrt(x * x + y * y);
}
double angl()
{
return atan2(y, x);
}
} poi[maxn];
double dis[maxn][maxn], ang[maxn][maxn];
pair<double, int> pdi[maxn];
#define mp(a, b) make_pair(a, b)
bool check(double r)
{
int i, j, t, ans = 1, k;
double d;
for (i = 1; i <= n; i++)
{
t = 0;
for (j = 1; j <= n; j++)
if (i != j)
{
if (cmp(dis[i][j] - 2.0 * r) > 0) continue;
d = acos(dis[i][j] / (2.0 * r));
pdi[++t] = mp(ang[i][j] - d, 1);
pdi[++t] = mp(ang[i][j] + d, -1);
}
sort(pdi + 1, pdi + t + 1);
k = 1;
for (j = 1; j <= t; j++)
{
k += pdi[j].second;
ans = max(ans, k);
}
}
return ans >= m;
}
int main()
{
// cout << time(NULL) << endl;
// freopen("tcial.in", "r", stdin);
// freopen("tcial.out", "w", stdout);
int T, I;
scanf("%d", &T);
for (I = 1; I <= T; I++)
{
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) poi[i].input();
scanf("%d", &R);
if (m > n)
{
printf("The cake is a lie.\n");
continue;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
dis[i][j] = (poi[j] - poi[i]).norm(), ang[i][j] = (poi[j] - poi[i]).angl();
l = R;
r = 30000;
while (r - l > eps)
{
mid = (l + r) / 2;
if (check(mid - R))
r = mid;
else
l = mid;
}
printf("%.6f\n", r);
}
// cout << time(NULL) << endl;
return 0;
}


by Infi

## G. Spare Tire

by ZGH

$a_n = k p^n + an^2 + bn + c$

$a_n = n^2 + n$

\begin{aligned} &\sum_{i=1}^{n} [\gcd(i, m)=1] (i^2 + i)\\ =&\sum_{d|m \land d \leq n} \mu(d) \cdot \sum_{t=1}^{[\frac{n}{d}]} ((td)^2 + td)\\ =&\sum_{d|m \land d \leq n} \mu(d) \cdot \left( d^2 \cdot \sum_{t=1}^{[\frac{n}{d}]} t^2 + d\cdot \sum_{t=1}^{[\frac{n}{d}]} t \right) \end{aligned}

## H. Hamming Weight

by Asm.Def

$N = \sum_{i=0}^{n-1} A_i 2^i,\ A_i \in \{0,1\}$

$Ans(N) = \sum_{i=0}^{n-1}\left[A_i \cdot (1 +\sum_{j=0}^{i-1}A_j\cdot 2^j )+ 2^i \cdot \sum_{j=i+1}^{n-1} A_j\cdot 2^{j-i-1} \right]^2$

$Ans_{[L,R)}(x)=\sum_{i=L}^{R-1} \left[A_i \cdot (1 +\sum_{j=L}^{i-1}A_j\cdot x^{j-L} )+ x^{i-L} \cdot \sum_{j=i+1}^{R-1} A_j\cdot x^{j-i-1} \right]^2$

\begin{aligned} &Ans_{[L,R)}(x)\\ =&\sum_{i=L}^{R-1} \left[A_i^2 + 2A_i(A_i \cdot \sum_{j=L}^{i-1}A_j\cdot x^{j-L} + x^{i-L} \cdot \sum_{j=i+1}^{R-1} A_j\cdot x^{j-i-1}) \\+ (A_i \cdot \sum_{j=L}^{i-1}A_j\cdot x^{j-L} + x^{i-L} \cdot \sum_{j=i+1}^{R-1} A_j\cdot x^{j-i-1})^2 \right] \end{aligned}

\begin{aligned} &Ans_{[L,R)}(x)\\ =&\sum_{i=L}^{R-1} A_i + 2 \sum_{i=L}^{R-1} A_i \cdot \left( \sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} \right) + \sum_{i=L}^{R-1} \left(A_i\sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} \right)^2 \end{aligned}

$\begin{array}{ccl} &S(i)&=&\sum\limits_{j=i}^{n-1} A_i\\ &F_{[L,R)}(x)&=&\sum\limits_{i=L}^{R-1}A_i x^i\\ &Ans1_{[L,R)}(x)&=&\sum\limits_{i=L}^{R-1} A_i \cdot \left( \sum\limits_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum\limits_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} \right)\\ &Ans2_{[L,R)}(x)&= &\sum\limits_{i=L}^{R-1} \left(A_i\sum\limits_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum\limits_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} \right)^2 \end{array}$

$Ans_{[L,R)}(x) = S(L)-S(R) + 2Ans1_{[L,R)}(x) + Ans2_{[L,R)}(x)$

\begin{aligned} Ans1_{[L,R)}(x)=&\sum_{i=L}^{R-1} A_i \cdot \left( \sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} \right)\\ =&\sum_{i=L}^{mid-1} A_i \cdot \left( \sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{mid-1} A_j\cdot x^{j-L-1} + \sum_{j=mid}^{R-1} A_j \cdot x^{j-L-1} \right)\\ &+ \sum_{i=mid}^{R-1} A_i \cdot \left( \sum_{j=mid}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} + \sum_{j=L}^{mid-1} x^{j-L} \right)\\ =&Ans1_{[L,mid)}(x) + Ans1_{[mid,R)}(x)\cdot x^{mid-L}\\ &+ \left(\sum_{i=L}^{mid-1} A_i \right) \cdot \left( \sum_{j=mid}^{R-1} A_j \cdot x^{j-L-1} \right)\cdot + \left(\sum_{i=mid}^{R-1} A_i \right) \cdot \left( \sum_{j=L}^{mid-1} A_j \cdot x^{j-L} \right)\\ =&Ans1_{[L,mid)}(x) + Ans1_{[mid,R)}(x)\cdot x^{mid-L} \\&+\sum_{j=mid}^{R-1}(S(L)-S(mid))\cdot A_j x^{j-L-1}+\sum_{j=L}^{mid-1}(S(mid)-S(R))\cdot A_j x^{j-L} \end{aligned}

\begin{aligned} Ans2_{[L,R)}(x)=&\sum_{i=L}^{R-1} \left(A_i\sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} \right)^2\\ =&\sum_{i=L}^{mid-1} \left(A_i\sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{mid-1} A_j\cdot x^{j-L-1} + \sum_{j=mid}^{R-1}A_j\cdot x^{j-L-1} \right)^2 \\&+ \sum_{i=mid}^{R-1} \left(A_i\sum_{j=mid}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{R-1} A_j\cdot x^{j-L-1} + A_i\sum_{j=L}^{mid-1} A_j\cdot x^{j-L} \right)^2\\ =&Ans2_{[L,mid)}(x) + Ans2_{[mid, R)}(x) \cdot x^{2(mid-L)}\\ &+2\sum_{i=L}^{mid-1} \left(A_i\sum_{j=L}^{i-1}A_j\cdot x^{j-L} + \sum_{j=i+1}^{mid-1}A_j\cdot x^{j-L-1} \right)\cdot F_{[mid,R)}(x) x^{mid-L-1} \\ &+2\sum_{i=mid}^{R-1} A_i \left( \sum_{j=mid}^{i-1}A_j\cdot x^{j-mid} + \sum_{j=i+1}^{R-1}A_j\cdot x^{j-mid-1} \right) \cdot x^{mid-L} \cdot F_{[L,mid)}(x)\\ &+F_{[mid,R)}(x)^2\cdot (mid-L) \cdot x^{2(mid-L-1)}+F_{[L,mid)}(x)^2\cdot [S(mid) - S(R)] \end{aligned}

$Ans_{[0,n)}(2)​$分治求解，总复杂度$O(n\log^2 n)​$.

//Asm.Def
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005, mod = 998244353, g = 3, maxk = 1 << 19;
typedef long long LL;
typedef vector<int> Poly;
int A[maxn], N, Sum[maxn];

void Print(const Poly &ans)
{
for(int i = ans.size()-1;i >= 0;--i) printf("%d ", ans[i]);
puts("");
}

int powmod(int a, int n)
{
int ans = 1;
while(n)
{
if(n & 1) ans = (LL) ans * a % mod;
a = (LL) a * a % mod;
n >>= 1;
}
return ans;
}

void Carry(Poly &x)
{
int C = 0, c;
while(x.size() > 1)
{
if(x.back() == 0) x.pop_back();
else break;
}

for(int i = 0;i < x.size() || C;++i)
{
if(i < x.size())
{
x[i] += C;
C = x[i] >> 1;
x[i] = x[i] & 1;
}
else
{
x.push_back(C & 1);
C >>= 1;
}
}
}

//Poly operator * (const Poly &a, const Poly &b)
//{
//	Poly ans(a.size() + b.size() - 1);
//	for(int i = 0;i < (int) a.size();++i)
//		for(int j = 0;j < (int) b.size();++j)
//			ans[i+j] += a[i] * b[j];
//	Carry(ans);
//	return ans;
//}

void Shuff(int A[], int n)//n为位数
{
static int B[maxk];
int N = 1 << n, j, mx = 1 << (n-1);
for(int i = 0, it = 0;i < N;++i)
{
B[i] = A[it];
j = mx;
while(it & j)
{
it ^= j;
j >>= 1;
}
it ^= j;
}
for(int i = 0;i < N;++i) A[i] = B[i];
}

void DFT(Poly &A, int n, int a)//n为位数
{
static int B[maxk], pw[20];
int N = 1 << n;
pw[n-1] = a;
for(int i = n-1;i;--i) pw[i-1] = (LL) pw[i] * pw[i] % mod;
A.resize(N);
for(int i = 0;i < N;++i) B[i] = A[i];
Shuff(B, n);
for(int i = 0;i < n;++i)
{
int d = (1 << i), x0 = pw[i];
for(int j = 0;j < N;j += (d<<1))
{
for(int k = j, x = 1;k < j+d;++k)
{
int t = (LL) x * B[k+d] % mod;
B[k+d] = (B[k] + mod - t) % mod;
B[k] = (B[k] + t) % mod;
x = (LL) x * x0 % mod;
}
}
}
for(int i = 0;i < N;++i) A[i] = B[i];
//Print(A);
}

Poly operator * (const Poly &x, const Poly &y)
{
static Poly A, B;
A = x, B = y;
while(A.size() > 1)
{
if(A.back() == 0) A.pop_back();
else break;
}
while(B.size() > 1)
{
if(B.back() == 0) B.pop_back();
else break;
}
int n = 0;
while((1<<n) < A.size() + B.size()) ++n;

int a = powmod(g, (mod-1) >> n);
DFT(A, n, a);
DFT(B, n, a);

for(int i = 0;i < (1 << n);++i) A[i] = (LL) A[i] * B[i] % mod;
DFT(A, n, powmod(a, mod-2));
int t = powmod((1 << n), mod-2);
for(int i = 0;i < (1 << n);++i) A[i] = (LL) A[i] * t % mod;

//Print(A);
Carry(A);
return A;
}

Poly operator * (const Poly &a, int x)
{
Poly ans(a.size());
for(int i = 0;i < (int) a.size();++i)
ans[i] = a[i] * x;
Carry(ans);
return ans;
}

Poly operator + (const Poly &a, const Poly &b)
{
Poly ans(max(a.size(), b.size()));
for(int i = 0;i < (int) ans.size();++i)
{
ans[i] = 0;
if(i < (int) a.size()) ans[i] += a[i];
if(i < (int) b.size()) ans[i] += b[i];
}
return ans;
}

Poly operator << (const Poly &x, int n)
{
Poly ans(x.size() + n, 0);
for(int i = 0;i < x.size();++i) ans[n+i] = x[i];
return ans;
}

//void Multi(const Poly &a, const Poly &b, Poly &ans)
//{
//	ans.resize(a.size() + b.size() - 1);
//	for(int i = 0;i < (int) a.size();++i)
//		for(int j = 0;j < (int) b.size();++j)
//			ans[i+j] += a[i] * b[j];
//}

void init()
{
for(int i = 1;i <= N;++i) scanf("%1d", &A[N-i]);
Sum[N] = 0;
for(int i = N-1;i >= 0;--i)
Sum[i] = Sum[i+1] + A[i];
}

void Solve(int L, int R, Poly &Ans1, Poly &Ans2)
{
static Poly SL, SR, AL, AR;
if(R - L == 1)
{
Ans1.resize(1);Ans1[0] = 0;
Ans2.resize(1);Ans2[0] = 0;
return;
}
int mid = (L + R) >> 1;
Poly Ans1L, Ans1R, Ans2L, Ans2R;
Solve(L, mid, Ans1L, Ans2L);
Solve(mid, R, Ans1R, Ans2R);
//printf("Solve (%d,%d)\n", L, R);
//
//Print(Ans1L);
//Print(Ans1R);
//Print(Ans2L);
//Print(Ans2R);
//puts("");

AL.clear();
AL.resize(mid-L);
for(int i = L;i < mid;++i) AL[i-L] = A[i];

AR.clear();
AR.resize(R-mid);
for(int i = mid;i < R;++i) AR[i-mid] = A[i];

SL.clear();
SL.resize(mid-L);
for(int i = L;i < mid-1;++i)
SL[i-L] = A[i] * (Sum[i+1]-Sum[mid]) + A[i+1] * (i+1-L);

SR.clear();
SR.resize(R-mid);
for(int i = mid;i < R-1;++i)
SR[i-mid] = A[i] * (Sum[i+1]-Sum[R]) + A[i+1] * (Sum[mid]-Sum[i+1]);

//puts("");
//Print(AL);
//Print(AR);
//Print(SL);
//Print(SR);

Ans2 = Ans2L + ((AR * SL) << (mid-L)) + ((AR * AR * (mid - L)) << (2 * (mid-L-1))) + (Ans2R << (2 * (mid-L))) + ((AL * SR) << (mid-L+1)) + AL * AL * (Sum[mid]-Sum[R]);
Carry(Ans2);

Ans1 = Ans1L + (Ans1R << (mid-L));
if((int) Ans1.size() < (R-L-1))
Ans1.resize(R-L-1);
for(int i = L;i < mid;++i)
Ans1[i-L] += A[i] * (Sum[mid] - Sum[R]);
for(int i = mid;i < R;++i)
Ans1[i-L-1] += A[i] * (Sum[L] - Sum[mid]);
Carry(Ans1);
//Print(Ans1);
//Print(Ans2);
}

void work()
{
Poly ans1, ans2;
Solve(0, N, ans1, ans2);
//Print(ans1), Print(ans2);
ans2 = ans2 + (ans1 << 1);
ans2[0] += (Sum[0] - Sum[N]);
Carry(ans2);
int sum = 0;
for(int i = ans2.size()-1;i >= 0;--i) printf("%d", ans2[i]);
puts("");
//for(int i = 0;i < (int) ans.size();++i)
//	sum += ans[i];
//printf("%d\n", sum % 1000000007);
}

int main()
{
time_t beg = clock();
while(~scanf("%d", &N))
{
init();
work();
}
//printf("%.2f sec", double(clock() - beg) / CLOCKS_PER_SEC);
return 0;
}


by Joker

by bird_14

## K. Supreme Number

by morejarphone

posted @ 2018-09-08 21:26  Asm.Definer  阅读(1570)  评论(0编辑  收藏  举报