In mathematics you don't understand things, you just get used to them.

标签 implementation 下的文章

「ABC 196A」Difference Max

Link.

略。

#include<cstdio>
long long a,b,c,d;
int main(){
    scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
    printf("%lld\n",b-c);
    return 0;
}

「ABC 196B」Round Down

Link.

略。

#include<cstdio>
#include<cstring>
char s[10000];
int main(){
    scanf("%s",s);int len=strlen(s);
    for(int i=0;i<len;++i)if(s[i]^'.')putchar(s[i]);else break;
    return 0;
}

「ABC 196C」Doubled

Link.

分类讨论即可,可能会有点点细节需要注意。

#include<cstdio>
#include<algorithm>
using namespace std;
long long n;
int dig[20],cnt;
long long qpow(long long bas,long long fur){long long res=0;for(long long i=1;i<=fur;++i)res=res*10+9;return res;}
long long getnum(int l,int r){long long res=0;for(int i=r;i>=l;--i)res=res*10+dig[i];return res;}
int main(){
    scanf("%lld",&n);long long bk=n;do dig[++cnt]=bk%10,bk/=10; while(bk);
    if(cnt==1)return puts("0"),0;int lm=(cnt>>1);
    long long pre=getnum(cnt-lm+1,cnt),suf=getnum(1,lm);
    if(cnt&1)printf("%lld\n",qpow(9,lm));
    else{
        if(pre<=suf)printf("%lld\n",pre);
        else printf("%lld\n",pre-1);
    }
    return 0;
}
/*
23333

3 3 3 3 2

232
*/

「ABC 196D」Hanjo

Link.

暴搜。

#include<iostream>
using namespace std;
int h,w,a,b,ans;
void dfs(int solvedNumber,int stateBoard,int leftLongerBlock,int leftCenterBlock)
{
    if(solvedNumber==h*w)    ++ans;
    else
    {
        if(stateBoard&(1<<solvedNumber))    return dfs(solvedNumber+1,stateBoard,leftLongerBlock,leftCenterBlock);
        if(leftLongerBlock)
        {
            if((solvedNumber%w!=w-1)&&(!(stateBoard&(1<<(solvedNumber+1)))))    dfs(solvedNumber+1,stateBoard|(1<<solvedNumber)|(1<<(solvedNumber+1)),leftLongerBlock-1,leftCenterBlock);
            if(solvedNumber+w<h*w)    dfs(solvedNumber+1,stateBoard|(1<<solvedNumber)|(1<<(solvedNumber+w)),leftLongerBlock-1,leftCenterBlock);
        }
        if(leftCenterBlock)    dfs(solvedNumber+1,stateBoard|(1<<solvedNumber),leftLongerBlock,leftCenterBlock-1);
    }
}
int main()
{
    cin >> h >> w >> a >> b;
    dfs(0,0,a,b); cout << ans << "\n";
    return 0;
}

「ABC 196E」Filters

Link.

这是个 Segment Tree Beats 的板子,不打了。

// Oops, something went wrong.

「ABC 196F」Substring 2

Link.

你 ABC 考 FFT 字符串匹配。

定义匹配函数 $f(x)=\sum_{i=0}^{|T|-1}(S_{x+i}-T_{i})^{2}=\sum_{i=0}^{|T|-1}S^{2}_{x+i}-2\sum_{i=0}^{|T|-1}S_{x+i}T_{i}+\sum_{i=0}^{|T|-1}T_{i}^{2}$。

然后反转 $T$ 卷积即可。

