牛客小白月赛76

好像上周练习赛忘记写题解了....最近vp的有点多懒得写题解了,见谅0.0

A.究极博弈输出自己即可

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int N =1e6+10,mod=1e9+7;

typedef pair PII;

int n,m;


void solve(){
    string s;
    cin>>s;
    cout<>t;
    while(t--) solve();
    return 0;
}

B.因为能复制就是变成两倍,跟二进制有关的

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int N =1e6+10,mod=1e9+7;
#define int long long
typedef pair PII;

int n,m;


void solve(){
    cin>>n;
    int ans=1,res=0;
    if(n<=1){
        cout<<"0\n";return ;
    }
    while(1){
        res++;
        ans+=ans;
        if(ans>=n) break;
    }
    cout<>t;
    while(t--) solve();
    return 0;
}

C.首先a%b=m 直接让a=m就行,b=n-m就行,然后看题目条件判断a b就行

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int N =1e6+10,mod=1e9+7;
#define int long long
typedef pair PII;

int n,m;


void solve(){
    cin>>n>>m;
    
    int a=m,b=n-m;
    if(b<=0||a%b!=m||b>1e9||a<0||a>1e9){
        cout<<"-1\n";
        return ;
    }
    cout<>t;
    while(t--) solve();
    return 0;
}

D.对于0和1直接相加即可,等大于2的时候后面除了1 0全部相乘肯定是最优的

但是题目ai=1e9 1e9的2e5次方巨大,所以等大于2的时候可以进行mod运算,

import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect

BUFSIZE = 4096


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def I():
    return input()

def II():
    return int(input())

def MI():
    return map(int, input().split())

def LI():
    return list(input().split())

def LII():
    return list(map(int, input().split()))

def GMI():
    return map(lambda x: int(x) - 1, input().split())

def LGMI():
    return list(map(lambda x: int(x) - 1, input().split()))

def solve():
    x=0
    mod=998244353
    n=II()
    a=LII()
    f=0
    for i in a:
        if i==0:continue
        if i==1:
            x+=1
            continue
        if x>=2:
            f=1
        if f==1:
            x*=i
            x=x%mod
        else:
            x+=i
    print(x%mod)
    
if __name__ == '__main__':
    for _ in range(II()):
        solve()

E.

首先括号序列过程要保证大于等于0的,即(括号大于等于)括号

我们先入队(括号

如果当前出现了)括号,且等于过程中的计数是小于0的,那么就是不合法,从后面位置里面找一个(括号调换即可,为啥不从前面找,因为前面都已经合法了别动他了,动了代价更大

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int N =2e5+10,mod=998244353;
#define int long long
typedef pair PII;

int n,m;
int a[N];
void solve(){
    string s;
    cin>>n>>s;
    queue q;
    int l=0,r=0;
    for(int i=0;i>t;
    while(t--) solve();
    return 0;
}

F.首先get函数里面就是把矩阵变成m列,m列里面哪一行变成x需要的代价最小

复杂度是nlogn别想成平方了

然后看题目范围最多2e5 所以分成1000列,肯定有多的一列只有一个数,只要把当前列变成k就行

所以直接枚举1000即可

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int N =2e5+10,mod=998244353;

typedef pair PII;

int n,m,k;
int a[N];
void solve(){
    cin>>n>>m>>k;
    int res=2e9;
    for(int i=1;i<=n;i++) cin>>a[i];
    auto get=[&](int m){
        int ans=0x3f3f3f3f;
        for(int i=1;i<=m;i++)
        {
            int cnt=0;
            for(int j=i;j<=n;j+=m)
            {
                if(a[j]!=k) cnt++;
            }
            ans=min(ans,cnt);
        }
        return ans;
    };
    for(int i=max(1,m-2000);i<=m+2000;i++)
    {
        int t=get(i);
        res=min(res,t+abs(i-m));
    }
    cout<>t;
    while(t--) solve();
    return 0;
}

G:

首先除法根据费马小定理除x=qmi(x,mod-2,mod)

逆元那部分知识

然后就枚举当前有多少个数,然后其实就没了...记忆化搜索

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const int N =2e5+10,mod=998244353;
#define int long long
typedef pair PII;
typedef long long LL;
int n,m,k;
int a[N];
unordered_map mp;
int qmi(int a, int k, int p)  // 求a^k mod p
{
    int res = 1 % p;
    while (k)
    {
        if (k & 1) res = (LL)res * a % p;
        a = (LL)a * a % p;
        k >>= 1;
    }
    return res;
}

void solve(){
    cin>>n;
    function dfs=[&](int now){
        if(mp.count(now)) return mp[now];
        return mp[now]=(1ll+(1ll*dfs((now+1)/2ll)*((now+1)/2ll)+1ll*dfs(now/2ll)*(now/2ll))%mod*qmi(now,mod-2,mod))%mod;
    };
    cout<>t;
    while(t--) solve();
    return 0;
}

你可能感兴趣的:(算法)