# 题解(按照题目通过顺序)

## B - Fraction (cy)

00:07:54 通过 (1次提交)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <complex>
#include <queue>
#include <algorithm>
#include <string>
#include <stack>
#include <bitset>
#include <cmath>
#include <set>

int N = 1e6, SZ = 320, INF = 1 << 29;
long long LINF = (1LL << 61), mod = 1e9 + 7;
const long double eps = 1e-9, PI = acos(-1.0);

#define lowbit(x) (x & (-(x)))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define rp(a, b, c) for (int a = b; a <= c; ++a)
#define RP(a, b, c) for (int a = b; a < c; ++a)
#define lp(a, b, c) for (int a = b; a >= c; --a)
#define LP(a, b, c) for (int a = b; a > c; --a)
#define rps(i, s) for (int i = 0; s[i]; i++)
#define fson(u) for (int i = g[u]; ~i; i = edg[i].nxt)
#define adde(u, v) edg[++ecnt] = Edge(u, v, 0, g[u]), g[u] = ecnt
#define addew(u, v, w) edg[++ecnt] = Edge(u, v, w, g[u]), g[u] = ecnt
#define MID (l + r >> 1)
#define mst(a, v) memset(a, v, sizeof(a))
#define bg(x)            \
Edge edg[maxn << x]; \
int g[maxn], ecnt
#define ex(v)  \
cout << v; \
return 0
#define debug(x) cout << "debug: " << x << endl;
#define sqr(x) ((x) * (x))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cpx;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v)
{
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
v = v * 10 + READ_DATA - '0';
v *= SIGNAL_INPUT;
return v;
}
inline ll modru(ll &v)
{
ll p = 0;
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
p = v = READ_DATA - '0';
{
v = (v * 10 + READ_DATA - '0') % mod;
p = (p * 10 + READ_DATA - '0') % (mod - 1);
}
v *= SIGNAL_INPUT;
return p;
}
template <typename A, typename B>
inline int ru(A &x, B &y)
{
if (ru(x) == EOF)
return EOF;
ru(y);
return 2;
}
template <typename A, typename B, typename C>
inline int ru(A &x, B &y, C &z)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
return 3;
}
template <typename A, typename B, typename C, typename D>
inline int ru(A &x, B &y, C &z, D &w)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
ru(w);
return 4;
}
inline ll gcd(ll a, ll b)
{
while (b)
{
a %= b;
swap(a, b);
}
return a;
}

inline ll fastmul(ll a, ll b, ll mod = 1e9 + 7)
{
return (a * b - (ll)((long double)a * b / mod) * mod + mod) % mod;
}

inline ll dirmul(ll a, ll b, ll mod = 1e9 + 7)
{
return a * b % mod;
}

inline ll ss(ll a, ll b, ll mod = 1e9 + 7, ll(*mul)(ll, ll, ll) = dirmul)
{
if (b < 0)
{
b = -b;
a = ss(a, mod - 2, mod);
}
ll ans = 1;
while (b)
{
if (b & 1)
ans = mul(ans, a, mod);
a = mul(a, a, mod);
b >>= 1;
}
return ans;
}
inline int isprime(ll n)
{
if (n == 1)
return 0;

for (ll d = 2; d * d <= n; ++d)
{
if (n % d == 0)
return 0;
}

return 1;
}

template <typename Type>
void brc(Type *a, int n)
{
int k;
for (int i = 1, j = n / 2; i < n - 1; i++)
{
if (i < j)
swap(a[i], a[j]);

k = n >> 1;
while (j >= k)
{
j ^= k;
k >>= 1;
}
if (j < k)
j ^= k;
}
}
void fft(cpx *a, int n, int inv = 1)
{
cpx u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
cpx wn(cos(inv * 2.0 * PI / h), sin(inv * 2.0 * PI / h));
for (int j = 0; j < n; j += h)
{
cpx w(1, 0);
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)];
a[k] = u + t;
a[k + (h >> 1)] = u - t;
w *= wn;
}
}
}
if (inv == -1)
RP(i, 0, n)
a[i] /= n;
}
void ntt(ll *a, int n, int inv = 1)
{
ll u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
ll wn = ss(3, (mod - 1) / h);
if (inv == -1)
wn = ss(wn, mod - 2);
for (int j = 0; j < n; j += h)
{
ll w = 1;
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)] % mod;
a[k] = (u + t) % mod;
a[k + (h >> 1)] = (u - t + mod) % mod;
(w *= wn) %= mod;
}
}
}
if (inv == -1)
{
ll tmp = ss(n, mod - 2);
RP(i, 0, n)
(a[i] *= tmp) %= mod;
}
}
struct Edge
{
int u, v, nxt;
//ll w;
Edge(int _u = 0, int _v = 0, /*ll _w = 0,*/ int _nxt = 0)
{
u = _u;
v = _v;
//w = _w;
nxt = _nxt;
}

/*int operator<(const Edge &b) const
{
return w < b.w;
}*/
};
struct CMP
{
int operator()(const int &a, const int &b) const
{
return a > b;
}
};

