Acdream 1420 High Speed Trains(大数 + 容斥原理)

传送门

High Speed Trains

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Submit Statistic Next Problem

Problem Description

  The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by high speed trains Shinkansens going all around the country. Therefore he decided to build the system of high speed trains in Flatland.
  Each high speed train line will be bidirectional and connect exactly two different cities of Flatland. Although there is actually no need of high speed trains in Flatland, the king ordered that there must be at least one high speed train line from each city of Flatland.
  The minister of transportation told the king that there are several train system satisfying his requirements. The king was amazed by the fact and asked the minister to count the number of possible systems.
  Help the minister to calculate the number of train systems.

Input

  The input file contains one integer number n (2 ≤ n ≤ 100)

Output

  Output one integer number — the number of different train systems that can be arranged in Flatland.

Sample Input

4

Sample Output

41

Source

Andrew Stankevich Contest 22

题目大意:

就是你 n 座城市,然后保证每个城市都有一个高铁通往另一个城市(姑且称他为高铁),但是两个城市之间只能有一条铁路,然后让你求的是这样的方法数。

解题思路:

其实我们可以借助容斥原理的思路来做这道题,我们就可以求出总数来,因为一共有 n 座城市,那么最多有sum = n*(n-1)/2条边,然后总数就是2^sum,然后我们可以考虑抛弃一个点,抛弃两个点…一直到抛弃n个点。那么设 ans[n] 为有 n 座城市的方法数,那么我们就可以列出公式:

ans[n]=2sum?ans[n?1]?C(n,1)?ans[n?2]?C(n,2)?...?ans[1]?C(n,n?1)?ans[0]?C(n,n)

注意ans[1]=0,因为我们修一条高铁的时候连接的是两条边,那么不可能出现一个城市的时候。

列出公式来之后就是运算了,这里还有大数运算(还是直接套的模板)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

#define DIGIT   4      //四位隔开,即万进制
#define DEPTH   10000        //万进制
#define MAXX    1000+5    //题目最大位数/4,要不大直接设为最大位数也行
typedef int bignum_t[MAXX+1];

/************************************************************************/
/* 读取操作数,对操作数进行处理存储在数组里                             */
/************************************************************************/
int read(bignum_t a,istream&is=cin)
{
    char buf[MAXX*DIGIT+1],ch ;
    int i,j ;
    memset((void*)a,0,sizeof(bignum_t));
    if(!(is>>buf))return 0 ;
    for(a[0]=strlen(buf),i=a[0]/2-1; i>=0; i--)
        ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ;
    for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf); j<a[0]*DIGIT; buf[j++]=‘0‘);
    for(i=1; i<=a[0]; i++)
        for(a[i]=0,j=0; j<DIGIT; j++)
            a[i]=a[i]*10+buf[i*DIGIT-1-j]-‘0‘ ;
    for(; !a[a[0]]&&a[0]>1; a[0]--);
    return 1 ;
}

void write(const bignum_t a,ostream&os=cout)
{
    int i,j ;
    for(os<<a[i=a[0]],i--; i; i--)
        for(j=DEPTH/10; j; j/=10)
            os<<a[i]/j%10 ;
}

int comp(const bignum_t a,const bignum_t b)
{
    int i ;
    if(a[0]!=b[0])
        return a[0]-b[0];
    for(i=a[0]; i; i--)
        if(a[i]!=b[i])
            return a[i]-b[i];
    return 0 ;
}

int comp(const bignum_t a,const int b)
{
    int c[12]=
    {
        1
    }
    ;
    for(c[1]=b; c[c[0]]>=DEPTH; c[c[0]+1]=c[c[0]]/DEPTH,c[c[0]]%=DEPTH,c[0]++);
    return comp(a,c);
}