#include<cstdio>
#include<numeric>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MOD=998244353,INF=numeric_limits<int>::max();
void exGCD(int one,int ano,int &x,int &y)
{
    if(ano==0)    x=1,y=0;
    else    exGCD(ano,one%ano,y,x),y-=(one/ano)*x;
}
int getInv(int val){int res,w; exGCD(val,MOD,res,w); return (res+MOD)%MOD;}
int qpow(int bas,int fur)
{
    int res=1;
    while(fur)
    {
        if(fur&1)    res=LL(res)*bas%MOD;
        bas=LL(bas)*bas%MOD;
        fur>>=1;
    }
    return res%MOD;
}
namespace Poly
{
    typedef vector<int> poly;
    #define len(x) (int((x).size()))
    int lim,rev[4000010];
    void ntt(poly &f,int op)
    {
        for(int i=0;i<lim;++i)    if(i<rev[i])    swap(f[i],f[rev[i]]);
        for(int len=2;len<=lim;len<<=1)
        {
            int bas=qpow(op==1?3:332748118,(MOD-1)/len);
            for(int fr=0;fr<lim;fr+=len)
            {
                int now=1;
                for(int ba=fr;ba<fr+(len>>1);++ba,now=LL(now)*bas%MOD)
                {
                    int tmp=LL(now)*f[ba+(len>>1)]%MOD;
                    f[ba+(len>>1)]=(f[ba]-tmp+MOD)%MOD;
                    f[ba]=(f[ba]+tmp)%MOD;
                }
            }
        }
        if(op==-1)
        {
            int tmp=getInv(lim);
            for(int i=0;i<lim;++i)    f[i]=LL(f[i])*tmp%MOD;
        }
    }
    poly operator*(poly f,poly g)
    {
        int n=len(f)+len(g)-1; lim=1;
        while(lim<n)    lim<<=1;
        f.resize(lim),g.resize(lim);
        for(int i=0;i<lim;++i)    rev[i]=(rev[i>>1]>>1)|((i&1)?(lim>>1):0);
        ntt(f,1),ntt(g,1);
        for(int i=0;i<lim;++i)    f[i]=LL(f[i])*g[i]%MOD;
        ntt(f,-1),f.resize(n);
        return f;
    }
    poly operator*(int x,poly f){for(int i=0;i<len(f);++i)    f[i]=LL(f[i])*x%MOD; return f;}
    poly operator-(poly f,poly g)
    {
        int n=max(len(f),len(g));
        f.resize(n),g.resize(n);
        for(int i=0;i<len(f);++i)    f[i]=(f[i]-g[i]+MOD)%MOD;
        return f;
    }
    poly operator+(poly f,poly g)
    {
        int n=max(len(f),len(g));
        f.resize(n),g.resize(n);
        for(int i=0;i<len(f);++i)    f[i]=(f[i]+g[i])%MOD;
        return f;
    }
}using namespace Poly;
int main()
{
    string S,T;
    cin >> S >> T; reverse(T.begin(),T.end());
    poly onesi,anosi,onexsi,anoxsi;
    #define Sqr(x) ((LL)(x)*(x)%MOD)
    onesi.push_back(Sqr((*S.begin())-'0'));
    anosi.push_back(Sqr((*T.begin())-'0'));
    for(int i=1;i<len(S);++i)    onesi.push_back(onesi.back()+Sqr(S[i]-'0'));
    for(int i=1;i<len(T);++i)    anosi.push_back(anosi.back()+Sqr(T[i]-'0'));
    for(char c : S)    onexsi.push_back(c-'0'); for(char c : T)    anoxsi.push_back(c-'0');
    poly tmp=2*onexsi*anoxsi; int ans=INF;
    #define getValue(i) (((i)<(len(T)))?0:onesi[(i)-len(T)])
    for(unsigned int i=T.size()-1;i<S.size();++i)    ans=min(ans,onesi[i]-getValue(i)+anosi[len(T)-1]-tmp[i]);
    printf("%d\n",ans);
    return 0;
}

Description

Link.

Summarizing the fucking statement is the last thing in the world I ever want to do.

Solution

我们来重新描述一些概念:

  • 数字牌:筒条万。
  • 顺子:$3$ 张连续的数字牌。(面子)
  • 雀头:$2$ 张完全一样的牌。
  • 刻子:$3$ 张完全一样的牌。(面子)
  • 杠子:$4$ 张完全一样的牌。

