Codeforces Round 900 (Div. 3)(A~E)题解

目录

A. How Much Does Daytona Cost?

B. Aleksa and Stack

C. Vasilije in Cacak

D. Reverse Madness

E. Iva & Pav


A. How Much Does Daytona Cost?

Solution:题目没说序列长度,因此只要有k就成立。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long  LL;
typedef unsigned long long uLL;
typedef pair PII;
const int N = 5e5+10;
const int inf = 0x3f3f3f3f;
template 
struct ModInt {
    const static int MD = T;
    int x;
    ModInt(LL x = 0)
        : x(x % MD) {}
    int get() { return x; }
    ModInt operator+(const ModInt& that) const {
        int x0 = x + that.x;
        return ModInt(x0 < MD ? x0 : x0 - MD);
    }
    ModInt operator-(const ModInt& that) const {
        int x0 = x - that.x;
        return ModInt(x0 < MD ? x0 + MD : x0);
    }
    ModInt operator*(const ModInt& that) const {
        return ModInt((long long)x * that.x % MD);
    }
    ModInt operator/(const ModInt& that) const {
        return *this * that.inverse();
    }
    void operator+=(const ModInt& that) {
        x += that.x;
        if (x >= MD)
            x -= MD;
    }
    void operator-=(const ModInt& that) {
        x -= that.x;
        if (x < 0)
            x += MD;
    }
    void operator*=(const ModInt& that) { x = (long long)x * that.x % MD; }
    void operator/=(const ModInt& that) { *this = *this / that; }
    ModInt inverse() const {
        int a = x, b = MD, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        if (u < 0)
            u += MD;
        return u;
    }
    friend ostream& operator<<(ostream& os, ModInt x) {
        os << x.get();
        return os;
    }
};
const int mod=998244353;
typedef ModInt mint;
LL gcd(LL a, LL b)
{
   return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
   int n,k;cin>>n>>k;vectora(n);
   mapmp;
   for(int i=0;i>a[i],mp[a[i]]++;
   if(mp[k]) cout<<"Yes"<<'\n';
else cout<<"No"<<'\n';
}
int main()
{
   ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
   int t;cin>>t;
   while(t--) solve();
}

B. Aleksa and Stack

Solution:构造题,一种构造方法是数足够大就可以,我赛时构造方法是,从1开始每一个数都%3等于1,即从1开始,公差为3的等差数列。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long  LL;
typedef unsigned long long uLL;
typedef pair PII;
const int N = 5e5+10;
const int inf = 0x3f3f3f3f;
template 
struct ModInt {
    const static int MD = T;
    int x;
    ModInt(LL x = 0)
        : x(x % MD) {}
    int get() { return x; }
    ModInt operator+(const ModInt& that) const {
        int x0 = x + that.x;
        return ModInt(x0 < MD ? x0 : x0 - MD);
    }
    ModInt operator-(const ModInt& that) const {
        int x0 = x - that.x;
        return ModInt(x0 < MD ? x0 + MD : x0);
    }
    ModInt operator*(const ModInt& that) const {
        return ModInt((long long)x * that.x % MD);
    }
    ModInt operator/(const ModInt& that) const {
        return *this * that.inverse();
    }
    void operator+=(const ModInt& that) {
        x += that.x;
        if (x >= MD)
            x -= MD;
    }
    void operator-=(const ModInt& that) {
        x -= that.x;
        if (x < 0)
            x += MD;
    }
    void operator*=(const ModInt& that) { x = (long long)x * that.x % MD; }
    void operator/=(const ModInt& that) { *this = *this / that; }
    ModInt inverse() const {
        int a = x, b = MD, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        if (u < 0)
            u += MD;
        return u;
    }
    friend ostream& operator<<(ostream& os, ModInt x) {
        os << x.get();
        return os;
    }
};
const int mod=998244353;
typedef ModInt mint;
LL gcd(LL a, LL b)
{
   return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
    int n;cin>>n;
    vectorans(n);
    ans[0]=1;
    for(int i=1;i>t;
   while(t--) solve();
}

C. Vasilije in Cacak

Solution:结论题,赛时打表找的规律。1-n个数里面选k个数,则[1+...+k,k+...+n-k+1]区间里所有的数都可以被构造出来。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long  LL;
typedef unsigned long long uLL;
typedef pair PII;
const int N = 5e5+10;
const int inf = 0x3f3f3f3f;
template 
struct ModInt {
    const static int MD = T;
    int x;
    ModInt(LL x = 0)
        : x(x % MD) {}
    int get() { return x; }
    ModInt operator+(const ModInt& that) const {
        int x0 = x + that.x;
        return ModInt(x0 < MD ? x0 : x0 - MD);
    }
    ModInt operator-(const ModInt& that) const {
        int x0 = x - that.x;
        return ModInt(x0 < MD ? x0 + MD : x0);
    }
    ModInt operator*(const ModInt& that) const {
        return ModInt((long long)x * that.x % MD);
    }
    ModInt operator/(const ModInt& that) const {
        return *this * that.inverse();
    }
    void operator+=(const ModInt& that) {
        x += that.x;
        if (x >= MD)
            x -= MD;
    }
    void operator-=(const ModInt& that) {
        x -= that.x;
        if (x < 0)
            x += MD;
    }
    void operator*=(const ModInt& that) { x = (long long)x * that.x % MD; }
    void operator/=(const ModInt& that) { *this = *this / that; }
    ModInt inverse() const {
        int a = x, b = MD, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        if (u < 0)
            u += MD;
        return u;
    }
    friend ostream& operator<<(ostream& os, ModInt x) {
        os << x.get();
        return os;
    }
};
const int mod=998244353;
typedef ModInt mint;
LL gcd(LL a, LL b)
{
   return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
LL n,k,x;
vectora(N);
bool fl=false;
void select(int n,int m){
   if(m==0){
      LL sum=0;
      for(int i=1;i<=k;i++){
         sum+=a[i];
         //cout<>n>>k>>x;
    LL l=0,r=0;
    l=(1+k)*k/2,r=(n+n-k+1)*k/2;
    if(x>=l&&x<=r) cout<<"Yes"<<'\n';
    else cout<<"No"<<'\n';
}
int main()
{
   ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
   int t;cin>>t;
   while(t--) solve();
}

D. Reverse Madness

Solution:赛时过的很少,但其实题意看懂很简单。题目给出一个字符串,以及k个区间(题目保证不重叠),然后q个询问,每次给出一个位置,一定在一个区间[l,r]内,然后翻转[min(x,l+r-x],max(x,l+r-x)],不难看出翻转的区间关于(l+r)/2对称,因此swap(s[x],s[l+r-x])即可,即枚举半个区间长度即可。对于是否翻转,还有一个技巧就是异或,如果对称的两个点异或值为1则翻转了奇数次,需要翻转,否则不需要翻转。最终复杂度o(n)。

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
using namespace std;
typedef long long  LL;
typedef unsigned long long uLL;
typedef pair PII;
const int N = 5e5+10;
const int inf = 0x3f3f3f3f;
template 
struct ModInt {
    const static int MD = T;
    int x;
    ModInt(LL x = 0)
        : x(x % MD) {}
    int get() { return x; }
    ModInt operator+(const ModInt& that) const {
        int x0 = x + that.x;
        return ModInt(x0 < MD ? x0 : x0 - MD);
    }
    ModInt operator-(const ModInt& that) const {
        int x0 = x - that.x;
        return ModInt(x0 < MD ? x0 + MD : x0);
    }
    ModInt operator*(const ModInt& that) const {
        return ModInt((long long)x * that.x % MD);
    }
    ModInt operator/(const ModInt& that) const {
        return *this * that.inverse();
    }
    void operator+=(const ModInt& that) {
        x += that.x;
        if (x >= MD)
            x -= MD;
    }
    void operator-=(const ModInt& that) {
        x -= that.x;
        if (x < 0)
            x += MD;
    }
    void operator*=(const ModInt& that) { x = (long long)x * that.x % MD; }
    void operator/=(const ModInt& that) { *this = *this / that; }
    ModInt inverse() const {
        int a = x, b = MD, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        if (u < 0)
            u += MD;
        return u;
    }
    friend ostream& operator<<(ostream& os, ModInt x) {
        os << x.get();
        return os;
    }
};
const int mod=998244353;
typedef ModInt mint;
LL gcd(LL a, LL b)
{
   return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
   int n,k;cin>>n>>k;
   string s;cin>>s;
   vectorl(k),r(k);
   for(int i=0;i>l[i],l[i]--;
   for(int i=0;i>r[i],r[i]--;
   int o;cin>>o;int op;
   vectorf(n);
  for(int i=0;i>op;op--;
      f[op]^=1;
  }
  for(int i=0;i>t;
   while(t--) solve();
}

E. Iva & Pav

Solution:区间与运算明显单调递减,因此,可以二分,左端点确定,二分右端点,chk的时候如果直接遍历区间会tle,因此拆位维护1的数量,对于区间[l,r],count(1)=(r-l+1)时,这个位置才有贡献,因此每次chk循环三十次即可,也就是O(1)复杂度。

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
using namespace std;
typedef long long  LL;
typedef unsigned long long uLL;
typedef pair PII;
const int N = 2e5+10;
const int inf = 0x3f3f3f3f;
template 
struct ModInt {
    const static int MD = T;
    int x;
    ModInt(LL x = 0)
        : x(x % MD) {}
    int get() { return x; }
    ModInt operator+(const ModInt& that) const {
        int x0 = x + that.x;
        return ModInt(x0 < MD ? x0 : x0 - MD);
    }
    ModInt operator-(const ModInt& that) const {
        int x0 = x - that.x;
        return ModInt(x0 < MD ? x0 + MD : x0);
    }
    ModInt operator*(const ModInt& that) const {
        return ModInt((long long)x * that.x % MD);
    }
    ModInt operator/(const ModInt& that) const {
        return *this * that.inverse();
    }
    void operator+=(const ModInt& that) {
        x += that.x;
        if (x >= MD)
            x -= MD;
    }
    void operator-=(const ModInt& that) {
        x -= that.x;
        if (x < 0)
            x += MD;
    }
    void operator*=(const ModInt& that) { x = (long long)x * that.x % MD; }
    void operator/=(const ModInt& that) { *this = *this / that; }
    ModInt inverse() const {
        int a = x, b = MD, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        if (u < 0)
            u += MD;
        return u;
    }
    friend ostream& operator<<(ostream& os, ModInt x) {
        os << x.get();
        return os;
    }
};
const int mod=998244353;
typedef ModInt mint;
LL gcd(LL a, LL b)
{
   return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
int n;
vectorans(N);
LL s[N][30];
bool chk(int x,int l,int k){
    LL sum=0,cnt=x-l+1;
    //cout<=k) {
        return true;
    }
     else return false;
}
void solve()
{
   cin>>n;
   for(int i=1;i<=n;i++) {
       cin>>ans[i];
       for(int j=0;j<30;j++) s[i][j]=0;
}
   int q;cin>>q;
   for(int i=1;i<=n;i++){
    int cnt=0;
        for(int j=0;j<30;j++){
             if(ans[i]>>j&1) {
                s[i][j]=s[i-1][j]+1;
            }
        }
    }
    LL k,l;
   while(q--){
       cin>>l>>k;
       int r=n;
       int st=l;
       if(ans[l]>1;
         if(chk(mid,st,k)){
              l=mid;
         }else{
            r=mid-1;
         }
       }
       cout<>t;
   while(t--) solve();
}

你可能感兴趣的:(codeforces,c++,算法)