const int maxn = 1e2+ 5;
/*------------------------------------------------------------------------yah01------------------------------------------------------------------------*/

int n, a[maxn], b[maxn];

pll f(int i)
{
if (i == n)
{
ll d = gcd(a[n], b[n]);
return pll(b[n] / d, a[n] / d);
}

pll t = f(i + 1);
ll x = b[i] * t.second, y = a[i] * t.second + t.first;
ll d = gcd(x, y);
return pll(x / d, y / d);
}

int main()
{
int T;
ru(T);
rp(t,1,T)
{
ru(n);
rp(i, 1, n) ru(a[i]);
rp(i, 1, n) ru(b[i]);

pll ans = f(1);
printf("Case #%d: %lld %lld\n", t,ans.first, ans.second);
}
return 0;
}

/*
5
2 4 6 8 10
-1 2 2 2
1000
+ 1 10
+ 1 -2
+ 1 1
*/


## D - Triangle (jmx)

00:19:22 通过（一次提交）

#include <bits/stdc++.h>
#define ll long long
#define inf 0x6fffffff
using namespace std;
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int mx=(1<<20);
int f[30],a[30];
int main()
{
for(int op=0;op<mx;op++)
{
int cnt=0;
for(int i=0;i<20;i++)
if(((1<<i)&op)>0)a[++cnt]=i+1;
if(cnt<=2)
{
for(int i=a[cnt];i<=20;i++)f[i]=max(cnt,f[i]);
}
else{
bool flag=0;
for(int i=1;i<=cnt-2;i++)
{
if(a[i]+a[i+1]>a[i+2]){flag=1;break;}
}
if(!flag)
for(int i=a[cnt];i<=20;i++)f[i]=max(cnt,f[i]);
}
}
for(int i=1;i<=20;i++)f[i]=i-f[i];
for(int cas=1;cas<=T;cas++)
{
printf("Case #%d: %d\n",cas,f[n]);
}
return 0;
}


## F - Harmonic Value Description (jmx)

00:31:04 通过（1次提交）

1. $P_i,P_{i-1}$一个是奇数一个是偶数时，$gcd(P_i,P_{i-1})=1$
2. $P_i,P_{i-1}$为连续的奇数时，$gcd(P_i,P_{i-1})=1$
3. $P_i,P_{i-1}$为连续的偶数时，$gcd(P_i,P_{i-1})=2$

#include <bits/stdc++.h>
#define ll long long
#define inf 0x6fffffff
using namespace std;
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int mx=(1<<20);
int f[30],a[30];
int main()
{
for(int op=0;op<mx;op++)
{
int cnt=0;
for(int i=0;i<20;i++)
if(((1<<i)&op)>0)a[++cnt]=i+1;
if(cnt<=2)
{
for(int i=a[cnt];i<=20;i++)f[i]=max(cnt,f[i]);
}
else{
bool flag=0;
for(int i=1;i<=cnt-2;i++)
{
if(a[i]+a[i+1]>a[i+2]){flag=1;break;}
}
if(!flag)
for(int i=a[cnt];i<=20;i++)f[i]=max(cnt,f[i]);
}
}
for(int i=1;i<=20;i++)f[i]=i-f[i];
for(int cas=1;cas<=T;cas++)
{
printf("Case #%d: %d\n",cas,f[n]);
}
return 0;
}



## H - Sequence I (cy)

01:03:06 通过（1次提交）

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <complex>
#include <queue>
#include <algorithm>
#include <string>
#include <stack>
#include <bitset>
#include <cmath>
#include <set>

int N = 1e6, SZ = 320, INF = 1 << 29;
long long LINF = (1LL << 61), mod = 1e9 + 7;
const long double eps = 1e-9, PI = acos(-1.0);

