问题 A: 分糖果
答案为
N
−
1
N-1
N−1
代码如下:
#include <bits/stdc++.h>
using namespace std;
signed main() {
int n;
cin>>n;
cout<<n-1;
return 0;
}
问题 B: 数字回文
删除字符串最右端的所有
0
0
0 ,判断处理后的字符串是否为回文串即可
代码如下:
#include <bits/stdc++.h>
using namespace std;
signed main() {
string s;
cin>>s;
while(!s.empty()&&s.back()=='0')
s.pop_back();
for(int i=0,j=s.size()-1;i<j;i++,j--)
if(s[i]!=s[j])
{
cout<<"No";
return 0;
}
cout<<"Yes";
return 0;
}
问题 C: 冒险家
令
d
i
s
(
x
,
y
)
dis(x,y)
dis(x,y) 表示起点到终点距离
通过画图可得到,
- 当 d i s ( x , y ) < R dis(x,y)<R dis(x,y)<R , a n s = 2 ans=2 ans=2
- 当 d i s ( x , y ) = = R dis(x,y)==R dis(x,y)==R , a n s = 1 ans=1 ans=1
- 其他情况下, a n s = ⌈ d i s ( x , y ) / R ⌉ ans=\lceil dis(x,y)/R \rceil ans=⌈dis(x,y)/R⌉ (上取整)
代码如下:
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long r,x,y;
cin>>r>>x>>y;
if(x*x+y*y<r*r)
cout<<2;
else
cout<<ceil(sqrt(1.0*(x*x+y*y)/(r*r)));
return 0;
}
问题 D: 涂色树
根据题意进行
D
F
S
DFS
DFS ,记录
D
F
S
DFS
DFS 过程中遇到所有颜色的数量,若当前点颜色为新增颜色,则当前点记录为答案
代码如下:
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin>>n;
vector<int>ans;
vector<int>num(1e5+10,0),col(n+1);
vector<vector<int>>son(n+1);
for(int i=1;i<=n;i++)
cin>>col[i];
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
son[u].push_back(v);
son[v].push_back(u);
}
function<void(int,int)>dfs=[&](int u,int fa){
for(auto v:son[u])
if(v!=fa)
{
num[col[v]]++;
if(num[col[v]]==1)
ans.push_back(v);
dfs(v,u);
num[col[v]]--;
}
};
num[col[1]]++;
ans.push_back(1);
dfs(1,0);
sort(ans.begin(),ans.end());
for(auto i:ans)
cout<<i<<"\n";
return ;
}
signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T=1;
while(T--)
solve();
return 0;
}
问题 E: 涂色游戏
图上
D
F
S
DFS
DFS 统计答案,同一点上不同颜色方案用加法原则统计答案,不同点上方案用乘法原则统计答案
由于同一连通块内除初始进行
D
F
S
DFS
DFS 的点外,每个点都起码有一个已涂色的相邻点对可涂颜色进行限制,故时间复杂度为
O
(
N
×
2
N
)
O(N \times 2^N)
O(N×2N)
注意:需记录第一次
D
F
S
DFS
DFS 到达的父亲节点,防止一个点作为多个点的儿子被反复统计答案
代码如下:
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
void solve()
{
int n,m;
LL ans=1;
cin>>n>>m;
vector<int>col(n+1),vis(n+1);
vector<vector<int>>son(n+1);
for(int i=1,u,v;i<=m;i++)
{
cin>>u>>v;
son[u].push_back(v);
son[v].push_back(u);
}
function<LL(int,int)>dfs=[&](int u,int fa){
if(vis[u]&&vis[u]!=fa)return 0ll;
vis[u]=fa;
LL res=1,tans;
for(auto v:son[u])
if(!vis[v]||vis[v]==u)
{
tans=0;
for(int i=1;i<=3;i++)
if(col[u]!=i)
{
col[v]=i;
tans+=dfs(v,u);
col[v]=0;
}
res*=tans;
}
else if(col[u]==col[v])
return 0ll;
return res;
};
LL tans=0;
for(int i=1;i<=n;i++)
if(!vis[i])
{
tans=0;
for(int j=1;j<=3;j++){
col[i]=j;
tans+=dfs(i,i);
}
ans*=tans;
}
cout<<ans<<"\n";
return ;
}
signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T=1;
while(T--)
solve();
return 0;
}
问题 F: 自定义骰子
首先,让我们考虑一下在确定了要写入的 6 6 6 个数字之后,有多少种在立方体表面上写数字的方式。这可以通过首先列举将这些数字分配给每个面的所有排列,然后消除那些在立方体旋转后重合的排列来找到。
给定 6 6 6 个要写入的整数,写入它们的方式的数量仅取决于相同数字的数量。例如,写入 ( 1 , 1 , 1 , 2 , 2 , 3 ) (1,1,1,2,2,3) (1,1,1,2,2,3) 和 ( 1 , 2 , 2 , 2 , 3 , 3 ) (1,2,2,2,3,3) (1,2,2,2,3,3) 的方式是相同的。因此,我们只需要找到和为S的正整数6元组的数量,针对每种类型。
让我们用 x 1 ≤ x 2 ≤ x 3 ≤ x 4 ≤ x 5 ≤ x 6 x_1 \leq x_2 \leq x_3 \leq x_4 \leq x_5 \leq x_6 x1≤x2≤x3≤x4≤x5≤x6 表示要写入立方体的 6 6 6 个整数。这 5 5 5 个不等式中的每一个都是严格不等式或等式,因此总共有 32 32 32 种情况。
例如,让我们考虑这样一种情况:正整数的组合数量,满足 x 1 = x 2 < x 3 = x 4 = x 5 < x 6 x_1 = x_2 < x_3 = x_4 = x_5 < x_6 x1=x2<x3=x4=x5<x6 ,并且 x 1 + x 2 + x 3 + x 4 + x 5 + x 6 = S x_1 + x_2 + x_3 + x_4 + x_5 + x_6 = S x1+x2+x3+x4+x5+x6=S 。
为了方便起见,假设
x
0
=
0
x_0 = 0
x0=0 ,并且通过变量转换
y
i
=
x
i
−
x
i
−
1
y_i = x_i - x_{i-1}
yi=xi−xi−1 ,这样的组合数量就等于正整数元组
(
y
1
,
y
3
,
y
6
)
(y_1, y_3, y_6)
(y1,y3,y6) 的数量,使得
6
y
1
+
4
y
3
+
y
6
=
S
6y_1 + 4y_3 + y_6 = S
6y1+4y3+y6=S 。可以使用数位动态规划或快速矩阵乘法来找到这样的解法数量。
其他情况也适用相同的方法。
由于我们已经找到了每种类型的元组数量,原始问题可以通过将它们分别乘以在立方体上写数字的方式的数量来解决。
代码如下:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353;
int qp(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int c(int n,int m)
{
int r=1;
for(int i=1;i<=m;i++)
r=1ll*r*((n-i+1)%mod)%mod*qp(i,mod-2)%mod;
return r;
}
int n;
signed main()
{
cin>>n;
n-=6;
int r=0;
r=(r+c(n+5,5))%mod;
r=(r+6ll*c(n/2+3,3)%mod)%mod;
r=(r+6ll*(4ll*c(n/4+2,2)%mod-1ll*(3-n%4)*(n/4+1)%mod+mod)%mod)%mod;
if(n%2==0) r=(r+3ll*c(n/2+2,2)%mod)%mod;
if(n%3==0) r=(r+8ll*(n/3+1)%mod)%mod;
r=1ll*r*qp(24,mod-2)%mod;
cout<<r<<endl;
return 0;
}