2021牛客寒假算法基础集训营1 ABFI 题解


A

题意:问你含有us子序列的,长度不大于n的串有多少(都是小写字母)

思路:其实每个位置可填放的字符就三种, u,s,和其他24个字符。
由于要求含有us子序列的串比较麻烦,正难则反,我们可以先求出不含有us的串有多少,再用总数减去即可。
(用dp[i][0]表示这一位及其之前,都没有填放u的方案数)
(用dp[i][1]表示这一位填了u或者前i-1填过u的方案数)
对于每一位,因为我们不能产生us子序列,则有状态转移方程:

dp[i][0] = 25*dp[i-1][0]; //当前位及其之前无u, 转移方程为前i-1位没有填u的方案数乘上当前位可填的25种字符
dp[i][1] = 25*dp[i-1][1] + dp[i-1][0];  //若前i位有u,可能是前i-1有u,那我这一位不能是s;  也可能是当前第i位产生的u,那么答案要加上之前没有放u的方案数
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '
'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll dp[maxn][2];

int main()
{
    ll n = read();

    dp[1][0] = 25;  //初始化
    dp[1][1] = 1;

    rep(i,2,n)
    {
        dp[i][0] = (25%mod*dp[i-1][0]%mod)%mod; //当前位及其之前无u, 转移方程为前i-1位没有填u的方案数乘上当前位可填的25种字符
        dp[i][1] = ( (25%mod*dp[i-1][1]%mod)%mod + (dp[i-1][0])%mod)%mod ;  //若前i位有u,可能是前i-1有u,那我这一位不能是s;  也可能是当前第i位产生的u,那么答案要加上之前没有放u的方案数
    }
   ll ans = 0;
   ll cur = 1;
   rep(i,1,n)   //枚举i长度的串,用总数减去每次不含us的方案数即可,然后累加
   {
       cur = (cur%mod*26%mod)%mod;
       ll res = (cur%mod-dp[i][0]%mod-dp[i][1]%mod+mod+mod+mod)%mod;
       ans = (ans%mod + res%mod+mod)%mod;
   }
   cout<<ans<<endl;
    return 0;
}


B

题意:凑出k对括号,字符串长度不能超过1e5。

思路:贪心,先一对一对()的加,这个时候总数就是1+2+3+4+....的前m项和,还有少的括号就从后往前加。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '
'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    ll sum = 0;
    ll cur1 = 1;

    ll n = read();
    if(n==0)
    {
        cout<<"("<<endl;
        return 0;
    }
    ll m = sqrt(n*1.0);
    string s;
    ll cur = 0;
    ll step = 1;
    while(cur+step<=n)
    {
        cur += step;
        step++;
        s += "()";
    }
    ll left = n-cur;
    string t;
    int ok = 1;
    cur = step-1;
    for(int i=s.size()-1; i>=0; i--)
    {
        t += s[i];
        if(ok&&left>=cur)
       {
           int num = left/cur;
           left -= num*cur;
           rep(j,1,num) t += ")";
       }
       if(!ok) cur--;
       ok = !ok;
    }
    for(int i=t.size()-1; i>=0; i--) cout<<t[i]; cout<<endl;
    return 0;
}


F

水题

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '
'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

vector<string> a;
vector<string> b;

int main()
{
    ll n = read();
    rep(i,1,n)
    {
        string s;
        cin>>s;
        a.pb(s);
    }
    rep(i,1,n)
    {
        string s;
        cin>>s;
        b.pb(s);
    }
    ll ans = 0;
    for(int i=0; i<n; i++)
    {
        if(a[i]==b[i]) ans += 2;
        else ans ++;
    }
    cout<<ans<<' '<<0<<endl;
    return 0;
}


I

题意:构造出一个n的排列,使得两两不互质的对刚好是k对。

思路:可能我的方法麻烦了。
贪心先得到偶数序列,这能够使得对尽量的多,现在考虑再往两边加数。再得到n以内最大的(2*3^p),此时p最大,放在左边,往左依次除3。 同理找到n以内最大的(2*5^p),放右边,往右依次除5。剩下的按顺序排列就行。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '
'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int vis[maxn];
vector<int> item;
int use[maxn];
vector<int> head;
vector<int> tail;

int main()
{
    ll n = read(), k = read();
    if(n==1||n==2||n==3)
    {
        if(k==0)
        {
            rep(i,1,n) printf("%d%c",i,i==n?'
':' ');
        }
        else cout<<-1<<endl;
        return 0;
    }
    if(k==0)
    {
        rep(i,1,n) cout<<i<<' '; cout<<endl;
        return 0;
    }
    ll t = 2;
    int idx = 1;
    int num = 3;
    while(t*3<=n)
    {
        t = t*3;
        head.pb(num);
        vis[num] = 1;
        num *= 3;
    }
    if(t!=2)
    {
        item.pb(t);
        vis[t] = 1;
    }
    int t1 = 2;
    while(t1*5<=n)
    {
        t1 = t1*5;
    }

    if(t1!=2) vis[t1] = 1;

    for(int i=2; i<=n; i += 2)
    {
        if(i!=t&&i!=t1&&!vis[i]||(i==t&&t==2)||(i==t1&&t1==2)) item.pb(i), vis[i] = 1;
    }

    if(t1!=2)
    item.pb(t1);
     t1 = 2;
    while(t1*5<=n)
    {
        t1 = t1*5;
        tail.pb(t1/2);
        vis[t1/2] = 1;
    }
    vector<int> ans;
    if(item.size()+head.size()+tail.size()<k+1) cout<<-1<<endl;
    else
    {
        k++;

        if(k>item.size())
        for(int i=(int)0; i<head.size(); i++)
        {
            if(!k) break;
            k--;
            use[head[i]] = 1;
            ans.pb(head[i]);
        }

        for(int i=0;i<item.size(); i++)
        {
            if(!k) break;
            k--;
            use[item[i]] = 1;
            ans.pb(item[i]);
        }

        for(int i=(int)tail.size()-1; i>=0; i--)
        {
            if(!k) break;
            k--;
            use[tail[i]] = 1;
            ans.pb(tail[i]);
        }

        rep(i,1,n) if(!use[i])  ans.pb(i);
        for(int i=0; i<ans.size(); i++) printf("%d%c",ans[i],i+1==ans.size()?'
':' ');
    }
    return 0;
}


原文地址:https://www.cnblogs.com/Bgwithcode/p/14358099.html