#define lowbit(x) (x & (-(x)))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define rp(a, b, c) for (int a = b; a <= c; ++a)
#define RP(a, b, c) for (int a = b; a < c; ++a)
#define lp(a, b, c) for (int a = b; a >= c; --a)
#define LP(a, b, c) for (int a = b; a > c; --a)
#define rps(i, s) for (int i = 0; s[i]; i++)
#define fson(u) for (int i = g[u]; ~i; i = edg[i].nxt)
#define adde(u, v) edg[++ecnt] = Edge(u, v, 0, g[u]), g[u] = ecnt
#define addew(u, v, w) edg[++ecnt] = Edge(u, v, w, g[u]), g[u] = ecnt
#define MID (l + r >> 1)
#define mst(a, v) memset(a, v, sizeof(a))
#define bg(x)            \
Edge edg[maxn << x]; \
int g[maxn], ecnt
#define ex(v)  \
cout << v; \
return 0
#define debug(x) cout << "debug: " << x << endl;
#define sqr(x) ((x) * (x))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cpx;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v)
{
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
v = v * 10 + READ_DATA - '0';
v *= SIGNAL_INPUT;
return v;
}
inline ll modru(ll &v)
{
ll p = 0;
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
p = v = READ_DATA - '0';
{
v = (v * 10 + READ_DATA - '0') % mod;
p = (p * 10 + READ_DATA - '0') % (mod - 1);
}
v *= SIGNAL_INPUT;
return p;
}
template <typename A, typename B>
inline int ru(A &x, B &y)
{
if (ru(x) == EOF)
return EOF;
ru(y);
return 2;
}
template <typename A, typename B, typename C>
inline int ru(A &x, B &y, C &z)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
return 3;
}
template <typename A, typename B, typename C, typename D>
inline int ru(A &x, B &y, C &z, D &w)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
ru(w);
return 4;
}
inline ll gcd(ll a, ll b)
{
while (b)
{
a %= b;
swap(a, b);
}
return a;
}

inline ll fastmul(ll a, ll b, ll mod = 1e9 + 7)
{
return (a * b - (ll)((long double)a * b / mod) * mod + mod) % mod;
}

inline ll dirmul(ll a, ll b, ll mod = 1e9 + 7)
{
return a * b % mod;
}

inline ll ss(ll a, ll b, ll mod = 1e9 + 7, ll(*mul)(ll, ll, ll) = dirmul)
{
if (b < 0)
{
b = -b;
a = ss(a, mod - 2, mod);
}
ll ans = 1;
while (b)
{
if (b & 1)
ans = mul(ans, a, mod);
a = mul(a, a, mod);
b >>= 1;
}
return ans;
}
inline int isprime(ll n)
{
if (n == 1)
return 0;

for (ll d = 2; d * d <= n; ++d)
{
if (n % d == 0)
return 0;
}

return 1;
}

template <typename Type>
void brc(Type *a, int n)
{
int k;
for (int i = 1, j = n / 2; i < n - 1; i++)
{
if (i < j)
swap(a[i], a[j]);

k = n >> 1;
while (j >= k)
{
j ^= k;
k >>= 1;
}
if (j < k)
j ^= k;
}
}
void fft(cpx *a, int n, int inv = 1)
{
cpx u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
cpx wn(cos(inv * 2.0 * PI / h), sin(inv * 2.0 * PI / h));
for (int j = 0; j < n; j += h)
{
cpx w(1, 0);
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)];
a[k] = u + t;
a[k + (h >> 1)] = u - t;
w *= wn;
}
}
}
if (inv == -1)
RP(i, 0, n)
a[i] /= n;
}
void ntt(ll *a, int n, int inv = 1)
{
ll u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
ll wn = ss(3, (mod - 1) / h);
if (inv == -1)
wn = ss(wn, mod - 2);
for (int j = 0; j < n; j += h)
{
ll w = 1;
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)] % mod;
a[k] = (u + t) % mod;
a[k + (h >> 1)] = (u - t + mod) % mod;
(w *= wn) %= mod;
}
}
}
if (inv == -1)
{
ll tmp = ss(n, mod - 2);
RP(i, 0, n)
(a[i] *= tmp) %= mod;
}
}
struct Edge
{
int u, v, nxt;
//ll w;
Edge(int _u = 0, int _v = 0, /*ll _w = 0,*/ int _nxt = 0)
{
u = _u;
v = _v;
//w = _w;
nxt = _nxt;
}

/*int operator<(const Edge &b) const
{
return w < b.w;
}*/
};
struct CMP
{
int operator()(const int &a, const int &b) const
{
return a > b;
}
};

const int maxn = 1e6+ 5;
/*------------------------------------------------------------------------yah01------------------------------------------------------------------------*/

int n, m, p;
int a[maxn], b[maxn];
int f[maxn];

void KMP()
{
int j;
f[0] = f[1] = 0;
RP(i, 1, m)
{
j = f[i];
while (j && b[i] != b[j]) j = f[j];
f[i + 1] = b[i] == b[j] ? j + 1 : 0;
}
}