int comp(const bignum_t a,const int c,const int d,const bignum_t b)
{
    int i,t=0,O=-DEPTH*2 ;
    if(b[0]-a[0]<d&&c)
        return 1 ;
    for(i=b[0]; i>d; i--)
    {
        t=t*DEPTH+a[i-d]*c-b[i];
        if(t>0)return 1 ;
        if(t<O)return 0 ;
    }
    for(i=d; i; i--)
    {
        t=t*DEPTH-b[i];
        if(t>0)return 1 ;
        if(t<O)return 0 ;
    }
    return t>0 ;
}
/************************************************************************/
/* 大数与大数相加                                                       */
/************************************************************************/
void add(bignum_t a,const bignum_t b)
{
    int i ;
    for(i=1; i<=b[0]; i++)
        if((a[i]+=b[i])>=DEPTH)
            a[i]-=DEPTH,a[i+1]++;
    if(b[0]>=a[0])
        a[0]=b[0];
    else
        for(; a[i]>=DEPTH&&i<a[0]; a[i]-=DEPTH,i++,a[i]++);
    a[0]+=(a[a[0]+1]>0);
}
/************************************************************************/
/* 大数与小数相加                                                       */
/************************************************************************/
void add(bignum_t a,const int b)
{
    int i=1 ;
    for(a[1]+=b; a[i]>=DEPTH&&i<a[0]; a[i+1]+=a[i]/DEPTH,a[i]%=DEPTH,i++);
    for(; a[a[0]]>=DEPTH; a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
}
/************************************************************************/
/* 大数相减(被减数>=减数)                                               */
/************************************************************************/
void sub(bignum_t a,const bignum_t b)
{
    int i ;
    for(i=1; i<=b[0]; i++)
        if((a[i]-=b[i])<0)
            a[i+1]--,a[i]+=DEPTH ;
    for(; a[i]<0; a[i]+=DEPTH,i++,a[i]--);
    for(; !a[a[0]]&&a[0]>1; a[0]--);
}
/************************************************************************/
/* 大数减去小数(被减数>=减数)                                           */
/************************************************************************/
void sub(bignum_t a,const int b)
{
    int i=1 ;
    for(a[1]-=b; a[i]<0; a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
    for(; !a[a[0]]&&a[0]>1; a[0]--);
}

void sub(bignum_t a,const bignum_t b,const int c,const int d)
{
    int i,O=b[0]+d ;
    for(i=1+d; i<=O; i++)
        if((a[i]-=b[i-d]*c)<0)
            a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH ;
    for(; a[i]<0; a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
    for(; !a[a[0]]&&a[0]>1; a[0]--);
}
/************************************************************************/
/* 大数相乘,读入被乘数a,乘数b,结果保存在c[]                          */
/************************************************************************/
void mul(bignum_t c,const bignum_t a,const bignum_t b)
{
    int i,j ;
    memset((void*)c,0,sizeof(bignum_t));
    for(c[0]=a[0]+b[0]-1,i=1; i<=a[0]; i++)
        for(j=1; j<=b[0]; j++)
            if((c[i+j-1]+=a[i]*b[j])>=DEPTH)
                c[i+j]+=c[i+j-1]/DEPTH,c[i+j-1]%=DEPTH ;
    for(c[0]+=(c[c[0]+1]>0); !c[c[0]]&&c[0]>1; c[0]--);
}
/************************************************************************/
/* 大数乘以小数,读入被乘数a,乘数b,结果保存在被乘数                   */
/************************************************************************/
void mul(bignum_t a,const int b)
{
    int i ;
    for(a[1]*=b,i=2; i<=a[0]; i++)
    {
        a[i]*=b ;
        if(a[i-1]>=DEPTH)
            a[i]+=a[i-1]/DEPTH,a[i-1]%=DEPTH ;
    }
    for(; a[a[0]]>=DEPTH; a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
    for(; !a[a[0]]&&a[0]>1; a[0]--);
}

void mul(bignum_t b,const bignum_t a,const int c,const int d)
{
    int i ;
    memset((void*)b,0,sizeof(bignum_t));
    for(b[0]=a[0]+d,i=d+1; i<=b[0]; i++)
        if((b[i]+=a[i-d]*c)>=DEPTH)
            b[i+1]+=b[i]/DEPTH,b[i]%=DEPTH ;
    for(; b[b[0]+1]; b[0]++,b[b[0]+1]=b[b[0]]/DEPTH,b[b[0]]%=DEPTH);
    for(; !b[b[0]]&&b[0]>1; b[0]--);
}
/**************************************************************************/
/* 大数相除,读入被除数a,除数b,结果保存在c[]数组                         */
/* 需要comp()函数                                                         */
/**************************************************************************/
void div(bignum_t c,bignum_t a,const bignum_t b)
{
    int h,l,m,i ;
    memset((void*)c,0,sizeof(bignum_t));
    c[0]=(b[0]<a[0]+1)?(a[0]-b[0]+2):1 ;
    for(i=c[0]; i; sub(a,b,c[i]=m,i-1),i--)
        for(h=DEPTH-1,l=0,m=(h+l+1)>>1; h>l; m=(h+l+1)>>1)
            if(comp(b,m,i-1,a))h=m-1 ;
            else l=m ;
    for(; !c[c[0]]&&c[0]>1; c[0]--);
    c[0]=c[0]>1?c[0]:1 ;
}

void div(bignum_t a,const int b,int&c)
{
    int i ;
    for(c=0,i=a[0]; i; c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--);
    for(; !a[a[0]]&&a[0]>1; a[0]--);
}
/************************************************************************/
/* 大数平方根,读入大数a,结果保存在b[]数组里                           */
/* 需要comp()函数                                                       */
/************************************************************************/
void sqrt(bignum_t b,bignum_t a)
{
    int h,l,m,i ;
    memset((void*)b,0,sizeof(bignum_t));
    for(i=b[0]=(a[0]+1)>>1; i; sub(a,b,m,i-1),b[i]+=m,i--)
        for(h=DEPTH-1,l=0,b[i]=m=(h+l+1)>>1; h>l; b[i]=m=(h+l+1)>>1)
            if(comp(b,m,i-1,a))h=m-1 ;
            else l=m ;
    for(; !b[b[0]]&&b[0]>1; b[0]--);
    for(i=1; i<=b[0]; b[i++]>>=1);
}
/************************************************************************/
/* 返回大数的长度                                                       */
/************************************************************************/
int length(const bignum_t a)
{
    int t,ret ;
    for(ret=(a[0]-1)*DIGIT,t=a[a[0]]; t; t/=10,ret++);
    return ret>0?ret:1 ;
}
/************************************************************************/
/* 返回指定位置的数字,从低位开始数到第b位,返回b位上的数               */
/************************************************************************/
int digit(const bignum_t a,const int b)
{
    int i,ret ;
    for(ret=a[(b-1)/DIGIT+1],i=(b-1)%DIGIT; i; ret/=10,i--);
    return ret%10 ;
}
/************************************************************************/
/* 返回大数末尾0的个数                                                  */
/************************************************************************/
int zeronum(const bignum_t a)
{
    int ret,t ;
    for(ret=0; !a[ret+1]; ret++);
    for(t=a[ret+1],ret*=DIGIT; !(t%10); t/=10,ret++);
    return ret ;
}

void comp(int*a,const int l,const int h,const int d)
{
    int i,j,t ;
    for(i=l; i<=h; i++)
        for(t=i,j=2; t>1; j++)
            while(!(t%j))
                a[j]+=d,t/=j ;
}

void convert(int*a,const int h,bignum_t b)
{
    int i,j,t=1 ;
    memset(b,0,sizeof(bignum_t));
    for(b[0]=b[1]=1,i=2; i<=h; i++)
        if(a[i])
            for(j=a[i]; j; t*=i,j--)
                if(t*i>DEPTH)
                    mul(b,t),t=1 ;
    mul(b,t);
}
/************************************************************************/
/* 组合数                                                               */
/************************************************************************/
void combination(bignum_t a,int m,int n)
{
    int*t=new int[m+1];
    memset((void*)t,0,sizeof(int)*(m+1));
    comp(t,n+1,m,1);
    comp(t,2,m-n,-1);
    convert(t,m,a);
    delete[]t ;
}
/************************************************************************/
/* 排列数                                                               */
/************************************************************************/
void permutation(bignum_t a,int m,int n)
{
    int i,t=1 ;
    memset(a,0,sizeof(bignum_t));
    a[0]=a[1]=1 ;
    for(i=m-n+1; i<=m; t*=i++)
        if(t*i>DEPTH)
            mul(a,t),t=1 ;
    mul(a,t);
}

#define SGN(x) ((x)>0?1:((x)<0?-1:0))
#define ABS(x) ((x)>0?(x):-(x))

int read(bignum_t a,int&sgn,istream&is=cin)
{
    char str[MAXX*DIGIT+2],ch,*buf ;
    int i,j ;
    memset((void*)a,0,sizeof(bignum_t));
    if(!(is>>str))return 0 ;
    buf=str,sgn=1 ;
    if(*buf==‘-‘)sgn=-1,buf++;
    for(a[0]=strlen(buf),i=a[0]/2-1; i>=0; i--)
        ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ;
    for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf); j<a[0]*DIGIT; buf[j++]=‘0‘);
    for(i=1; i<=a[0]; i++)
        for(a[i]=0,j=0; j<DIGIT; j++)
            a[i]=a[i]*10+buf[i*DIGIT-1-j]-‘0‘ ;
    for(; !a[a[0]]&&a[0]>1; a[0]--);
    if(a[0]==1&&!a[1])sgn=0 ;
    return 1 ;
}
struct bignum
{
    bignum_t num ;
    int sgn ;
public :
    inline bignum()
    {
        memset(num,0,sizeof(bignum_t));
        num[0]=1 ;
        sgn=0 ;
    }
    inline int operator!()
    {
        return num[0]==1&&!num[1];
    }
    inline bignum&operator=(const bignum&a)
    {
        memcpy(num,a.num,sizeof(bignum_t));
        sgn=a.sgn ;
        return*this ;
    }
    inline bignum&operator=(const int a)
    {
        memset(num,0,sizeof(bignum_t));
        num[0]=1 ;
        sgn=SGN (a);
        add(num,sgn*a);
        return*this ;
    }
    ;
    inline bignum&operator+=(const bignum&a)
    {
        if(sgn==a.sgn)add(num,a.num);
        else if
        (sgn&&a.sgn)
        {
            int ret=comp(num,a.num);
            if(ret>0)sub(num,a.num);
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memcpy(num,a.num,sizeof(bignum_t));
                sub (num,t);
                sgn=a.sgn ;
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if(!sgn)
            memcpy(num,a.num,sizeof(bignum_t)),sgn=a.sgn ;
        return*this ;
    }
    inline bignum&operator+=(const int a)
    {
        if(sgn*a>0)add(num,ABS(a));
        else if(sgn&&a)
        {
            int  ret=comp(num,ABS(a));
            if(ret>0)sub(num,ABS(a));
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memset(num,0,sizeof(bignum_t));
                num[0]=1 ;
                add(num,ABS (a));
                sgn=-sgn ;
                sub(num,t);
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if
        (!sgn)sgn=SGN(a),add(num,ABS(a));
        return*this ;
    }
    inline bignum operator+(const bignum&a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof (bignum_t));
        ret.sgn=sgn ;
        ret+=a ;
        return ret ;
    }
    inline bignum operator+(const int a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof (bignum_t));
        ret.sgn=sgn ;
        ret+=a ;
        return ret ;
    }
    inline bignum&operator-=(const bignum&a)
    {
        if(sgn*a.sgn<0)add(num,a.num);
        else if
        (sgn&&a.sgn)
        {
            int ret=comp(num,a.num);
            if(ret>0)sub(num,a.num);
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memcpy(num,a.num,sizeof(bignum_t));
                sub(num,t);
                sgn=-sgn ;
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if(!sgn)add (num,a.num),sgn=-a.sgn ;
        return*this ;
    }
    inline bignum&operator-=(const int a)
    {
        if(sgn*a<0)add(num,ABS(a));
        else if(sgn&&a)
        {
            int  ret=comp(num,ABS(a));
            if(ret>0)sub(num,ABS(a));
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memset(num,0,sizeof(bignum_t));
                num[0]=1 ;
                add(num,ABS(a));
                sub(num,t);
                sgn=-sgn ;
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if
        (!sgn)sgn=-SGN(a),add(num,ABS(a));
        return*this ;
    }
    inline bignum operator-(const bignum&a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof(bignum_t));
        ret.sgn=sgn ;
        ret-=a ;
        return ret ;
    }
    inline bignum operator-(const int a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof(bignum_t));
        ret.sgn=sgn ;
        ret-=a ;
        return ret ;
    }
    inline bignum&operator*=(const bignum&a)
    {
        bignum_t t ;
        mul(t,num,a.num);
        memcpy(num,t,sizeof(bignum_t));
        sgn*=a.sgn ;
        return*this ;
    }
    inline bignum&operator*=(const int a)
    {
        mul(num,ABS(a));
        sgn*=SGN(a);
        return*this ;
    }
    inline bignum operator*(const bignum&a)
    {
        bignum ret ;
        mul(ret.num,num,a.num);
        ret.sgn=sgn*a.sgn ;
        return ret ;
    }
    inline bignum operator*(const int a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof (bignum_t));
        mul(ret.num,ABS(a));
        ret.sgn=sgn*SGN(a);
        return ret ;
    }
    inline bignum&operator/=(const bignum&a)
    {
        bignum_t t ;
        div(t,num,a.num);
        memcpy (num,t,sizeof(bignum_t));
        sgn=(num[0]==1&&!num[1])?0:sgn*a.sgn ;
        return*this ;
    }
    inline bignum&operator/=(const int a)
    {
        int t ;
        div(num,ABS(a),t);
        sgn=(num[0]==1&&!num [1])?0:sgn*SGN(a);
        return*this ;
    }
    inline bignum operator/(const bignum&a)
    {
        bignum ret ;
        bignum_t t ;
        memcpy(t,num,sizeof(bignum_t));
        div(ret.num,t,a.num);
        ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*a.sgn ;
        return ret ;
    }
    inline bignum operator/(const int a)
    {
        bignum ret ;
        int t ;
        memcpy(ret.num,num,sizeof(bignum_t));
        div(ret.num,ABS(a),t);
        ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*SGN(a);
        return ret ;
    }
    inline bignum&operator%=(const bignum&a)
    {
        bignum_t t ;
        div(t,num,a.num);
        if(num[0]==1&&!num[1])sgn=0 ;
        return*this ;
    }
    inline int operator%=(const int a)
    {
        int t ;
        div(num,ABS(a),t);
        memset(num,0,sizeof (bignum_t));
        num[0]=1 ;
        add(num,t);
        return t ;
    }
    inline bignum operator%(const bignum&a)
    {
        bignum ret ;
        bignum_t t ;
        memcpy(ret.num,num,sizeof(bignum_t));
        div(t,ret.num,a.num);
        ret.sgn=(ret.num[0]==1&&!ret.num [1])?0:sgn ;
        return ret ;
    }
    inline int operator%(const int a)
    {
        bignum ret ;
        int t ;
        memcpy(ret.num,num,sizeof(bignum_t));
        div(ret.num,ABS(a),t);
        memset(ret.num,0,sizeof(bignum_t));
        ret.num[0]=1 ;
        add(ret.num,t);
        return t ;
    }
    inline bignum&operator++()
    {
        *this+=1 ;
        return*this ;
    }
    inline bignum&operator--()
    {
        *this-=1 ;
        return*this ;
    }
    ;
    inline int operator>(const bignum&a)
    {
        return sgn>0?(a.sgn>0?comp(num,a.num)>0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<0:0):a.sgn<0);
    }
    inline int operator>(const int a)
    {
        return sgn>0?(a>0?comp(num,a)>0:1):(sgn<0?(a<0?comp(num,-a)<0:0):a<0);
    }
    inline int operator>=(const bignum&a)
    {
        return sgn>0?(a.sgn>0?comp(num,a.num)>=0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<=0:0):a.sgn<=0);
    }
    inline int operator>=(const int a)
    {
        return sgn>0?(a>0?comp(num,a)>=0:1):(sgn<0?(a<0?comp(num,-a)<=0:0):a<=0);
    }
    inline int operator<(const bignum&a)
    {
        return sgn<0?(a.sgn<0?comp(num,a.num)>0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<0:0):a.sgn>0);
    }
    inline int operator<(const int a)
    {
        return sgn<0?(a<0?comp(num,-a)>0:1):(sgn>0?(a>0?comp(num,a)<0:0):a>0);
    }
    inline int operator<=(const bignum&a)
    {
        return sgn<0?(a.sgn<0?comp(num,a.num)>=0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<=0:0):a.sgn>=0);
    }
    inline int operator<=(const int a)
    {
        return sgn<0?(a<0?comp(num,-a)>=0:1):
               (sgn>0?(a>0?comp(num,a)<=0:0):a>=0);
    }
    inline int operator==(const bignum&a)
    {
        return(sgn==a.sgn)?!comp(num,a.num):0 ;
    }
    inline int operator==(const int a)
    {
        return(sgn*a>=0)?!comp(num,ABS(a)):0 ;
    }
    inline int operator!=(const bignum&a)
    {
        return(sgn==a.sgn)?comp(num,a.num):1 ;
    }
    inline int operator!=(const int a)
    {
        return(sgn*a>=0)?comp(num,ABS(a)):1 ;
    }
    inline int operator[](const int a)
    {
        return digit(num,a);
    }
    friend inline istream&operator>>(istream&is,bignum&a)
    {
        read(a.num,a.sgn,is);
        return  is ;
    }
    friend inline ostream&operator<<(ostream&os,const bignum&a)
    {
        if(a.sgn<0)
            os<<‘-‘ ;
        write(a.num,os);
        return os ;
    }
    friend inline bignum sqrt(const bignum&a)
    {
        bignum ret ;
        bignum_t t ;
        memcpy(t,a.num,sizeof(bignum_t));
        sqrt(ret.num,t);
        ret.sgn=ret.num[0]!=1||ret.num[1];
        return ret ;
    }
    friend inline bignum sqrt(const bignum&a,bignum&b)
    {
        bignum ret ;
        memcpy(b.num,a.num,sizeof(bignum_t));
        sqrt(ret.num,b.num);
        ret.sgn=ret.num[0]!=1||ret.num[1];
        b.sgn=b.num[0]!=1||ret.num[1];
        return ret ;
    }
    inline int length()
    {
        return :: length(num);
    }
    inline int zeronum()
    {
        return :: zeronum(num);
    }
    inline bignum C(const int m,const int n)
    {
        combination(num,m,n);
        sgn=1 ;
        return*this ;
    }
    inline bignum P(const int m,const int n)
    {
        permutation(num,m,n);
        sgn=1 ;
        return*this ;
    }
};
/**+++++++++++++++++++分界线++++++++++++++++++++++**/
bignum ans[105];
bignum c[105][105];
void Init()
{
    for(int i=1; i<105; i++)
    {
        c[0][i] = 1;
        c[i][0] = 1;
    }
    for(int i=1; i<105; i++)
    {
        for(int j=1; j<=i; j++)
        {
            c[i][j] = c[i-1][j-1]+c[i-1][j];
        }
    }
}
void Init2()
{
    ans[0] = 1, ans[1] = 0, ans[2] = 1;
    bignum ret;
    ret = 1;
    int last = 0;
    for(int i=3; i<102; i++)
    {
        int x = i*(i-1)/2;
        bignum tmp;
        tmp = 0;
        for(int j=1; j<=i; j++)
            tmp += c[i][j]*ans[i-j];
        for(int j = last; j<x; j++)
            ret = ret*2;
        last = x;
        ans[i] = ret - tmp;
    }
}
int main()
{
    Init();
    Init2();
    short n;
    while(cin>>n)
    {
        cout<<ans[n]<<endl;
    }
    return 0;
}
时间: 2025-01-16 18:11:03

