Bestcoder round#31 解题报告

1001

我们将x数组排序,利用upper_bound()找到大于x[i]+k的第一个值的下标,求出i到这个下标的开区间的长度,求和即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long LL;
int n,k;
int a[110000];
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d %d",&n,&k);
        for (int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        LL ans=0;
        for (LL i=1;i<=n;i++)
        {
            LL x=upper_bound(a+1,a+1+n,a[i]+k)-a-1;
            ans+=x-i;
        }
        cout<<ans<<endl;
    }
    return 0;
}

1002

利用dfs搜出所有答案,排序后再利用lower_bound()和upper_bound()找到答案的区间。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>

using namespace std;

int a[11000];
int num;
int nu[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
void dfs(int depth,int last,int now)
{
    if (depth>9) return;
    a[num++]=now;
    for (int i=9/last;i>=1;i--)
    {
        dfs(depth+1,i*last,now+i*last*nu[depth]);
    }
}
int main()
{
    dfs(0,1,0);
    sort(a+1,a+1+num);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int l,r;
        scanf("%d %d",&l,&r);
        int l1=lower_bound(a+1,a+1+num,l)-a,l2=upper_bound(a+1,a+1+num,r)-a-1;
        printf("%d\n",l2-l1+1);
    }
    return 0;
}

1003

dfs打表。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>

using namespace std;
int n,k,f[16][16];
int main()
{
    f[1][0]=2;
    f[1][1]=1;
    f[1][2]=0;
    f[1][3]=0;
    f[1][4]=0;
    f[1][5]=0;
    f[1][6]=0;
    f[1][7]=0;
    f[1][8]=0;
    f[1][9]=0;
    f[1][10]=0;
    f[1][11]=0;
    f[1][12]=0;
    f[1][13]=0;
    f[1][14]=0;
    f[1][15]=0;
    f[2][0]=5;
    f[2][1]=8;
    f[2][2]=2;
    f[2][3]=0;
    f[2][4]=0;
    f[2][5]=0;
    f[2][6]=0;
    f[2][7]=0;
    f[2][8]=0;
    f[2][9]=0;
    f[2][10]=0;
    f[2][11]=0;
    f[2][12]=0;
    f[2][13]=0;
    f[2][14]=0;
    f[2][15]=0;
    f[3][0]=35;
    f[3][1]=72;
    f[3][2]=36;
    f[3][3]=6;
    f[3][4]=0;
    f[3][5]=0;
    f[3][6]=0;
    f[3][7]=0;
    f[3][8]=0;
    f[3][9]=0;
    f[3][10]=0;
    f[3][11]=0;
    f[3][12]=0;
    f[3][13]=0;
    f[3][14]=0;
    f[3][15]=0;
    f[4][0]=314;
    f[4][1]=940;
    f[4][2]=702;
    f[4][3]=192;
    f[4][4]=24;
    f[4][5]=0;
    f[4][6]=0;
    f[4][7]=0;
    f[4][8]=0;
    f[4][9]=0;
    f[4][10]=0;
    f[4][11]=0;
    f[4][12]=0;
    f[4][13]=0;
    f[4][14]=0;
    f[4][15]=0;
    f[5][0]=6427;
    f[5][1]=20953;
    f[5][2]=18778;
    f[5][3]=6528;
    f[5][4]=1200;
    f[5][5]=120;
    f[5][6]=0;
    f[5][7]=0;
    f[5][8]=0;
    f[5][9]=0;
    f[5][10]=0;
    f[5][11]=0;
    f[5][12]=0;
    f[5][13]=0;
    f[5][14]=0;
    f[5][15]=0;
    f[6][0]=202841;
    f[6][1]=724224;
    f[6][2]=766278;
    f[6][3]=310368;
    f[6][4]=63000;
    f[6][5]=8640;
    f[6][6]=720;
    f[6][7]=0;
    f[6][8]=0;
    f[6][9]=0;
    f[6][10]=0;
    f[6][11]=0;
    f[6][12]=0;
    f[6][13]=0;
    f[6][14]=0;
    f[6][15]=0;
    f[7][0]=12727570;
    f[7][1]=45687569;
    f[7][2]=51049098;
    f[7][3]=22486656;
    f[7][4]=4751400;
    f[7][5]=648000;
    f[7][6]=70560;
    f[7][7]=5040;
    f[7][8]=0;
    f[7][9]=0;
    f[7][10]=0;
    f[7][11]=0;
    f[7][12]=0;
    f[7][13]=0;
    f[7][14]=0;
    f[7][15]=0;
    f[8][0]=355115594;
    f[8][1]=736342192;
    f[8][2]=480159099;
    f[8][3]=593158322;
    f[8][4]=564699600;
    f[8][5]=71527200;
    f[8][6]=7161840;
    f[8][7]=645120;
    f[8][8]=40320;
    f[8][9]=0;
    f[8][10]=0;
    f[8][11]=0;
    f[8][12]=0;
    f[8][13]=0;
    f[8][14]=0;
    f[8][15]=0;
    f[9][0]=718817248;
    f[9][1]=766062498;
    f[9][2]=551790315;
    f[9][3]=804772379;
    f[9][4]=958725252;
    f[9][5]=916206996;
    f[9][6]=91739593;
    f[9][7]=85155840;
    f[9][8]=6531840;
    f[9][9]=362880;
    f[9][10]=0;
    f[9][11]=0;
    f[9][12]=0;
    f[9][13]=0;
    f[9][14]=0;
    f[9][15]=0;
    f[10][0]=789281595;
    f[10][1]=29367309;
    f[10][2]=234810099;
    f[10][3]=689850602;
    f[10][4]=160354814;
    f[10][5]=575223606;
    f[10][6]=565338593;
    f[10][7]=166965641;
    f[10][8]=87551353;
    f[10][9]=72576000;
    f[10][10]=3628800;
    f[10][11]=0;
    f[10][12]=0;
    f[10][13]=0;
    f[10][14]=0;
    f[10][15]=0;
    f[11][0]=245046738;
    f[11][1]=212490784;
    f[11][2]=527909966;
    f[11][3]=404618207;
    f[11][4]=648188905;
    f[11][5]=539918379;
    f[11][6]=532542087;
    f[11][7]=165451585;
    f[11][8]=428944600;
    f[11][9]=878079902;
    f[11][10]=878169600;
    f[11][11]=39916800;
    f[11][12]=0;
    f[11][13]=0;
    f[11][14]=0;
    f[11][15]=0;
    f[12][0]=681555006;
    f[12][1]=278546674;
    f[12][2]=315035573;
    f[12][3]=624869750;
    f[12][4]=794507062;
    f[12][5]=84429000;
    f[12][6]=792648481;
    f[12][7]=661448548;
    f[12][8]=160683200;
    f[12][9]=93278547;
    f[12][10]=346974481;
    f[12][11]=496038323;
    f[12][12]=479001600;
    f[12][13]=0;
    f[12][14]=0;
    f[12][15]=0;
    f[13][0]=413451720;
    f[13][1]=259920010;
    f[13][2]=496775457;
    f[13][3]=225920769;
    f[13][4]=842092390;
    f[13][5]=332550368;
    f[13][6]=221710589;
    f[13][7]=232620660;
    f[13][8]=166171087;
    f[13][9]=462729811;
    f[13][10]=955852083;
    f[13][11]=835265947;
    f[13][12]=902539673;
    f[13][13]=227020758;
    f[13][14]=0;
    f[13][15]=0;
    f[14][0]=571703274;
    f[14][1]=654602013;
    f[14][2]=270251289;
    f[14][3]=900887757;
    f[14][4]=942422378;
    f[14][5]=974702258;
    f[14][6]=610771898;
    f[14][7]=46596778;
    f[14][8]=844659211;
    f[14][9]=467836761;
    f[14][10]=298284961;
    f[14][11]=960033762;
    f[14][12]=424915175;
    f[14][13]=992136520;
    f[14][14]=178290591;
    f[14][15]=0;
    f[15][0]=7017421;
    f[15][1]=361818480;
    f[15][2]=635021015;
    f[15][3]=633194321;
    f[15][4]=924790619;
    f[15][5]=16861446;
    f[15][6]=556906023;
    f[15][7]=606568231;
    f[15][8]=432184532;
    f[15][9]=717658304;
    f[15][10]=181068670;
    f[15][11]=990596447;
    f[15][12]=108561136;
    f[15][13]=862468715;
    f[15][14]=230765390;
    f[15][15]=674358851;
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d %d",&n,&k);
        printf("%d\n",f[n][k]);
    }
    return 0;
}