int main()
{
int T;
ru(T);
rp(t,1,T)
{
ru(n, m, p);
RP(i, 0, n) ru(a[i]);
RP(i, 0, m) ru(b[i]);
KMP();

int j, ans = 0;
RP(s, 0, p)
{
j = 0;
for (int i = s; i < n; i += p)
{
while (j && a[i] != b[j]) j = f[j];
if (a[i] == b[j]) ++j;
if (j == m)
{
++ans;
j = f[j];
}
}
}

printf("Case #%d: %d\n", t, ans);
}
return 0;
}

/*
5
2 4 6 8 10
-1 2 2 2
1000
+ 1 10
+ 1 -2
+ 1 1
*/


## I - Sequence II (jmx)

02:03:53 通过（2次提交）

#include <bits/stdc++.h>
#define ll long long
#define inf 0x6fffffff
#define N 200086
using namespace std;
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
map<int,int>mp;
int a[N],pre[N],nxt[N],root[N];
int n,m;
int cnt;
struct node{int l,r,w;}f[N*80];
void ins(int &i,int l,int r,int ps,int dd)
{
f[++cnt]=f[i];i=cnt;f[cnt].w+=dd;
if(l==r)return;
int mid=(l+r)/2;
if(ps<=mid)ins(f[i].l,l,mid,ps,dd);else ins(f[i].r,mid+1,r,ps,dd);
}
int ask(int i,int l,int r,int lt,int rt)
{
if(lt<=l&&r<=rt)return f[i].w;
int mid=(l+r)/2,res=0;
return res;
}
int find(int i,int l,int r,int rk)
{
if(l==r)return l;
int mid=(l+r)/2,tmp=f[f[i].l].w;
if(tmp>=rk)return find(f[i].l,l,mid,rk);
else return find(f[i].r,mid+1,r,rk-tmp);

}
int main()
{
for(int cas=1;cas<=T;cas++)
{
printf("Case #%d:",cas);
cnt=0;mp.clear();
for(int i=1;i<=n+1;i++)root[i]=pre[i]=nxt[i]=0;
for(int i=1;i<=n;i++)
{
pre[i]=mp[a[i]];mp[a[i]]=i;
if(pre[i]!=0)nxt[pre[i]]=i;
}
for(int i=n;i;i--)
{
root[i]=root[i+1];
ins(root[i],1,n,i,1);
if(nxt[i]!=0)ins(root[i],1,n,nxt[i],-1);
}
int ans=0;
for(int i=1;i<=m;i++)
{
if(l>r)swap(l,r);
int rk=(k+1)/2;
ans=find(root[l],1,n,rk);
printf(" %d",ans);
}
puts("");
}
return 0;
}


## J - Ugly Problem (cy)

02:49:50 通过（5次提交）

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <complex>
#include <queue>
#include <algorithm>
#include <string>
#include <stack>
#include <bitset>
#include <cmath>
#include <set>

int N = 1e6, SZ = 320, INF = 1 << 29;
long long LINF = (1LL << 61), mod = 1e9 + 7;
const long double eps = 1e-9, PI = acos(-1.0);

#define lowbit(x) (x & (-(x)))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define rp(a, b, c) for (int a = b; a <= c; ++a)
#define RP(a, b, c) for (int a = b; a < c; ++a)
#define lp(a, b, c) for (int a = b; a >= c; --a)
#define LP(a, b, c) for (int a = b; a > c; --a)
#define rps(i, s) for (int i = 0; s[i]; i++)
#define fson(u) for (int i = g[u]; ~i; i = edg[i].nxt)
#define adde(u, v) edg[++ecnt] = Edge(u, v, 0, g[u]), g[u] = ecnt
#define addew(u, v, w) edg[++ecnt] = Edge(u, v, w, g[u]), g[u] = ecnt
#define MID (l + r >> 1)
#define mst(a, v) memset(a, v, sizeof(a))
#define bg(x)            \
Edge edg[maxn << x]; \
int g[maxn], ecnt
#define ex(v)  \
cout << v; \
return 0
#define debug(x) cout << "debug: " << x << endl;
#define sqr(x) ((x) * (x))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cpx;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v)
{
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
v = v * 10 + READ_DATA - '0';
v *= SIGNAL_INPUT;
return v;
}
inline ll modru(ll &v)
{
ll p = 0;
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
p = v = READ_DATA - '0';
{
v = (v * 10 + READ_DATA - '0') % mod;
p = (p * 10 + READ_DATA - '0') % (mod - 1);
}
v *= SIGNAL_INPUT;
return p;
}
template <typename A, typename B>
inline int ru(A &x, B &y)
{
if (ru(x) == EOF)
return EOF;
ru(y);
return 2;
}
template <typename A, typename B, typename C>
inline int ru(A &x, B &y, C &z)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
return 3;
}
template <typename A, typename B, typename C, typename D>
inline int ru(A &x, B &y, C &z, D &w)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
ru(w);
return 4;
}
inline ll gcd(ll a, ll b)
{
while (b)
{
a %= b;
swap(a, b);
}
return a;
}