观察发现:杠子在除了 $14$ 张牌胡牌情况以外的胡牌情况都出现了,于是又发现:$\binom{4}{3}>\binom{4}{4}$;

于是:

  • 对于胡牌的形式,我们只需要考虑「$3\times4+2$」「七对子」和「国士无双」。

于是我们只需要三种情况取最大即可。

  1. 「七对子」只需要把所有牌型的贡献拉出来,取前 $7$ 个最大的乘起来即可。
  2. 「国士无双」枚举谁来做 $2$ 个的那个,然后取最大。
  3. 「$3\times4+2$」

$\qquad$ 考虑这样一个 DP,设:$f(i,j,k,a,b,c)$ 为前 $i$ 个数,$j$ 个面子,$k$ 个雀头,第 $i$ / $i+1$ / $i+2$ 张牌用了 $a$ / $b$ / $c$ 张的最优答案($k\in\{0,1\}$)。

$\qquad$ $\qquad$ 1. 对于雀头:

$$ f(i,j,1,a+2,b,c)=\max\{\frac{f(i,j,0,a,b,c)\times\text{calc}(i,a+2)}{\text{calc}(i,a)}\} $$

$\qquad$ $\qquad$ $\qquad$ 其中 $\text{calc}(x,y)$ 为第 $x$ 张牌,在手牌中需要出现 $y$ 次,此时对答案的贡献。

$\qquad$ $\qquad$ $\qquad$ 方程的意义即:去掉把第 $i$ 种牌之前的贡献,再算上现在算作雀头的贡献。

$\qquad$ $\qquad$ 2. 对于刻子:

$$ f(i,j+1,0\text{ or }1,a+3,b,c)=\max\{\frac{f(i,j,0\text{ or }1,a,b,c)\times\text{calc}(i,a+3)}{\text{calc}(i,a)}\} $$

$\qquad$ $\qquad$ $\qquad$ 基本同上。

$\qquad$ $\qquad$ 3. 对于顺子:

$$ f(i,j+1,0\text{ or }1,a+1,b+1,c+1)=\max\{\frac{f(i,j,0\text{ or }1,a,b,c)\times\text{calc}(i,a+1)\times\text{calc}(i+1,b+1)\times\text{calc}(i+2,c+1)}{\text{calc}(i,a)\times\text{calc}(i+1,b)\times\text{calc}(i+2,c)}\} $$

$\qquad$ $\qquad$ $\qquad$ 完全同理。