Acdream 1420 High Speed Trains(大数 + 容斥原理)的相关文章

ACdream 1420 High Speed Trains【Java大数高精度 + 递推】

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1420 Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was a

ACdream 1420 High Speed Trains(容斥原理)

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by h

Gym 100338H High Speed Trains(高精度)

Gym 100338H High Speed Trains 题意: 求n个城市相互连通的方案数. 思路: 和HDU 4390迷之相似. 一共有n个城市,那么就有n(n?1)/2条边,每条边均有两种可能,选或不选.那么我们用ans[n]来表示n个城市相互连通的方案数: ans[n]=2n(n?1)/2?C1n?ans[n?1]?C2n?ans[n?2]?...?Cn?2n?ans[2]?C0n?ans[0] 答案 = 所有 - 一个城市独立的情况 - 两个城市独立的情况 - - - n-2个城市独

codeforces Gym 100338H High Speed Trains

递推就好了,用二项式定理算出所有连边的方案数,减去不合法的方案, 每次选出一个孤立点,那么对应方案数就是上次的答案. 枚举选几个孤立点和选哪些,选到n-1个点的时候相当于都不选,只减1. 要用到高精度,直接开100*100的组合数数组会MLE,用滚动数组优化一下就好了. 不会java,python太伤了 #include<bits/stdc++.h> using namespace std; const int MAXN = 20000; struct bign { int len, s[MA

递推 ACdream1420 High Speed Trains

传送门:点击打开链接 题意:n个点,每个点至少被一条边覆盖,不存在自环和重边.问有多少种情况 一个非常有意思的递推,其实可以利用容斥的思想去考虑这道题目 设F[n]表示为n个点的情况数,那么最多会存在n*(n-1)/2条边,这些边中,每条边都是可有可无.如果不考虑不满足条件的情况(也就是有的点没有被边覆盖的情况),可能的情况数为pow(2,n*(n-1)/2),也就是考虑每一条边是否存在 那么对于答案而言,不能有点没有被覆盖,接下来我们就来枚举点被边覆盖的个数,设为k 当有k个点被覆盖了,这k个

ASC(22)H(大数+推公式)

High Speed Trains Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by high

A Spy in the Metro

Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro con

UVa 1025 A Spy in the Metro(动态规划)

传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro

UVA1025---A Spy in the Metro(DP)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. Afterseveral thrilling events we ?nd her in the ?rst station of Algorithms City Metro, exam