inline ll fastmul(ll a, ll b, ll mod = 1e9 + 7)
{
return (a * b - (ll)((long double)a * b / mod) * mod + mod) % mod;
}

inline ll dirmul(ll a, ll b, ll mod = 1e9 + 7)
{
return a * b % mod;
}

inline ll ss(ll a, ll b, ll mod = 1e9 + 7, ll(*mul)(ll, ll, ll) = dirmul)
{
if (b < 0)
{
b = -b;
a = ss(a, mod - 2, mod);
}
ll ans = 1;
while (b)
{
if (b & 1)
ans = mul(ans, a, mod);
a = mul(a, a, mod);
b >>= 1;
}
return ans;
}
inline int isprime(ll n)
{
if (n == 1)
return 0;

for (ll d = 2; d * d <= n; ++d)
{
if (n % d == 0)
return 0;
}

return 1;
}

template <typename Type>
void brc(Type *a, int n)
{
int k;
for (int i = 1, j = n / 2; i < n - 1; i++)
{
if (i < j)
swap(a[i], a[j]);

k = n >> 1;
while (j >= k)
{
j ^= k;
k >>= 1;
}
if (j < k)
j ^= k;
}
}
void fft(cpx *a, int n, int inv = 1)
{
cpx u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
cpx wn(cos(inv * 2.0 * PI / h), sin(inv * 2.0 * PI / h));
for (int j = 0; j < n; j += h)
{
cpx w(1, 0);
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)];
a[k] = u + t;
a[k + (h >> 1)] = u - t;
w *= wn;
}
}
}
if (inv == -1)
RP(i, 0, n)
a[i] /= n;
}
void ntt(ll *a, int n, int inv = 1)
{
ll u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
ll wn = ss(3, (mod - 1) / h);
if (inv == -1)
wn = ss(wn, mod - 2);
for (int j = 0; j < n; j += h)
{
ll w = 1;
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)] % mod;
a[k] = (u + t) % mod;
a[k + (h >> 1)] = (u - t + mod) % mod;
(w *= wn) %= mod;
}
}
}
if (inv == -1)
{
ll tmp = ss(n, mod - 2);
RP(i, 0, n)
(a[i] *= tmp) %= mod;
}
}
struct Edge
{
int u, v, nxt;
//ll w;
Edge(int _u = 0, int _v = 0, /*ll _w = 0,*/ int _nxt = 0)
{
u = _u;
v = _v;
//w = _w;
nxt = _nxt;
}

/*int operator<(const Edge &b) const
{
return w < b.w;
}*/
};
struct CMP
{
int operator()(const int &a, const int &b) const
{
return a > b;
}
};

const int maxn = 1e3 + 5;
/*------------------------------------------------------------------------yah01------------------------------------------------------------------------*/

int n;
char s[maxn];
vector<string> ans;
int main()
{
int T;
ru(T);
rp(t, 1, T)
{
//srand(47);
scanf("%s", s + 1);
n = strlen(s + 1);
//n = 1000;
//rp(i, 2, n) s[i] = rand() % 10 + '0';
//s[1] = rand() % 9 + 1 + '0';
reverse(s + 1, s + n + 1);
rp(i, 1, n)
{
s[i] = s[i] - '0';
}
printf("Case #%d:\n", t);

ans.clear();
string now;
int x, cnt = 0;
while (n)
{
int m = n / 2;
int choice = 0;
if (1)
{
if (n>=2 && s[n / 2 + 1] == 0)
{
now.clear();
rp(i, 1, n / 2 + 1) now.push_back('1');
}
else
{
rp(i, 1, m)
{
if (s[i] < s[n - i + 1]) break;
else if (s[i] > s[n - i + 1])
{
choice = 1;
break;
}
}

now.clear();
choice = 0;
if (n & 1)
{
if (n == 1)
{
now.push_back(s[1]+'0');
}
else
{
rp(i, 1, m) now.push_back(s[n - i + 1] + '0');
now.push_back(s[n / 2 + 1] - 1 + '0');
lp(i, m - 1, 0) now.push_back(now[i]);
}
}
else
{
if (n == 2)
{
int x = min(s[1], s[2]);
if (x)
{
now.push_back(x + '0');
now.push_back(x + '0');
}
else
{
now.push_back('1');
}
}
else
{
rp(i, 1, m - 1) now.push_back(s[n-i+1] + '0');
now.push_back(s[m + 1] - 1 + '0');
//now.push_back(s[m + 1] - 1 + '0');
lp(i, m-1, 0) now.push_back(now[i]);
}
}

}
}
else
{
now.clear();
now.push_back(10 - s[n] + '0');
}

rp(i, 1, n)
{
if(i-1<now.length())
s[i] -= now[i - 1] - '0';
while (s[i] < 0)
{
s[i] += 10;
s[i + 1]--;
}
}

while (n&&s[n] == 0) n--;
ans.push_back(now);
}

int tot = ans.size();
printf("%d\n", tot);
RP(i, 0, tot)
{
int m = ans[i].length();
RP(j, 0, m) printf("%c", ans[i][j]);
putchar('\n');
}
}
return 0;
}