然后就完了。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL koshi[15]={0,1,9,10,18,19,27,28,29,30,31,32,33,34}; // the cards that Kokushimusou needs
const bool chunko[36]={0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1}; // the cards which are able to be jyunko
LL getCard()
{
    static char s[10];
    scanf("%s",s+1);
    if(s[1]=='0')    return -1;
    else if(!isdigit(s[1]))
    {
        if(s[1]=='E')    return 28;
        else if(s[1]=='S')    return 29;
        else if(s[1]=='W')    return 30;
        else if(s[1]=='N')    return 31;
        else if(s[1]=='Z')    return 32;
        else if(s[1]=='B')    return 33;
        else    return 34;
    }
    else
    {
        if(s[2]=='m')    return s[1]-'0';
        else if(s[2]=='p')    return s[1]-'0'+9;
        else    return s[1]-'0'+18;
    }
}
LL t,comb[10][10],cnt[40],trs[40],f[40][5][5][5][5][5];
#define calc(x,f) (((cnt[x])<(f)?0:comb[cnt[x]][f])*(trs[x]?(1<<(f)):1))
LL onesolve() // Seven Pairs
{
    vector<LL> pri;
    for(LL i=1;i<=34;++i)    if(cnt[i]>=2)    pri.push_back(calc(i,2));
    sort(pri.begin(),pri.end(),greater<LL>());
    if(pri.size()<size_t(7))    return 0;
    else
    {
        LL res=7;
        for(LL i=0;i<7;++i)    res*=pri[i];
        return res;
    }
}
LL anosolve() // Kokushimusou
{
    LL flag=0;
    for(LL i=1;i<=13;++i)
    {
        if(cnt[koshi[i]]>=2)    flag=1;
        if(cnt[koshi[i]]==0)    return 0;
    }
    if(flag)
    {
        LL res=0;
        for(LL i=1;i<=13;++i)
        {
            LL tmp=13;
            if(cnt[koshi[i]]>=2)
            {
                for(LL j=1;j<=13;++j)    tmp*=calc(koshi[j],(i==j)+1);
            }
            res=max(res,tmp);
        }
        return res;
    }
    else    return 0;
}
void getmax(LL &one,LL ano){one=max(one,ano);}
LL exsolve() // 3x4+2
{
    #define f(i,j,k,a,b,c) (f[i][j][k][a][b][c])
    f(1,0,0,0,0,0)=1;
    for(LL i=1;i<=34;++i)
    {
        for(LL j=0;j<=4;++j)
        {
            for(LL a=0;a<=4;++a)
            {
                for(LL b=0;b<=2;++b)
                {
                    for(LL c=0;c<=2;++c)
                    {
                        if(cnt[i]-a>=2)    getmax(f(i,j,1,a+2,b,c),f(i,j,0,a,b,c)/calc(i,a)*calc(i,a+2));
                        if(j^4)
                        {
                            if(cnt[i]-a>=3)
                            {
                                getmax(f(i,j+1,0,a+3,b,c),f(i,j,0,a,b,c)/calc(i,a)*calc(i,a+3));
                                getmax(f(i,j+1,1,a+3,b,c),f(i,j,1,a,b,c)/calc(i,a)*calc(i,a+3));
                            }
                            if(chunko[i]&&cnt[i]-a>=1&&cnt[i+1]-b>=1&&cnt[i+2]-c>=1&&(b^2)&&(c^2))
                            {
                                getmax(f(i,j+1,0,a+1,b+1,c+1),f(i,j,0,a,b,c)/calc(i,a)/calc(i+1,b)/calc(i+2,c)*calc(i,a+1)*calc(i+1,b+1)*calc(i+2,c+1));
                                getmax(f(i,j+1,1,a+1,b+1,c+1),f(i,j,1,a,b,c)/calc(i,a)/calc(i+1,b)/calc(i+2,c)*calc(i,a+1)*calc(i+1,b+1)*calc(i+2,c+1));
                            }
                        }
                        getmax(f(i+1,j,0,b,c,0),f(i,j,0,a,b,c));
                        getmax(f(i+1,j,1,b,c,0),f(i,j,1,a,b,c));
                    }
                }
            }
        }
    }
    LL res=0;
    for(LL i=1;i<=34;++i)
    {
        for(LL a=0;a<=4;++a)
        {
            for(LL b=0;b<=2;++b)
            {
                for(LL c=0;c<=2;++c)    getmax(res,f(i,4,1,a,b,c));
            }
        }
    }
    #undef f
    return res;
}
int main()
{
    for(LL i=0;i<=4;++i)    comb[i][0]=comb[i][i]=1;
    for(LL i=1;i<=4;++i)    for(LL j=1;j<i;++j)    comb[i][j]=comb[i-1][j-1]+comb[i-1][j];
    scanf("%lld",&t);
    while(t--)
    {
        memset(f,0,sizeof(f));
        for(LL i=1;i<=34;++i)    cnt[i]=4,trs[i]=0;
        LL tmp=getCard();
        while(~tmp)    --cnt[tmp],tmp=getCard();
        tmp=getCard();
        while(~tmp)    trs[tmp]=1,tmp=getCard();
        printf("%lld\n",max(max(onesolve(),anosolve()),exsolve()));
    }
    return 0;
}

「ARC 113A」A*B*C

Link.

就是算 $\sum_{i=1}^{k}\sum_{j=1}^{\lfloor\frac{k}{i}\rfloor}\lfloor\frac{k}{j\times j}\rfloor$。

