感情粉末沿着试管边缘 在祝福中逐渐分解 加热认知离子重新排列 于底部悲伤沉淀
test39
降水
令 \(a_i\gets \frac{a_i}{2}\),先计算出 \(\sum a_i=\frac{\sum p_i}{2}\),然后因为限定了 \(n\) 的奇偶性容易减出 \(a_n\),然后容易依次求出 \(a_1,\dots,a_{n-1}\)。
#pragma GCC optimize(1,2,3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
using namespace std;
const int N=100005;
int n, p[N], a[N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
up(i,1,n) cin >> p[i], a[n]+=p[i];
a[n]/=2;
up(i,1,n/2) a[n]-=p[2*i-1];
a[1]=p[n]-a[n];
up(i,2,n-1) a[i]=p[i-1]-a[i-1];
up(i,1,n) cout << 2*a[i] << ' ';
return 0;
}
查找
其实我考试的时候根本没看到 70mb,这种题感觉纯无聊没事找事,你要是能优化量级卡空间就算了,你这是啥,卡空间还要快读卡常。
你想直接归并数组,但是你不能开 long long,所以我们考虑离线,对询问排序,然后双指针去扫答案。
#pragma GCC optimize(1,2,3,"Ofast","inline")
#include<bits/stdc++.h>
#define ll long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
#define pll pair<ll,ll>
#define fir first
#define sec second
using namespace std;
inline int read() {
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-') flag=0; ch=getchar(); }
while(ch>='0'&&ch<='9') { X=(X<<1)+(X<<3)+ch-'0'; ch=getchar(); }
if(flag) return X;
return ~(X-1);
}
const int N=7500005, M=1000005;
int n, q, a[N], b[N];
pll u[M];
signed main() {
n=read(), q=read();
up(i,1,n) a[i]=read();
up(i,1,n) b[i]=read();
up(i,1,q) u[i]=make_pair(read(),i);
sort(u+1,u+1+q);
int l=0, r=0; ll A=0, B=0;
up(i,1,q) {
while(l+r<u[i].fir) if(l<n&&A+a[l+1]<B+b[r+1]||r>=n) A+=a[++l]; else B+=b[++r];
if(l&&r) u[i].fir=max(A,B); else u[i].fir=A+B;
}
sort(u+1,u+1+q,[](pll i,pll j){return i.sec<j.sec;});
up(i,1,q) printf("%lld\n", u[i].fir);
return 0;
}
概率
首先 \(</>\) 是等价的因为可以交换,所以计算等于的方案数就好了,考虑去枚举 \(s=\sum_{i\leq n}a_i\) 是 \(O(Tnm)\) 起步且难以拆开的,我们还是想办法让 \(a_{1\to 2n}\) 一起做。
难点在于 \(i\leq n/i>n\) 的贡献不等价,注意到和为 \(s/nm-s\) 是一对的,可以把 \(i>n\) 双射成 \(a'_i=m-a_i\),这样子就是 \(2n\) 个数和是 \(nm\),每个位置只能放 \([0,m]\) 的方案数,还是上二项式反演。
#pragma GCC optimize(1,2,3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
using namespace std;
const int N=2005, M=5000005;
int T, n, m, P, ans, mul[M], inv[M];
int ksm(int a,int b=P-2) {
int res=1;
for( ; b; b>>=1) {
if(b&1) res=res*a%P;
a=a*a%P;
}
return res;
}
int C(int n,int m) {
if(m<0||n<m) return 0;
return mul[n]*inv[m]%P*inv[n-m]%P;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> P >> T;
mul[0]=inv[0]=inv[1]=1;
up(i,1,5e6) mul[i]=mul[i-1]*i%P;
up(i,2,5e6) inv[i]=inv[P%i]*(P-P/i)%P;
up(i,2,5e6) inv[i]=inv[i-1]*inv[i]%P;
while(T--) {
cin >> n >> m, ans=0;
up(i,0,2*n) {
int v=C(2*n,i)*C(n*m-(m+1)*i+2*n-1,2*n-1)%P;
if(i%2==0) (ans+=v)%=P;
if(i%2==1) (ans-=v)%=P;
}
int tot=((ksm((m+1),2*n)-ans)%P+P)%P, inv=ksm(m+1);
up(i,1,2*n) tot=tot*inv%P;
cout << tot*ksm(2)%P << '\n';
}
return 0;
}
回文
你这个题目 \(T=10,n=1000,k\geq 3\),你如果不带 log 你就得(手)写 unordered_map,不写这个常数巨大的东西去带 \(\log\) 虽然本地能快一倍,但是你直接提交一个 \(Tnmk\log nm\) 次操作的代码都过不了, 事实上你这个数据范围测试了一下 o3 你只能跑 \(8Tnmk\),这个到底怎么过的?我感觉分类讨论都不止 8 次计数?
分类讨论 \(len(A)\) 和 \(len(C)\) 的大小,然后每一种都是多步骤的简单计数,在草稿纸上认真写了发了牢骚就不赘述了 /yun
upd 其实有简单匹配做法,但是真的懒得写了

#include <algorithm>
#include <cstdio>
#include <cstring>
#define FILEIO(filename) (freopen(filename ".in", "r", stdin), freopen(filename ".out", "w", stdout))
typedef long long ll;
typedef unsigned long long ull;
const int N = 3005;
int Test;
int n[3], nn;
char s[3][N];
ll f[N][N];
ll g[3][N], h[3][N], w;
inline int getc(int id)
{
if (id <= n[0])
return s[0][id] - 'a';
else if (id <= n[0] + n[1])
return s[1][id - n[0]] - 'a';
return s[2][id - n[0] - n[1]] - 'a';
}
inline int ledge(int b)
{
int res = 1;
for (int i = 0; i < b; ++i)
res += n[i];
return res;
}
inline int redge(int b)
{
int res = 0;
for (int i = 0; i <= b; ++i)
res += n[i];
return res;
}
int main(void)
{
freopen("pali.in","r",stdin);
freopen("pali.out","w",stdout);
scanf("%d", &Test);
while (Test--)
{
for (int o = 0; o < 3; ++o)
scanf("%s", s[o] + 1), n[o] = strlen(s[o] + 1);
nn = n[0] + n[1] + n[2];
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(h, 0, sizeof(h));
w = 0;
ll ans = 0;
for (int i = 1; i <= nn; ++i)
for (int j = nn; j > i; --j)
{
if (getc(i) != getc(j))
continue;
int beli = (i > n[0]) + (i > n[0] + n[1]);
int belj = (j > n[0]) + (j > n[0] + n[1]);
if (beli == 0 && belj == 2)
f[i][j] = 1;
f[i][j] += g[belj][i];
f[i][j] += h[beli][j];
if (beli == 1 && belj == 1)
f[i][j] += w;
if (i != ledge(beli) && j != redge(belj))
f[i][j] += f[i - 1][j + 1];
if (beli == 0 && belj == 2)
w += f[i][j];
if (belj != 0 && i != redge(beli))
g[belj - 1][i + 1] += f[i][j];
if (beli != 2 && j != ledge(belj))
h[beli + 1][j - 1] += f[i][j];
if (f[i][j])
{
if (beli == belj)
ans += (j - i <= 2) * f[i][j];
else if (beli + 1 == belj)
ans += (1 + (i != redge(beli)) + (j != ledge(belj))) * f[i][j];
}
}
ans += w * n[1];
printf("%lld\n", ans);
}
return 0;
}
#pragma GCC optimize(1,2,3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
using namespace std;
const int N=1005, M=3000005, B=233;
int T, m, ans, L[N], R[N], cnt[M], res[M];
ull sp[M], pw[N];
struct Str {
int len, LR[N][N], RL[N][N];
char str[N];
ull L[N], R[N];
inline ull lr(int l,int r) { --l; return L[r]-L[l]*pw[r-l]; }
inline ull rl(int l,int r) { ++r; return R[l]-R[r]*pw[r-l]; }
inline int glr(int l,int r) { return LR[l][r]; }
inline int grl(int l,int r) { return RL[l][r]; }
void build() {
len=strlen(str+1), L[0]=R[len+1]=0;
up(i,1,len) L[i]=L[i-1]*B+(str[i]-'a'+1);
dn(i,len,1) R[i]=R[i+1]*B+(str[i]-'a'+1);
up(i,1,len) up(j,1,len) sp[++m]=i<=j?lr(i,j):rl(j,i);
}
bool check(int l,int r) { return lr(l,r)==rl(l,r); }
void rev() {
up(i,1,len/2) {
swap(str[i],str[len-i+1]);
swap(L[i],L[len-i+1]);
swap(R[i],R[len-i+1]);
}
up(i,1,len) swap(L[i],R[i]);
}
void Sort() {
up(i,1,len) up(j,i,len) {
LR[i][j]=lower_bound(sp+1,sp+1+m,lr(i,j))-sp;
RL[i][j]=lower_bound(sp+1,sp+1+m,rl(i,j))-sp;
}
}
} a, b, c;
void solve1() {
memset(L,0,sizeof(L)), memset(R,0,sizeof(R));
up(l,1,b.len) up(r,l,b.len) if(b.check(l,r)) ++R[l];
up(l,1,b.len) up(r,l,b.len) cnt[b.glr(l,r)]+=R[r+1]+1;
up(l,1,c.len) up(r,l,c.len) L[r]+=cnt[c.grl(l,r)];
up(l,1,b.len) up(r,l,b.len) cnt[b.glr(l,r)]=0;
up(l,1,c.len) up(r,l,c.len) res[c.glr(l,r)]+=L[l-1];
up(l,1,a.len) up(r,l,a.len) ans+=res[a.grl(l,r)];
up(l,1,c.len) up(r,l,c.len) res[c.glr(l,r)]=0;
}
void solve2() {
memset(L,0,sizeof(L)), memset(R,0,sizeof(R));
up(l,1,c.len) up(r,l,c.len) if(c.check(l,r)) ++L[r];
up(l,1,b.len) up(r,l,b.len) ++cnt[b.glr(l,r)];
up(l,1,c.len) up(r,l,c.len) R[r]+=L[l-1]*cnt[c.grl(l,r)];
up(l,1,b.len) up(r,l,b.len) cnt[b.glr(l,r)]=0;
up(l,1,a.len) up(r,l,a.len) ++res[a.glr(l,r)];
up(l,1,c.len) up(r,l,c.len) ans+=res[c.grl(l,r)]*R[l-1];
up(l,1,a.len) up(r,l,a.len) res[a.glr(l,r)]=0;
}
void mian() {
cin >> (a.str+1) >> (b.str+1) >> (c.str+1);
m=0, a.build(), b.build(), c.build();
sort(sp+1,sp+1+m), m=unique(sp+1,sp+1+m)-sp-1;
a.Sort(), b.Sort(), c.Sort();
int mid=0, ran=0;
up(l,1,b.len) up(r,l,b.len) if(b.check(l,r)) ++mid;
up(l,1,a.len) up(r,l,a.len) ++cnt[a.glr(l,r)];
up(l,1,c.len) up(r,l,c.len) ran+=cnt[c.grl(l,r)];
up(l,1,a.len) up(r,l,a.len) --cnt[a.glr(l,r)];
ans=mid*ran;
solve1(), solve2();
a.rev(), b.rev(), c.rev(), swap(a,c), a.Sort(), b.Sort(), c.Sort();
solve1(), solve2();
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
pw[0]=1;
up(i,1,1000) pw[i]=B*pw[i-1];
cin >> T;
while(T--) mian();
return 0;
}

浙公网安备 33010602011771号