Editorial Divide by Zero and Codeforces Round #399 (Div. 1+2, combined) (A~F)

素颜马尾好姑娘i 2022-07-12 11:48 227阅读 0赞

CF #399

A题:水题

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[100010];
  4. int main()
  5. {
  6. int n;
  7. cin>>n;
  8. for(int i=0;i<n;i++){
  9. cin>>a[i];
  10. //s.insert(a);
  11. }
  12. int ans=0;
  13. if(n<=2)return 0*printf("%d",0);
  14. sort(a,a+n);
  15. for(int i=1;i<n-1;i++){
  16. if(a[i]>a[0]&&a[i]<a[n-1]){
  17. ans++;
  18. }
  19. }
  20. cout<<ans<<endl;
  21. return 0;
  22. }

B题:二分。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. ll ans;
  5. void solve(ll a,ll b,ll l, ll r, ll d)
  6. {
  7. if(a > b || l > r) return;
  8. else{
  9. ll mid=(a+b)/2;
  10. if(r < mid)solve(a,mid-1,l,r,d/2);
  11. else if(mid < l)solve(mid+1,b,l,r,d/2);
  12. else{
  13. ans += d%2;
  14. solve(a,mid-1,l,mid-1,d/2);
  15. solve(mid+1,b,mid+1,r,d/2);
  16. }
  17. }
  18. }
  19. int main()
  20. {
  21. ll n,l,r,sum=1;
  22. cin>>n>>l>>r;
  23. ll t=n;
  24. while(t>=2)
  25. {
  26. t/=2;
  27. sum=sum*2+1;
  28. }
  29. solve(1,sum,l,r,n);
  30. cout<<ans<<endl;
  31. return 0;
  32. }

C题:模拟,经过若干个操作后其序列会稳定不变。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e5+10;
  4. int a[maxn];
  5. int L[maxn],S[maxn];
  6. int main()
  7. {
  8. int n,k,x;
  9. cin>>n>>k>>x;
  10. for(int i=0;i<n;i++)cin>>a[i];
  11. sort(a,a+n);
  12. for(int i=0;i<k;i++){
  13. for(int j=0;j<n;j++){
  14. if(j%2==0)
  15. a[j]=a[j]^x;
  16. }
  17. sort(a,a+n);
  18. S[i]=a[0];
  19. L[i]=a[n-1];
  20. //经过若干轮操作就会稳定
  21. if(i>=2)
  22. if(S[i]==S[i-1]&&S[i]==S[i-2]&&L[i]==L[i-1]&&L[i]==L[i-2])
  23. break;
  24. }
  25. cout<<a[n-1]<<" "<<a[0]<<endl;
  26. return 0;
  27. }
  28. /*
  29. 5 1 2
  30. 9 7 11 15 5
  31. 13 7
  32. */

D题:概率DP。

总共有k种物品,每天产生一种物品(等概率)。问至少需要几天才能保证收集齐K种物品的概率超过(p - 1e-7)/2000。

设dp [ i ][ j ] 表示前 i 天总共得到了 j 种物品的概率。

那么转移方程为:dp[i][j]=(dp[i-1][j] * j + dp[i-1][j-1] * (k-j+1))/k

详细看代码。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. double dp[10005][1005];
  4. //总共有k种物品,每天产生一种物品(等概率)。
  5. //问至少需要几天才能保证收集齐K种物品的概率超过(p - 1e-7)/2000
  6. int main()
  7. {
  8. int k,q;
  9. cin>>k>>q;
  10. dp[0][0]=1.;
  11. for(int i=1;i<=10000;i++){
  12. for(int j=1;j<=k;j++)
  13. //dp[i][j] 表示前i天总共得到了j种物品的概率
  14. dp[i][j]=(dp[i-1][j] * j + dp[i-1][j-1] * (k-j+1))/k;
  15. }
  16. int p;
  17. while(q--){
  18. cin>>p;
  19. int i=1;
  20. int ans=0;
  21. while(dp[i][k]*2000 + 1e-7 < p )
  22. {
  23. i++;
  24. }
  25. cout<<i<<endl;
  26. }
  27. return 0;
  28. }
  29. /*
  30. 3 5
  31. 1
  32. 4
  33. 20
  34. 50
  35. 300
  36. 3
  37. 3
  38. 3
  39. 3
  40. 3
  41. */

E题:Nim博弈。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int sg[100010];
  4. int main()
  5. {
  6. int n;
  7. int st=0;
  8. cin>>n;
  9. for(int i=1;i<=100;i++){
  10. for(int j=1;j<=i+1;j++){
  11. sg[++st]=i;//状态数:1+2+3+...+n=n(n+1)/2
  12. }
  13. }
  14. int ans=0;
  15. while(n--){
  16. cin>>st;//1<=st<=60
  17. ans^=sg[st];
  18. }
  19. if(ans==0)cout<<"YES"<<endl;
  20. else cout<<"NO"<<endl;
  21. return 0;
  22. }

F题:组合数学+费马小定理+逆元

详细看代码。

官方题解:

Every arrangement of stacks can expressed in the form of linear arrangement. In this linear arrangement, every contiguous segment of wine barrels are separated by food boxes. For the arrangement to be liked by Jon each of the f + 1 partitions created by f food boxes must contain either 0 or greater than h wine barrels.

Let u out of f + 1 partitions have non-zero wine barrels then the remaining r + 1 - u partitions must have 0 wine barrels..

Total number of arrangements with exactly u stacks of wine barrels are 75e3aa80e8fe1bbb26bbd2ad749af6a87bcfa0a1.png
ccebfd63f39ea76c07de8b60d7761f3fab44e7a6.png is the number of ways of choosing u partitions out of f + 1 partitions.
X is the number of ways to place w wine barrels in these u partitions which is equal to the coefficient of x**w in { x**h + 1·(1 + x + …)}u. Finally we sum it up for all u from 1 to f + 1.

So the time complexity becomes O(w) with pre-processing of factorials.

w = 0 was the corner case for which the answer was 1.
We did not anticipate it will cause so much trouble. Not placing it in the pretests was a rookie mistake.

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn=1e5+10;
  5. const int mod=1e9+7;
  6. ll fac[maxn],inv[maxn];
  7. ll q_mod(ll a,ll n)
  8. {
  9. ll res=1;
  10. while(n){
  11. if(n&1)res=res*a%mod;
  12. n>>=1;
  13. a=a*a%mod;
  14. }
  15. return res;
  16. }
  17. ll C(ll n,ll m)//C(n,m)
  18. {
  19. if(n<m)return 0;
  20. return fac[n]*inv[m] % mod * inv[n-m] %mod;
  21. }
  22. int main()
  23. {
  24. fac[0]=1;
  25. for(int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod;
  26. inv[maxn-1]=q_mod(fac[maxn-1],mod-2);
  27. for(int i=maxn-2;i>=0;--i){
  28. inv[i]=inv[i+1]*(i+1)%mod;
  29. }
  30. ll f,w,h;
  31. cin>>f>>w>>h;
  32. if(w==0)return 0*puts("1");//特判...
  33. ll a=0,b=0;
  34. for(int i=1;i<=w;i++){
  35. b=(b + C(w-1-i*h,i-1) * C(f+1,i) )%mod;//符合要求的情况
  36. a=(a + C(w-1,i-1) * C(f+1,i) )%mod;//全部情况
  37. }
  38. //cout<<b<<endl;
  39. //cout<<a<<endl;
  40. // 概率为:b/a
  41. cout<<b * q_mod(a,mod-2)%mod<<endl;
  42. return 0;
  43. }

G题:。。。

不会。。。

不会。。。

不会。。。

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读