直接调和级数。

#include<cstdio>
long long k;
int main()
{
    scanf("%lld",&k);
    long long ans=0;
    for(long long i=1;i<=k;++i)
    {
        for(long long j=1;j<=k/i;++j)    ans+=(k/i/j);
    }
    printf("%lld\n",ans);
    return 0;
}

「ARC 113B」A^B^C

Link.

扩展欧拉定理裸题,$A^{B^{C}}\bmod10=A^{(B^{C}\bmod\varphi(10))+\varphi(10)}\bmod10$。

#include<cstdio>
long long getphi(long long x)
{
    long long res=x;
    for(long long i=2;i*i<=x;++i)
    {
        if(x%i==0)
        {
            res=res/i*(i-1);
            while(x%i==0)    x/=i;
        }
    }
    if(x>1)    res=res/x*(x-1);
    return res;
}
long long cqpow(long long bas,long long fur,long long mod)
{
    long long res=1;
    while(fur)
    {
        if(fur&1)    res=res*bas%mod;
        bas=bas*bas%mod;
        fur>>=1;
    }
    return res;
}
long long a,b,c;
int main()
{
    scanf("%lld %lld %lld",&a,&b,&c);
    printf("%lld\n",cqpow(a,cqpow(b,c,getphi(10))+getphi(10),10));
    return 0;
}

「ARC 113C」String Invasion

Link.

正序枚举 $i\in[1,n]$,如果满足条件,那么后面的字符串都可以执行操作,则 $ans:=ans+n-i$。

当然,由于后面可能存在一个字符就是 $s_{i}$,所以要记录当前操作的字符,特判 $ans:=ans-1$。

#include<cstdio>
#include<cstring>
int n;
long long ans;
char s[200010];
int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    char las=0;
    for(int i=1;i<=n;++i)
    {
        if(i!=n&&s[i]==s[i+1]&&s[i]!=s[i+2]&&s[i]!=las)    ans+=n-i,las=s[i];
        else if(s[i]==las)    --ans;
    }
    printf("%lld\n",ans);
    return 0;
}

「ARC 113D」Sky Reflector

Link.

显然只要 $\forall i\in[1,m],b_{i}\ge a_{\max}$ 即可,那么枚举 $i\in[1,k]=a_{\max}$,有:

$$ ans=\sum_{i=1}^{k}(i^{n}-(i-1)^{n})\times(k-i+1)^{m}\bmod998244353 $$

#include<cstdio>
const int mod=998244353;
long long cqpow(long long bas,int fur)
{
    long long res=1;
    while(fur)
    {
        if(fur&1)    res=res*bas%mod;
        bas=bas*bas%mod;
        fur>>=1;
    }
    return res;
}
int n,m,k;
long long ans;
int main()
{
    scanf("%d %d %d",&n,&m,&k);
    if(n==1)    ans=cqpow(k,m);
    else if(m==1)    ans=cqpow(k,n);
    else
    {
        for(int i=1;i<=k;++i)    ans=(ans+((cqpow(i,n)-cqpow(i-1,n)+mod)%mod)*cqpow(k-i+1,m)%mod)%mod;
    }
    printf("%lld\n",ans);
    return 0;
}

「ARC 113E」Rvom and Rsrev

Link.

「ARC 113F」Social Distance

Link.

「ABC 113A」Star

Link.

略。

#include<cstdio>
int x;
int main()
{
    scanf("%d",&x);
    for(int i=1;;++i)
    {
        if((x+i)%100==0)
        {
            printf("%d\n",i);
            break;
        }
    }
    return 0;
}

「ABC 192B」uNrEaDaBlE sTrInG

Link.

略。

#include<cstdio>
#include<cstring>
char s[1010];
int main()
{
    scanf("%s",s+1);
    bool flag=1;
    int n=strlen(s+1);
    for(int i=1;i<=n;++i)
    {
        if(i&1)
        {
            if(s[i]<'a'||s[i]>'z')
            {
                flag=0;
                break;
            }
        }
        else
        {
            if(s[i]<'A'||s[i]>'Z')
            {
                flag=0;
                break;
            }
        }
    }
    printf("%s\n",flag?"Yes":"No");
    return 0;
}