另,把之前dfs的代码改了改,竟然跑得飞快:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<bitset>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
int n,m,k;
int ans;
struct node
{
    int a[16];
    bool operator < (const node &b) const
    {
        for (int i=0;i<15;i++)
            if (a[i]!=b.a[i]) return a[i]<b.a[i];
        return 0;
    }
};
vector<node> q;
vector<int> zy[2000];
void dfs(int depth,int last,node a)
{
    if (depth==m+1)
    {
        ans++;
        q.push_back(a);
        return ;
    }
    if (last==1) a.a[depth-1]=0,dfs(depth+1,0,a);
    else
    {
        a.a[depth-1]=0,dfs(depth+1,0,a);
        a.a[depth-1]=1,dfs(depth+1,1,a);

    }
}
LL dp1[16][16];
node anss;
int l;
LL last1[2000],last2[2000];
LL num;
LL cal[21000];
void search(int depth)
{
    memset(last2,0,sizeof(last2));
    for (int i=0;i<l;i++)
    {
        int ll=zy[i].size();
        for (int j=0;j<ll;j++)
        {
            last2[zy[i][j]]=(last2[zy[i][j]]+last1[i])%mod;
        }
    }
    for (int i=0;i<l;i++)
        last1[i]=last2[i];
    LL res=0;
    for (int i=0;i<l;i++)
        res+=last1[i];
    dp1[m][depth]=res;
    if (depth==15) return;
    search(depth+1);
}
LL dp2[16][16];
map<node,LL> S;
vector<int> qqq[16];
node ha[110000];
int way[16];
void search2(int depth)
{
    if (depth==m+1)
    {
        node a;
        memset(a.a,0,sizeof(a.a));
        way[0]=1;
        int num=0;
        int flag=0;
        for (int i=m;i>=0;i--)
        {
            if (way[i]==0)
            {
                num++;
                flag=1;
            }
            else
            {
                a.a[num]++;
                num=0;
            }
        }   
        if (!flag) a.a[0]=m+1;
        else a.a[0]=0;
        S[a]=(S[a]+1)%mod;
        return;
    }
    for (int i=0;i<=1;i++)
    {
        way[depth]=i;
        search2(depth+1);
    }
}
LL f[16],inv[16];
LL C[20][20];
LL multi(LL a, LL b)
{
    LL ret=0;
    while(b)
    {
        if(b&1) ret=(ret+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return ret;
}
LL pow_mod(LL a,LL b)
{
    LL ret=1;
    while(b)
    {
        if(b&1) ret=multi(ret,a)%mod;
        a=multi(a,a)%mod;
        b>>=1;
    }
    return ret;
}
int main()
{
    C[0][0]=1;
    for(int i=1;i<=16;i++){
        C[i][0]=1;
        for (int j=1;j<=i;j++)
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    f[0]=1;
    for (LL i=1;i<=15;i++)
    {
        f[i]=(i*f[i-1])%mod;
    }
    f[0]=1;
    inv[1]=inv[0]=1;  
    for (int i=2;i<=15;i++)
    {
        inv[i]=(LL)(mod-mod/i)*inv[mod%i]%mod;
    }
    for (int i=2;i<=15;i++)
        inv[i]=(inv[i-1]*inv[i])%mod;
    for (int i=0;i<=15;i++)
        dp1[i][0]=dp1[0][i]=1;
    for (m=1;m<=15;m++)
    {
        q.clear();
        ans=0;
        memset(anss.a,0,sizeof(anss.a));
        dfs(1,0,anss);
        l=q.size();
        int dd=0;
        for (int i=0;i<l;i++)
            zy[i].clear();
        for (int i=0;i<l;i++)
        {
            for (int j=0;j<l;j++)
            {
                node a=q[i],b=q[j];
                int flag=0;
                for (int k=0;k<m;k++)
                {
                    if (b.a[k])
                    {
                        if ((k!=0&&a.a[k-1])||a.a[k]||a.a[k+1])
                        {
                            flag=1;
                            break;
                        }
                    }
                }
                if (!flag) zy[i].push_back(j);
            }
        }
        memset(last1,0,sizeof(last1));
        last1[0]=1;
        search(1);
    }
    for (int i=1;i<=15;i++)
        for (int j=i+1;j<=15;j++)
            dp1[i][j]=dp1[j][i]; 
    for (m=1;m<=15;m++)
    {
        S.clear();
        search2(1);
        for (int i=0;i<=m;i++)
            qqq[i].clear();
        map<node,LL>::iterator it;
        num=0;
        for (it=S.begin();it!=S.end();it++)
        {
            if (it->second!=0)
            {
                LL geshu=0; 
                node b=it->first;
                for (int i=0;i<=m;i++)
                {
                    geshu=(geshu+b.a[i]*i)%mod;
                }
                num++;
                geshu=m-geshu;
                ha[num]=b;
                qqq[geshu].push_back(num);
                cal[num]=it->second;
            }
        }
        dp2[m][m]=f[m];
        for (int k=0;k<m;k++)
        {

            l=qqq[k].size();
            for (int i=0;i<l;i++)
                for (int j=0;j<l;j++)
                {
                    node a=ha[qqq[k][i]],b=ha[qqq[k][j]];
                    LL res=1;
                    int x=0,y=0;
                    for (int ll=0;ll<=m;ll++)
                        x+=a.a[ll];
                    for (int ll=0;ll<=m;ll++)
                        y+=b.a[ll];
                    for (int l1=0;l1<=m;l1++)
                    {
                        if (a.a[l1]==0) continue;
                        for (int l2=0;l2<=m;l2++)
                        {
                            if (b.a[l2]==0) continue;
                            LL xx=(pow_mod(dp1[l1][l2],a.a[l1]*b.a[l2]))%mod;
                            res=(res*xx)%mod;
                        }
                    }
                    for (int l1=1;l1<=m;l1++)
                        res=(res*inv[a.a[l1]])%mod; 
                    for (int l2=1;l2<=m;l2++)
                        res=(res*inv[b.a[l2]])%mod;
                    res=(res*f[y])%mod;     
                    res=(res*f[x])%mod;
                    res=(res*f[k])%mod;
                    res=(res*C[k+1][x])%mod;
                    res=(res*C[k+1][y])%mod;
                    dp2[m][k]=(dp2[m][k]+res)%mod;
                }
            dp2[m][k]=(dp2[m][k])%mod;
        }
    }
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        printf("%I64d\n",dp2[x][y]);
    }
    return 0;
}   

1004

。。。不会。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值