/*
5
2 4 6 8 10
-1 2 2 2
1000
+ 1 10
+ 1 -2
+ 1 1
*/


## G - Instability (cy&jmx)

03:28:07 通过（3次提交）

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <map>
#include <complex>
#include <queue>
#include <algorithm>
#include <string>
#include <stack>
#include <bitset>
#include <cmath>
#include <set>

int N = 1e6, SZ = 320, INF = 1 << 29;
long long LINF = (1LL << 61), mod = 1e9 + 7;
const long double eps = 1e-9, PI = acos(-1.0);

#define lowbit(x) (x & (-(x)))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define rp(a, b, c) for (int a = b; a <= c; ++a)
#define RP(a, b, c) for (int a = b; a < c; ++a)
#define lp(a, b, c) for (int a = b; a >= c; --a)
#define LP(a, b, c) for (int a = b; a > c; --a)
#define rps(i, s) for (int i = 0; s[i]; i++)
#define fson(u) for (int i = g[u]; ~i; i = edg[i].nxt)
#define adde(u, v) edg[++ecnt] = Edge(u, v, 0, g[u]), g[u] = ecnt
#define addew(u, v, w) edg[++ecnt] = Edge(u, v, w, g[u]), g[u] = ecnt
#define MID (l + r >> 1)
#define mst(a, v) memset(a, v, sizeof(a))
#define bg(x)            \
Edge edg[maxn << x]; \
int g[maxn], ecnt
#define ex(v)  \
cout << v; \
return 0
#define debug(x) cout << "debug: " << x << endl;
#define sqr(x) ((x) * (x))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cpx;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v)
{
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
v = v * 10 + READ_DATA - '0';
v *= SIGNAL_INPUT;
return v;
}
inline ll modru(ll &v)
{
ll p = 0;
SIGNAL_INPUT = 1;
SIGNAL_INPUT = -1;
return EOF;
p = v = READ_DATA - '0';
{
v = (v * 10 + READ_DATA - '0') % mod;
p = (p * 10 + READ_DATA - '0') % (mod - 1);
}
v *= SIGNAL_INPUT;
return p;
}
template <typename A, typename B>
inline int ru(A &x, B &y)
{
if (ru(x) == EOF)
return EOF;
ru(y);
return 2;
}
template <typename A, typename B, typename C>
inline int ru(A &x, B &y, C &z)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
return 3;
}
template <typename A, typename B, typename C, typename D>
inline int ru(A &x, B &y, C &z, D &w)
{
if (ru(x) == EOF)
return EOF;
ru(y);
ru(z);
ru(w);
return 4;
}
inline ll gcd(ll a, ll b)
{
while (b)
{
a %= b;
swap(a, b);
}
return a;
}

inline ll fastmul(ll a, ll b, ll mod = 1e9 + 7)
{
return (a * b - (ll)((long double)a * b / mod) * mod + mod) % mod;
}

inline ll dirmul(ll a, ll b, ll mod = 1e9 + 7)
{
return a * b % mod;
}

inline ll ss(ll a, ll b, ll mod = 1e9 + 7, ll(*mul)(ll, ll, ll) = dirmul)
{
if (b < 0)
{
b = -b;
a = ss(a, mod - 2, mod);
}
ll ans = 1;
while (b)
{
if (b & 1)
ans = mul(ans, a, mod);
a = mul(a, a, mod);
b >>= 1;
}
return ans;
}
inline int isprime(ll n)
{
if (n == 1)
return 0;

for (ll d = 2; d * d <= n; ++d)
{
if (n % d == 0)
return 0;
}

return 1;
}