「ABC 192C」Kaprekar Number

Link.

照题意模拟。

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
long long las,now,n;
int k;
long long f(long long x)
{
    long long one=0,ano=0;
    vector<long long> num;
    while(x>0)
    {
        num.push_back(x%10);
        x/=10;
    }
    sort(num.begin(),num.end(),greater<long long>());
    for(auto val:num)    one=one*10+val;
    reverse(num.begin(),num.end());
    for(auto val:num)    ano=ano*10+val;
//    printf("%lld %lld\n",one,ano);
    return one-ano;
}
int main()
{
    scanf("%lld %d",&n,&k);
    las=n;
    if(k==0)    return printf("%lld\n",n)&0;
    while(k--)
    {
        now=f(las);
        las=now;
    }
    printf("%lld\n",now);
    return 0;
}

「ABC 192D」Base n

Link.

显然随着进制增大数字也会增大,所以可以二分。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct bigInt : vector<long long>{
    bigInt &check( ){
        while( ! empty( ) && ! back( ) ) pop_back( );
        if( empty( ) )    return *this;
        for( unsigned i = 1; i < size( ); ++ i ){ ( *this )[i] += ( *this )[i - 1] / 10; ( *this )[i - 1] %= 10; }
        while( back( ) >= 10 ){ push_back( back( ) / 10 ); ( *this )[size( ) - 2] %= 10; }
        return *this;
    }
    bigInt( long long tpN = 0 ){ push_back( tpN ); check( ); }
};
istream &operator >> ( istream &is, bigInt &tpN ){
    string s;
    is >> s; tpN.clear( );
    for( int i = s.size( ) - 1; i >= 0; --i ) tpN.push_back( s[i] - '0' );
    return is;
}
ostream &operator << ( ostream &os, const bigInt &tpN ){
    if( tpN.empty( ) )    os << 0;
    for( int i = tpN.size( ) - 1; i >= 0; --i )    os << tpN[i];
    return os;
}
bool operator != ( const bigInt &one, const bigInt &another ){
    if( one.size( ) != another.size( ) )    return 1;
    for( int i = one.size( ) - 1; i >= 0; --i ){
        if( one[i] != another[i] )    return 1;
    }
    return 0;
}
bool operator == ( const bigInt &one, const bigInt &another ){
    return ! ( one != another );
}
bool operator < ( const bigInt &one, const bigInt &another ){
    if( one.size( ) != another.size( ) )    return one.size( ) < another.size( );
    for( int i = one.size( ) - 1; i >= 0; --i ){
        if( one[i] != another[i] )    return one[i] < another[i];
    }
    return 0;
}
bool operator > ( const bigInt &one, const bigInt &another ){ return another < one; }
bool operator <= ( const bigInt &one, const bigInt &another ){ return ! (one > another ); }
bool operator >= ( const bigInt &one, const bigInt &another ){ return ! (one < another ); }
bigInt &operator += ( bigInt &one, const bigInt &another ){
    if( one.size( ) < another.size( ) )    one.resize(another.size( ) );
    for( unsigned i = 0; i != another.size( ); ++ i ) one[i] += another[i];
    return one.check( );
}
bigInt operator + ( bigInt one, const bigInt &another ){ return one += another; }
bigInt &operator -= ( bigInt &one, bigInt another ){
    if( one < another )    swap( one, another );
    for( unsigned i = 0; i != another.size( ); one[i] -= another[i], ++ i ){
        if( one[i] < another[i] ){
            unsigned j = i + 1;
            while( ! one[j] ) ++ j;
            while( j > i ){ -- one[j]; one[--j] += 10; }
        }
    }
    return one.check( );
}
bigInt operator - ( bigInt one, const bigInt &another ){ return one -= another; }
bigInt operator * ( const bigInt &one, const bigInt &another ){
    bigInt tpN;
    tpN.assign( one.size( ) + another.size( ) - 1, 0 );
    for( unsigned i = 0; i != one.size( ); ++ i ){
        for( unsigned j = 0; j != another.size( ); ++ j ) tpN[i + j] += one[i] * another[j];
    }
    return tpN.check( );
}
bigInt &operator *= ( bigInt &one, const bigInt &another ){ return one = one * another; }
bigInt divMod( bigInt &one, const bigInt &another ){
    bigInt ans;
    for( int t = one.size( ) - another.size( ); one >= another; -- t ){
        bigInt tpS;
        tpS.assign( t + 1, 0 );
        tpS.back( ) = 1;
        bigInt tpM = another * tpS;
        while( one >= tpM ){ one -= tpM; ans += tpS; }
    }
    return ans;
}
bigInt operator / ( bigInt one, const bigInt &another ){ return divMod(one, another ); }
bigInt &operator /= ( bigInt &one, const bigInt &another ){ return one = one / another; }
bigInt &operator %= ( bigInt &one, const bigInt &another ){ divMod( one, another ); return one; }
bigInt operator % ( bigInt one, const bigInt &another ){ return one %= another; }
char s[70];
int n,cntot;
bigInt m,num[70],mx;
bool check(bigInt bas)
{
    bigInt res=0,sab=1;
    for(int i=1;i<=cntot;++i)
    {
        res+=num[i]*sab;
        sab*=bas;
        if(res>m)    return false;
    }
    return true;
}
int main()
{
    cin>>(s+1)>>m;
    n=strlen(s+1);
    for(int i=n;i>=1;--i)
    {
        num[++cntot]=s[i]-'0';
        mx=max(mx,num[cntot]);
    }
    if(cntot==1)    cout<<(num[cntot]<=m)<<"\n";
    else
    {
//        bigInt l=0,r=1e18,ans=0;
//        while(l<=r)
//        {
//            bigInt mid=(l+r)/2;
//            if(check(mid))
//            {
//                l=mid+1;
//                ans=mid;
//            }
//            else    r=mid-1;
//        }
//        bigInt l=mx,r=m+1;
//        while(l+1<r)
//        {
//            bigInt mid=(l+r)/2;
//            if(check(mid))    l=mid;
//            else    r=mid;
//        }
        bigInt l=mx+1,r=m+1,ans=mx;
        while(l<=r)
        {
            bigInt mid=(l+r)/2;
            if(check(mid))    l=mid+1,ans=mid;
            else    r=mid-1;
        }
        cout<<ans-mx<<"\n";
    }
    return 0;
}

