首页 > 其他分享 >Educational Codeforces Round 155 (Rated for Div

Educational Codeforces Round 155 (Rated for Div

时间:2023-09-30 22:34:45浏览次数:33  
标签:pre Educational Rated 155 int ans2 cnt ans mod

B. Chips on the Board

image-20230930212557504

题解:贪心

  • 显然我们可以把题意转化为:对于任意一个\((i,j)\),我们可以花费\(a_{i,j}\)的代价占据第\(i\)行和第\(j\)列,求占据所有格子的最小代价

  • 考虑两种情况:

  1. 在每一行选一个格子
  2. 在每一列选一个格子

贪心选即可

int n, a[N], b[N];

void solve()
{
    cin >> n;
    int posa = -1, posb = -1;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        if (posa == -1 || a[i] < a[posa])
            posa = i;
    }
    for (int i = 1; i <= n; ++i)
    {
        cin >> b[i];
        if (posb == -1 || b[i] < b[posb])
            posb = i;
    }
    int ans = INF, sum = 0;
    for (int i = 1; i <= n; ++i)
        sum += a[i];
    sum += n * b[posb];
    ans = min(ans, sum);
    sum = 0;
    for (int i = 1; i <= n; ++i)
        sum += b[i];
    sum += n * a[posa];
    ans = min(ans, sum);
    cout << ans << endl;
}

C. Make it Alternating

image-20230930213426552

题解:组合数学

  • 显然可以将字符串\(s\)分成由连续\(1\)或\(0\)组成的几段区间,那么最小操作次数显然是区间的数量

  • 我们考虑方案数:

对于每段区间,我们必须留下一个数,假设某段区间的长度为\(len\),那么对答案的贡献为:\(ans:=ans\times len\)

那么其他没有被留下的数就要全部被删除,那么删除的顺序任意,假设被删除的数的数量为\(cnt\),那么对答案的贡献为\(ans:=ans \times cnt!\)

int n, fac[N];

void init()
{
    fac[0] = 1ll;
    for (int i = 1; i < N; ++i)
        fac[i] = (fac[i - 1] * i) % mod;
}

void solve()
{
    string s;
    cin >> s;
    n = s.length();
    s = " " + s;
    int ans1 = 0, ans2 = 1;
    for (int i = 1; i < n; ++i)
        ans1 += (s[i] == s[i + 1]);
    int l, r, f = 0;
    for (int i = 1; i < n; ++i)
    {
        if (!f && s[i] == s[i + 1])
        {
            f = 1;
            l = i;
        }
        else if (f && s[i] != s[i + 1])
        {
            f = 0;
            r = i;
            ans2 *= (r - l + 1);
            ans2 %= mod;
        }
        else if (f && i + 1 == n)
        {
            f = 0;
            r = n;
            ans2 *= (r - l + 1);
            ans2 %= mod;
        }
    }
    if (f)
        ans2 = ans2 * (n - l + 1) % mod;
    ans2 = ans2 * fac[ans1] % mod;
    cout << ans1 << " " << ans2 << endl;
}

D. Sum of XOR Functions

image-20230930214054285

题解:按位计算贡献

  • 我们发现,对于二进制中某一位\(i\)来说,我们考虑其对答案的贡献 :

\[2^i \sum_{r = 1}^n \sum_{l=1}^r g(l,r)(r - l + 1) \]

\(g(l,r)\)为第\(i\)位二进制在\([l,r]\)中\(1\)的个数,如果\(1\)为奇数,则\(g(l,r)=1\),否则\(g(l,r) = 0\)

  • 那么题目就转化为:对于一个\(01\)序列,固定右端点\(r\),求有多少个左端点\(l\)使得\(g(l,r)=1\)

  • 我们定义\(pre[i]\)为\([1,i]\)的前缀异或和,那么\(pre[r] \oplus pre[l-1] = 1\)就代表\(g(l,r)=1\)

  • 那么我们完全可以对前缀维护一个\(cnt\)和\(sum\)

  • 所以复杂度为 \(O(30n)\)

int n, a[N];

ll add(ll a, ll b) { return (a + b) % mod; }
ll sub(ll a, ll b) { return ((a - b) % mod + mod) % mod; }

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    int ans = 0ll;
    // 考虑每一位二进制对答案产生的贡献
    for (int i = 0; i <= 30; ++i)
    {
        vector<int> vec(n + 10), pre(n + 10), cnt(2), sum(2);
        int res = 0ll;
        cnt[0]++; // pre[0] = 0
        for (int j = 1; j <= n; ++j)
        {
            vec[j] = (a[j] >> i & 1);
            pre[j] = (pre[j - 1] ^ vec[j]);
            cnt[pre[j]]++;
            sum[pre[j]] += j;
            res = add(res, sub((cnt[pre[j] ^ 1] * (j) % mod), sum[pre[j] ^ 1]));
        }
        ans = add(ans, (1ll << i) * res % mod);
    }
    cout << ans << endl;
}