template <typename Type>
void brc(Type *a, int n)
{
int k;
for (int i = 1, j = n / 2; i < n - 1; i++)
{
if (i < j)
swap(a[i], a[j]);

k = n >> 1;
while (j >= k)
{
j ^= k;
k >>= 1;
}
if (j < k)
j ^= k;
}
}
void fft(cpx *a, int n, int inv = 1)
{
cpx u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
cpx wn(cos(inv * 2.0 * PI / h), sin(inv * 2.0 * PI / h));
for (int j = 0; j < n; j += h)
{
cpx w(1, 0);
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)];
a[k] = u + t;
a[k + (h >> 1)] = u - t;
w *= wn;
}
}
}
if (inv == -1)
RP(i, 0, n)
a[i] /= n;
}
void ntt(ll *a, int n, int inv = 1)
{
ll u, t;
brc(a, n);
for (int h = 2; h <= n; h <<= 1)
{
ll wn = ss(3, (mod - 1) / h);
if (inv == -1)
wn = ss(wn, mod - 2);
for (int j = 0; j < n; j += h)
{
ll w = 1;
for (int k = j; k < j + (h >> 1); k++)
{
u = a[k];
t = w * a[k + (h >> 1)] % mod;
a[k] = (u + t) % mod;
a[k + (h >> 1)] = (u - t + mod) % mod;
(w *= wn) %= mod;
}
}
}
if (inv == -1)
{
ll tmp = ss(n, mod - 2);
RP(i, 0, n)
(a[i] *= tmp) %= mod;
}
}
struct Edge
{
int u, v, nxt;
//ll w;
Edge(int _u = 0, int _v = 0, /*ll _w = 0,*/ int _nxt = 0)
{
u = _u;
v = _v;
//w = _w;
nxt = _nxt;
}

/*int operator<(const Edge &b) const
{
return w < b.w;
}*/
};
struct CMP
{
int operator()(const int &a, const int &b) const
{
return a > b;
}
};

const int maxn = 1e3 + 5;
/*------------------------------------------------------------------------yah01------------------------------------------------------------------------*/

int n, m;
int f[51][51], g[51][51][51];
int main()
{
int T;
ru(T);
rp(t, 1, T)
{
mst(f, 0);
mst(g, 0);
ru(n, m);
while (m--)
{
int u, v;
ru(u, v);
f[u][v] = f[v][u] = 1;
}

ll ans = 0, tot = ss(2, n);
tot -= (1 + n + n * (n - 1)*ss(2,mod-2)) % mod;
if (tot < 0) tot += mod;

rp(i, 1, n)
{
rp(j, i + 1, n)
{
rp(k, j + 1, n)
{
if ((f[i][j] && f[i][k] && f[j][k]) || (!f[i][j] && !f[i][k] && !f[j][k]))
{
++ans;
g[i][j][k] = 1;
}
--tot;
if (tot < 0) tot += mod;
}
}
}

rp(i, 1, n)
{
rp(j, i + 1, n)
{
rp(k, j + 1, n)
{
rp(p, k + 1, n)
{
if (g[i][j][k] || g[i][j][p] || g[j][k][p] || g[i][k][p])
{
++ans;
if (ans >= mod) ans -= mod;
}
--tot;
if (tot < 0) tot += mod;
}
}
}
}

rp(i, 1, n)
{
rp(j, i + 1, n)
{
rp(k, j + 1, n)
{
rp(p, k + 1, n)
{
rp(q, p + 1, n)
{
if (g[i][j][k] || g[i][j][p] || g[i][j][q] || g[i][k][p]||g[i][k][q] || g[i][p][q] || g[j][k][p] || g[j][k][q] || g[j][p][q] || g[k][p][q])
{
++ans;
if (ans >= mod) ans -= mod;
}
--tot;
if (tot < 0) tot += mod;
}
}
}
}
}

ans += tot;
ans %= mod;
printf("Case #%d: %lld\n", t, ans);
}
return 0;
}

/*
5
2 4 6 8 10
-1 2 2 2
1000
+ 1 10
+ 1 -2
+ 1 1
*/


## E - The Fastest Runner Ms. Zhang (jmx赛后补题)

1. 如果$(S,T)$在同一颗子树中，那么答案就是$2n-len-dis_{S,T}$
2. 如果$(S,T)$不在同一颗子树中，令$S$和$T$所在的树的根分别为$U$和$V$，那么答案就是$2n-mxdep_S-mxdep_T-dis_{U,V}(取环上较长的路径)-2$