「ABC 192E」Train

Link.

我也不知道我怎么过的,反正就是 Dijkstra 板子套上去后把 if(dis[y]>dis[x]+z) 改成了 if(dis[y]>get(dis[x],k)+z),其中 get(dis[x],k) 就是算下一班车来的时间加上 dis[x] 本身。

然后就莫名其妙过了,可能算个贪心?

#include<queue>
#include<cstdio>
using namespace std;
const long long INF=1e18;
priority_queue<pair<long long,long long>,vector<pair<long long,long long> >,greater<pair<long long,long long> > > q;
vector<pair<long long,pair<long long,long long> > > e[100010];
long long n,m,st,ed,dis[100010],vis[100010];
long long get(long long val,long long k)
{
    if(val%k==0)    return val;
    else    return val+k-(val%k);
}
void find()
{
    for(long long i=1;i<=n;++i)    dis[i]=INF;
    dis[st]=0;
    q.push(make_pair(dis[st],st));
    while(!q.empty())
    {
        long long now=q.top().second;
        q.pop();
        if(!vis[now])
        {
            vis[now]=1;
            for(long long i=0;i<e[now].size();++i)
            {
                long long y=e[now][i].first,w=e[now][i].second.first,k=e[now][i].second.second;
                if(dis[y]>get(dis[now],k)+w)
                {
                    dis[y]=get(dis[now],k)+w;
                    q.push(make_pair(dis[y],y));
                }
            }
        }
    }
}
int main()
{
    scanf("%lld %lld %lld %lld",&n,&m,&st,&ed);
    for(long long i=1;i<=m;++i)
    {
        long long u,v,w,k;
        scanf("%lld %lld %lld %lld",&u,&v,&w,&k);
        e[u].push_back(make_pair(v,make_pair(w,k)));
        e[v].push_back(make_pair(u,make_pair(w,k)));
    }
    find();
    printf("%lld\n",dis[ed]==INF?-1:dis[ed]);
    return 0;
}

