3.13

· · 个人记录

比赛结果出炉(翻车了)
|A |B |C |D |E |总分|排名| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| |100|65 |40 |0 | 0 |204|2|

P10835

第三个条件就是因为他很简单的原因,每个元素都相同,我们知道a \bigoplus a = 0 , 0 \bigoplus a = a,所以如果m是偶数的话,不管怎么样结果都是0,否则如果a_i=n结果就是n

#include <bits/stdc++.h>
using namespace std ;
int t , n , m ;
int main()
{
    cin >> t ;
    while(t --)
    {
        cin >> n >> m ;
        if(n == 0)
        {
            if(m % 2 == 0) cout << "Yes\n" ;
            else cout << "No\n" ; 
        }
        else
        {
            if(m % 2 == 0) cout << "No\n" ;
            else cout << "Yes\n" ;
        }
    }   
    return 0 ;
}

2.P14664

如果直接求解的话会很困难,所以我们可以求出最优的a,b,初始化a=n,b=0,后面从高位到低位贪心调整

用A_i,B_i表示a,b二进制的i位,ans表示a+b

  1. 如果A_i=0,因为1\bigoplus1=0,0\bigoplus0=0,所以当a+2^i\le n,b+2^i\le n时设A_i=B_i=1,ans \gets ans + a^{i+1}
  2. 如果A_i=1,当且仅当 |(a-2^i)-(b+2^i)|<|a-b|是把(A_i,B_i)=(1,0)\gets(0,1) ::::success[为什么正确] 2进制有个性质:2^x-\sum^{x-1}_{i=1}2^i,那么肯定从高位开始贪心更优,如果a,b越接近,越易满足低位扩容条件。 ::::

代码如下:

#include <bits/stdc++.h>
using namespace std;
long long t , n , b[40] , m ;
int main()
{
    ios::sync_with_stdio(0) ;
    cin.tie(0) ;
    cout.tie(0) ;
    cin >> t ;
    while(t --)
    {
        cin >> n ;
        m = 0 ;
        long long x = n , y = 0 , t = n ;
        while(n > 0)
        {
            b[++ m] = n % 2 ;
            n /= 2 ;
        }
        n = t ;
        for(int i = m ; i >= 1 ; i --)
        {
            long long p = (1 << (i - 1)) ;
            if(b[i] == 0 && x + p <= t && y + p <= t)
            {
                y += p;
                x += p;
            }
            else if(b[i] == 1)
            {
                if(abs(x - y) > abs((x - p) - (y + p)))
                {
                    x -= p;
                    y += p;
                }
            }
        }
        cout << x + y << "\n";
    }
    return 0;
}

3.T740903

首先异或有规律,设s=\bigoplus_{i=1}^{i=n} a_i,则s \bigoplus a-x=a_x(1\le x \le n)

我们可以手推几个数组就会发现其实他是一个结论题

  1. 如果n是奇数,那么如果k>0,数组只会变化一次({1,2,4}\to{6,5,3}\to{6,5,3}...)
  2. 否则(请参见样例)
    1. 当k是奇数,数组相当于变化一次
    2. 否则,相当于不变化
      #include <bits/stdc++.h>
      using namespace std ;
      long long t , n , a[100005] , k ;
      int main()
      {
      cin >> t ;
      while(t --)
      {
       long long sum = 0 ;
       cin >> n >> k ;
       for(int i = 1 ; i <= n ; i ++)
       {
           cin >> a[i] ;
           sum ^= a[i] ;
       }
       if(n % 2 == 0)
       {
           if(k % 2 == 0)
           {
               for(int i = 1 ; i <= n ; i ++)
               {
                   cout << a[i] << " " ;
               }
           }
           else
           {
               for(int i = 1 ; i <= n ; i ++)
               {
                   cout << (sum ^ a[i]) << " " ;
               }
           }
       }
       else
       {
           for(int i = 1 ; i <= n ; i ++)
           {
               cout << (sum ^ a[i]) << " " ;
           }
       }
       cout << "\n" ;
      }
      return 0 ;
      }

      ::::warning[经过本人现场考试WA总结的经验] 别只看样例的偶数就觉得自己懂了一切,你还是要手推一波 ::::

      4.T417748

      我们可以通过or,and和xor的定义和运算发现:一位当且仅当至少有2个1和1个0时可以发现这个机器这一位的工作方式,所以可以直接记录每一位1,0的数量在算出最多还需要多少张纸条

      #include <bits/stdc++.h>
      using namespace std ;
      int n , cnt1[105] , cnt0[105] , maxn , ans = 0 ;
      string s ;
      int main()
      {
      cin >> n ;
      while(n --)
      {
       cin >> s ;
       maxn = max(maxn , int(s.size())) ;
       for(int i = 0 ; i < s.size() ; i ++)
       {
           if(s[i] == '0') cnt0[i] ++ ;
           else cnt1[i] ++ ;
       }
      }
      for(int i = 0 ; i < maxn ; i ++)
      {
       if(cnt0[i] && cnt1[i] >= 2)
       {
           ans = max(ans , 0) ;
       }
       else if(cnt0[i])
       {
           ans = max(ans , 2 - cnt1[i]) ;
       }
       else if(cnt1[i] >= 2)
       {
           ans = max(ans , 1) ;
       }
       else
       {
           ans = max(ans , 3 - cnt1[i]) ;
       }
      }
      cout << ans ;
      return 0 ; 
      }

      ::::warning[易错点(本人没犯)] 认为机器所有位的工作方式都一样 ::::

      5.P7076

  3. 本题本质是二进制位运算,c和饲料编号为无用条件,直接忽略
  4. 用按位或统计所有动物已存在的二进制位,标记必须为0的二进制位(如果为1那么就有新增饲料),剩余自由位数量为 t
  5. 总情况=2^t-n(总数量-已有数量) ::::warning[特例] 当t=64,n=0,需要手动输出2^{64}的字符串 ::::
    #include <bits/stdc++.h>
    using namespace std ;
    unsigned long long n , m , c , k , a , p , q , flag , g , ans = 1 ;
    int main()
    {
     cin >> n >> m >> c >> k ;
     for(int i = 1 ; i <= n ; i ++)
     {
         cin >> a ;
         flag |= a ;
     }
     while(m --)
     {
         cin >> p >> q ;
         if((flag >> p & 1) == 0) g |= 1ULL << p ;
     }
     for(int i = 0 ; i < k ; i ++)
     {
         if((g >> i & 1) == 0)
         {
             ans <<= 1 ;
         }
     }
     if(ans == 0 && n == 0) cout << "18446744073709551616" ;
     else cout << ans - n ;
     return 0 ;
    }