#include <bits/stdc++.h>
#define ll long long
#define inf 0x6fffffff
#define N 200086
using namespace std;
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,ans,ansx,ansy;
int q[N*2],nxt[N*2];
int p[N];
int tt;
bool flag;
int len;
int l[N];
int f[N*4];
void clr()
{
ans=tt=flag=len=0;
}
void findcircle(int i,int pre)
{
vis[i]=1;p[i]=pre;
if(q[j]!=pre)
{
if(vis[q[j]])
{
for(int x=i;x!=q[j];x=p[x]){inl[x]=1;l[++len]=x;}
l[++len]=q[j];inl[q[j]]=1;
flag=1;return;
}
else findcircle(q[j],i);
if(flag)return;
}
}
void update(int tmp,int x,int y)
{
if(x>y)swap(x,y);
if(tmp<ans)return;
if(tmp>ans)
{
ans=tmp;ansx=x;ansy=y;
}
else
{
if(x>ansx)return;
if(x<ansx)
{
ansx=x;ansy=y;
}
else{
if(y<ansy)ansy=y;
}
}
}
void dfs(int i,int ro,int pre)
{
mxdep[i]=i;
int id=i;
if(!inl[q[j]]&&q[j]!=pre)
{
dep[q[j]]=dep[i]+1;dfs(q[j],ro,i);
int tmp=dep[id]+dep[mxdep[q[j]]]-2*dep[i];
update(tmp+len,mxdep[q[j]],id);
if(dep[mxdep[q[j]]]>dep[id]||(dep[mxdep[q[j]]]==dep[id]&&mxdep[q[j]]<id))id=mxdep[q[j]];
}
mxdep[i]=id;
}
int findmn(int a,int b)
{
if(a==0||b==0)return a+b;
if(v[a]<v[b])return a;
if(v[a]>v[b])return b;
if(mxdep[l[a]]<mxdep[l[b]])return a;else return b;
}
void build(int i,int l,int r)
{
if(l==r){f[i]=l;return;}
int mid=(l+r)/2;
build(i*2,l,mid);build(i*2+1,mid+1,r);
f[i]=findmn(f[i*2],f[i*2+1]);
}
int ask(int i,int l,int r,int lt,int rt)
{
if(lt<=l&&r<=rt)return f[i];
int mid=(l+r)/2,res=0;
return res;
}
int main()
{
for(int cas=1;cas<=T;cas++)
{
clr();
for(int i=1;i<=n;i++)
{
ins(u,v);ins(v,u);
}
findcircle(1,0);
for(int i=1;i<=n;i++)
if(inl[i]){mxdep[i]=i;dfs(i,i,0);}
//for(int i=1;i<=len;i++)printf("%d %d\n",l[i],mxdep[l[i]]);
for(int i=1;i<=len;i++)l[i+len]=l[i];
len*=2;
for(int i=1;i<=len;i++)v[i]=i-dep[mxdep[l[i]]];
build(1,1,len);
for(int i=2;i<=len;i++)
{
int tmp=i+dep[mxdep[l[i]]]+2;
update(tmp-v[id],mxdep[l[id]],mxdep[l[i]]);
}
printf("Case #%d: %d %d %d\n",cas,2*n-ans,ansx,ansy);
}
return 0;
}


## K - Binary Indexed Tree (jmx赛后补题)

#include <bits/stdc++.h>
#define MOD 1000000007
#define inf 0x6fffffff
using namespace std;
typedef long long ll;
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll f[61][61][2],bin[61];
void pre()
{
bin[0]=1;
for(int i=1;i<=60;i++)bin[i]=(bin[i-1]<<1)%MOD;
for(int i=1;i<=60;i++)
{
for(int j=0;j<=i-2;j++)
{
f[i][j][0]=bin[i-j-2];
f[i][j][1]=bin[i-j-2];
}
f[i][i-1][1]=1;
f[i][i][0]=1;
}
}
ll calc(ll n)
{
int num[61],cnt=0;
ll x=n;
while(x)
{
num[++cnt]=x&1;x>>=1;
}
ll ans=0;
n%=MOD;
for(int k=0;k<=cnt;k++)
{
ll res=0;
for(int i=1;i<cnt;i++)res=(res+f[i][k][1])%MOD;
for(int i=cnt-1;i;i--)
{
if(num[i]==1)
{
if(k<i-1)res=(res+f[i][k][0])%MOD;
}
}
res=res*(bin[k]*((n+1-bin[k])%MOD)%MOD)%MOD;
ans=(ans+res)%MOD;
}
ll tmp=(1LL<<(cnt-1))%MOD;
for(int i=cnt-1;i;i--)
{
if(num[i+1])ans=(ans+(n-tmp+1)*(n+1-(n-tmp+1))%MOD)%MOD;
tmp=(tmp+num[i]*(1LL<<(i-1))%MOD)%MOD;
}
if(num[1])ans+=n;
return (ans+MOD)%MOD;
}
int main()
{
pre();