我是靠谱客的博主 坚强黑夜,这篇文章主要介绍AGC010 B - Boxes(思维,数学),现在分享给大家,希望可以做个参考。

题意:

在这里插入图片描述

解法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
这种判断能否合法的题,得往数学式子那边想... ---------- 每次操作数组的总值一定减少t=n*(n+1)/2,因此数组的和sum必须是t的倍数,否则无解. 设总操作次数为tot=sum/t. 由于相邻数的差值变化要么为1,要么为n-1, 容易想到可能差分数组有关:d[i]=a[i]-a[i-1]. 设s[i]为以i为起点的操作次数,那么有式子: d[i]=s[i]*(n-1)-(tot-s[i]), 解得d[i]=(n*s[i]-t),s[i]=(d[i]+tot)/n, 因此当(d[i]+tot)%n!=0(d[i]+tot)/n<0,无解. 其他情况有解. 参考: https://www.luogu.com.cn/blog/ryoku/solution-at2303

code:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h> #define int long long #define PI pair<int,int> using namespace std; const int maxm=2e6+5; int a[maxm]; int n; void solve(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } int sum=0; for(int i=1;i<=n;i++){ sum+=a[i]; } int t=n*(n+1)/2; if(sum%t){ cout<<"NO"<<endl; return ; } int tot=sum/t;//总操作次数 a[0]=a[n]; for(int i=1;i<=n;i++){ int dif=a[i]-a[i-1]; if((tot-dif)%n||(tot-dif)/n<0){ cout<<"NO"<<endl; return ; } } cout<<"YES"<<endl; } signed main(){ ios::sync_with_stdio(0); solve(); return 0; }

最后

以上就是坚强黑夜最近收集整理的关于AGC010 B - Boxes(思维,数学)的全部内容,更多相关AGC010内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部