「ABC 192F」Potion

Link.

考虑枚举 $k$,设 $f_{i,j,c}$ 为前 $i$ 位选了 $j$ 个数 balabala。

我也不知道怎么 DP 的,可能是本能做出来的。

后面自己意会吧,反正也没难度了。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,x,a[110],f[110][110][110],ans=1145141919810233454;
int main()
{
    scanf("%lld %lld",&n,&x);
    for(int i=1;i<=n;++i)    scanf("%lld",&a[i]);
    for(int s=1;s<=n;++s)
    {
        memset(f,-0x3f,sizeof(f));
        f[0][0][0]=0;
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=min(s,i);++j)
            {
                for(int k=0;k<n;++k)    f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][((k-a[i])%s+s)%s]+a[i]);
            }
        }
        if(f[n][s][x%s]>=0)    ans=min(ans,(x-f[n][s][x%s])/s);
    }
    printf("%lld\n",ans);
    return 0;
}

「CF 1486A」Shifting Stacks

Link.

考虑最少需要操作多少次后判断。

#include<map>
#include<cstdio>
using namespace std;
int t,n,flag;
long long sum,cum,x;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        flag=1;
        for(int i=1;i<=n;++i)
        {
            scanf("%lld",&x);
            sum+=x;
            if(sum-cum<0)    flag=0;
            cum+=i;
        }
        printf("%s\n",flag?"YES":"NO");
        sum=cum=0;
    }
    return 0;
}

「CF 1486B」Eastern Exhibition

Link.

可以发现行列独立,所以用个结论就行了。

#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
int t,n;
long long one[1010],ano[1010];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)    scanf("%lld %lld",&one[i],&ano[i]);
        sort(one+1,one+n+1);
        sort(ano+1,ano+n+1);
        printf("%lld\n",(one[(n+2)/2]-one[(n+1)/2]+1)*(ano[(n+2)/2]-ano[(n+1)/2]+1));
    }
    return 0;
}

「CF 1486C1」Guessing the Greatest (easy version)

Link.

看到 $20$ 的限制,想到 Robot Arms,猜想是二分。

然后就完了。

#include<cstdio>
int engoric(int l,int r)
{
    int res;
    printf("? %d %d\n",l,r);
    fflush(stdout);
    scanf("%d",&res);
    return res;
}
int n;
int main()
{
    scanf("%d",&n);
    int mxpos=engoric(1,n);
    int l=1,r=n;
    if(mxpos==1)    l=1;
    else
    {
        if(engoric(1,mxpos)==mxpos)    r=mxpos;
        else    l=mxpos;
    }
    if(l==mxpos)
    {
        while(l+1<r)
        {
            int mid=(l+r)>>1;
            if(engoric(mxpos,mid)==mxpos)    r=mid;
            else    l=mid;
        }
        printf("! %d\n",r);
    }
    else
    {
        while(l+1<r)
        {
            int mid=(l+r)>>1;
            if(engoric(mid,mxpos)==mxpos)    l=mid;
            else    r=mid;
        }
        printf("! %d\n",l);
    }
    return 0;
}

「CF 1486C2」Guessing the Greatest (hard version)

Link.

同 C1。

「CF 1486D」Max Median

Link.

「CF 1486E」Paired Payment

Link.

「CF 1486F」Pairs of Paths

Link.