标签:pre,Educational,Rated,155,int,ans2,cnt,ans,mod
From: https://www.cnblogs.com/Zeoy-kkk/p/17738331.html

相关文章

  • Educational Codeforces Round 122 (Rated for Div. 2)
    A.Div.7#include<bits/stdc++.h>usingnamespacestd;voidsolve(){intn,a,b,c;cin>>n;c=n%10,n/=10;b=n%10,n/=10;a=n%10,n/=10;intres,val=100;for(inti=0;i<=9......
  • 解题报告 洛谷P2155 [SDOI2008] 沙拉公主的困惑
    P2155[SDOI2008]沙拉公主的困惑题目题面非常的简洁,求\(\sum\limits_{i=1}^{n!}[i\perpm!]\)直接颓式子,\[\begin{aligned}ans&=\dfrac{n!}{m!}\cdot\varphi(m!)\\\\&=\dfrac{n!}{m!}*m!\prod\limits_{p\midm!}[\dfrac{p-1}{p}]\\&=n!\cdot\dfrac{\......
  • Educational Codeforces Round 155 (Rated for Div. 2)
    Preface这天晚上这场因为不明原因(玩CCLCC中)就没有打,只能赛后补一下这场的EF都不算难初看都有做法,但好家伙E写挂两发,F写个根号做法直接T到天上去了A.Rigged!签到题,对于所有的\(e_i\gee_1\)的\(i\),求出\(S=\maxs_i\),根据\(S+1\)和\(s_1\)的大小关系判断即可#include<cstdio......
  • 加训日记 Day4——挑战edu155,铭记巅峰的一集
    Day4,9.24edu155  ·打满六场新手保护赛之后的第一场(早知道暑假就不打那几场浪费保护了)  ·这场不出意外就是出意外了,库函数用的不熟练,奇奇怪怪的地方爆LL  ·C题赛后10分钟内看了看别人思路补出来了,进入思维误区了属于是  ·打完这场掉了25分捏,我觉得罚时得背大锅,越w......
  • CF957 Codeforces Round 472 (rated, Div. 2, based on VK Cup 2018 Round 2)
    CF957ATritonicIridescence如果原序列中有两个相同的字符,显然不合法。如果开头或者结尾为?,或者有两个连续的?,或者一个?两边的字符不同显然合法。否则一定不合法。#include<iostream>#include<cstdio>usingnamespacestd;constintN=105;intn;chars[N];intma......
  • CF1036 Educational Codeforces Round 50 (Rated for Div. 2)
    CF1036AFunctionHeight答案为\(\lceil\frac{k}{n}\rceil\)。#include<iostream>#include<cstdio>usingnamespacestd;longlongn,k;intmain(){ scanf("%lld%lld",&n,&k); printf("%lld",(k+n-1)/n); return0;}......
  • GENERATED_BODY()函数是什么?
    会发现它是一个宏定义//Includearedundantsemicolonattheendofthegeneratedcodeblock,sothatintellisenseparserscanstartparsing//anewdeclarationifthelinenumber/generatedcodeisoutofdate.#defineGENERATED_BODY_LEGACY(...)BODY_MACRO......
  • Educational Codeforces Round 155 D (CF1879_D)
    题目大意给一个长度为\(n\)的数组,求\(\Sigma_{i=1}^{n}\Sigma_{j=i}^{n}区间异或和\times(j-i+1)\)其中\(n\leq3e5,~a[i]\leq1e9\)分析首先注意到由\(l\)到\(r\)的区间异或和可以转化为\(sum_{l-1}~XOR~sum_r\)因此,对于每一个点\(x\),无论它作为上述的\(sum......
  • # Tensorflow Federated (tff)
    TensorflowFederated(tff)1.安装pipinstalltensorflow-federated可通过以下方式检查已安装成功,importtensorflow_federatedastffprint(tff.federated_computation(lambda:'HelloWorld')())理想输出,2.数据tff.simulation.datasetstff.simulation下的一个模......
  • Educational Codeforces Round 155 (Rated for Div. 2)
    EducationalCodeforcesRound155(RatedforDiv.2)A.Rigged!解题思路:若存在\(s[i]>=s[1]\)并且\(e[i]>=e[i]\),那么答案为\(-1\).否则,答案为\(s[1]\).代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;constintN=1e6+10;con......