A.怪盗-1412
111
…
1
⏟
⌊
n
2
⌋
444
…
4
⏟
m
111
…
1
⏟
⌈
n
2
⌉
222
…
2
⏟
k
\begin{matrix} \underbrace{ 111\dots\ 1} \\ \lfloor \frac{n}{2} \rfloor \end {matrix}\begin{matrix} \underbrace{ 444\dots\ 4} \\ m \end{matrix}\begin{matrix} \underbrace{ 111\dots\ 1} \\ \lceil \frac{n}{2} \rceil \end {matrix}\begin{matrix} \underbrace{ 222\dots\ 2} \\ k \end {matrix}
111… 1⌊2n⌋
444… 4m
111… 1⌈2n⌉
222… 2k
上述排列方式最优,由此根据组合数即可求解。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
IO;
int T=1;
cin>>T;
while(T--)
{
ll n,m,k;
cin>>n>>m>>k;
cout<<n/2*((n+1)/2)*m*k<<'\n';
}
return 0;
}
Dis2
与某个点距离为2的点有三种①父亲的父亲②儿子的儿子③兄弟节点
跑dfs统计以下即可
sz[u]
表示u
节点儿子的数量
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=200010,M=2*N;
int h[N],e[M],ne[M],idx;
int ans[N],sz[N];
void add(int a,int b)
{
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
int n;
void dfs(int u,int fa)
{
for(int i=h[u];i!=-1;i=ne[i])
{
int j=e[i];
if(j==fa) continue;
if(fa)
{
ans[fa]++;
ans[j]++;
}
sz[u]++;
dfs(j,u);
}
for(int i=h[u];i!=-1;i=ne[i])
{
int j=e[i];
if(j==fa) continue;
ans[j]+=sz[u]-1;
}
}
int main()
{
IO;
int T=1;
//cin>>T;
while(T--)
{
memset(h,-1,sizeof h);
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
dfs(1,0);
for(int i=1;i<=n;i++) cout<<ans[i]<<'\n';
}
return 0;
}
C.序列卷积之和
一步一步推式子,预处理各种前缀和。真的无脑暴力
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=200010:
const ll mod=1e9+7;
ll a[N],b[N],c[N],d[N],e[N],f[N];
int n;
int main()
{
IO;
int T=1;
//cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) b[i]=(b[i-1]+a[i])%mod;
for(int i=1;i<=n;i++) c[i]=(c[i-1]+a[i]*b[i-1]%mod)%mod;
for(int i=1;i<=n;i++) d[i]=(d[i-1]+b[i]*b[i]%mod)%mod;
for(int i=1;i<=n;i++) e[i]=(e[i-1]+b[i])%mod;
for(int i=1;i<=n;i++) f[i]=(f[i-1]+c[i])%mod;
ll res=0;
for(int i=1;i<=n;i++)
res=(res+d[n]-d[i-1]-b[i-1]*(e[n]-e[i-1])%mod-f[n]+f[i-1]+(n-i+1)*c[i-1]%mod)%mod;
cout<<(res+mod)%mod<<'\n';
}
return 0;
}
D.宝石装箱
学习自大佬题解
首先先说一下错排,虽然我没做过错排,看到这个题首先想到如果每个箱子只对应一个不能放的宝石,那么直接可以容斥原理解决了,不过此题没有那么简单。看了上述文章学到错排还可以用递推方式解决这里记录一下。
状态表示: f i f_i fi错排规模为 i i i时的方案数
状态计算:对于规模为 i i i的错排,考虑第 i i i个小球,不妨让第 i i i个小球放在了第 j ( 1 ≤ j < i ) j(1\leq j<i) j(1≤j<i)个箱子里(第 i i i禁止放在第 i i i个箱子里),那么考虑第 k k k个小球是否放在了第 i i i个箱子,如果放在了第 i i i个箱子那么很容易发现现在规模是 i − 2 i-2 i−2的错排方案,如果没有放在第 i i i个箱子里那么现在规模是 i − 1 i-1 i−1的错排方案,又因为 k k k有 i − 1 i-1 i−1种选择那么可以得到递推式 f i = ( i − 1 ) × ( f i − 1 + f i − 2 ) f_i=(i-1)×(f_{i-1}+f_{i-2}) fi=(i−1)×(fi−1+fi−2)
经过一顿操作计算可得 f n = n ! [ 1 2 ! − 1 3 ! + ⋯ + ( − 1 ) n 1 n ! ] f_n=n![\frac{1}{2!}-\frac{1}{3!}+\dots+(-1)^n\frac{1}{n!}] fn=n![2!1−3!1+⋯+(−1)nn!1]
不难看出上式和容斥原理得出的答案相同。
容斥原理后现在需要求得至少
{
1
,
2
,
3
,
…
,
n
}
\{1,2,3,\dots,n\}
{1,2,3,…,n}个盒子不合法的情况,直接上上述大佬题解结论:
状态表示:
f
(
i
,
j
)
f_{(i,j)}
f(i,j)对于只考虑前
i
i
i种盒子,
j
j
j个盒子不合法的方案数
状态转移:考虑最后一步第
i
i
i个盒子是否合法易得出
f
(
i
,
j
)
=
f
(
i
−
1
,
j
)
+
f
(
i
−
1
,
j
−
1
)
×
c
n
t
i
f_{(i,j)}=f_{(i-1,j)}+f_{(i-1,j-1)}×cnt_i
f(i,j)=f(i−1,j)+f(i−1,j−1)×cnti
c
n
t
i
cnt_i
cnti是第
i
i
i个箱子不能放的小球数量
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=8010;
const ll mod=998244353;
ll f[2][N],fact[N];
int cnt[N],n;
int main()
{
IO;
int T=1;
//cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
cnt[a]++;
}
fact[0]=1;
for(int i=1;i<=n;i++) fact[i]=fact[i-1]*i%mod;
f[0][0]=f[1][0]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
f[i&1][j]=(f[i-1&1][j]+f[i-1&1][j-1]*cnt[i]%mod)%mod;
ll res=fact[n];
for(int i=1,k=-1;i<=n;i++,k*=-1)
res=(res+k*fact[n-i]*f[n&1][i])%mod;
cout<<(res+mod)%mod<<'\n';
}
return 0;
}
这个dp太妙了吧,递推太精髓了。
E.红色的樱花
待补——2020/9/23补
大佬题解
扩展欧几里得可以求出正整数
a
,
b
a,b
a,b满足
a
x
+
b
y
=
g
c
d
(
a
,
b
)
ax+by=gcd(a,b)
ax+by=gcd(a,b)一组特解
{
x
=
x
0
y
=
y
0
\begin{cases} x=x_0 \\y=y_0\end{cases}
{x=x0y=y0,通解是
{
x
=
x
0
+
k
b
g
c
d
(
a
,
b
)
y
=
y
0
−
k
a
g
c
d
(
a
,
b
)
\begin{cases} x=x_0+\frac{kb}{gcd(a,b)} \\y=y_0-\frac{ka}{gcd(a,b)}\end{cases}
{x=x0+gcd(a,b)kby=y0−gcd(a,b)ka
本人在看上述题解的时候不明白如何判断两点是否在同一个循环中于时自己推了一下:
{
s
x
+
?
=
k
1
n
+
e
x
s
y
+
?
=
k
2
m
+
e
y
\begin{cases} s_x+?=k_1n+e_x \\s_y+?=k_2m+e_y\end{cases}
{sx+?=k1n+exsy+?=k2m+ey两式子作差可得
s
x
−
s
y
−
(
e
x
−
e
y
)
=
k
1
n
+
(
−
k
2
)
m
=
k
g
c
d
(
n
,
m
)
s_x-s_y-(e_x-e_y)=k_1n+(-k_2)m=kgcd(n,m)
sx−sy−(ex−ey)=k1n+(−k2)m=kgcd(n,m)
由此推出
s
x
−
s
y
−
(
e
x
−
e
y
)
≡
0
(
g
c
d
(
n
,
m
)
)
s_x-s_y-(e_x-e_y)\equiv0 (gcd(n,m))
sx−sy−(ex−ey)≡0(gcd(n,m))
因此只要满足上述式子,就能保证两点在同一循环中。
那么如何通过操作二使得不在同一个循环的两点处在同一循环?
{
s
x
+
x
d
=
k
1
n
+
e
x
s
y
=
k
2
m
+
e
y
\begin{cases} s_x+xd=k_1n+e_x \\s_y=k_2m+e_y\end{cases}
{sx+xd=k1n+exsy=k2m+ey两式子作差可得
s
x
−
s
y
−
(
e
x
−
e
y
)
+
x
d
=
k
1
n
+
(
−
k
2
)
m
=
y
g
c
d
(
n
,
m
)
s_x-s_y-(e_x-e_y)+xd=k_1n+(-k_2)m=ygcd(n,m)
sx−sy−(ex−ey)+xd=k1n+(−k2)m=ygcd(n,m)进一步化简可得
x
d
+
y
g
c
d
(
n
,
m
)
=
e
x
−
e
y
−
(
s
x
−
s
y
)
xd+ygcd(n,m)=e_x-e_y-(s_x-s_y)
xd+ygcd(n,m)=ex−ey−(sx−sy)由此用扩展欧几里得求出最小正整数
x
x
x即可。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=1e18;
ll n,m,d,sx,sy,ex,ey,a,b,c;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1,y=0;
return a;
}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
ll x,y;
ll calc(ll a,ll b,ll c,ll cost)
{
ll gcd=exgcd(a,b,x,y);
if(c%gcd) return INF;
b/=gcd;if(b<0) b=-b;
x=(c/gcd*x%b+b)%b;
return x*cost;
}
int main()
{
IO;
int T=1;
cin>>T;
while(T--)
{
cin>>n>>m>>d>>sx>>sy>>ex>>ey>>a>>b>>c;
ll gcd=exgcd(n,m,x,y);
if(sx==ex&&sy==ey) cout<<"0\n";
else
{
ll res=calc(d,n,ex-sx+n,b)+calc(d,m,ey-sy,c);
res=min(res,INF);
ll gcd=exgcd(n,m,x,y);
res=min(res,calc(d,gcd,ex-ey+sy-sx,b)+a);
res=min(res,calc(d,gcd,ey-ex+sx-sy,c)+a);
if(res==INF) cout<<-1<<'\n';
else cout<<res<<'\n';
}
}
return 0;
}
要加油哦~