bzoj4005[JLOI2015]骗我呢

http://www.lydsy.com/JudgeOnline/problem.php?id=4005

神题~远距离orz

膜拜PoPoQQQ大神

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const LL Mod=1000000007;

LL N,M;
LL ans;

inline LL power(LL a,LL k)
  {
      LL x=1;
        while(k){if(k&1)x=x*a%Mod;a=a*a%Mod;k>>=1;}
        return x;
    }

LL S[10000010];

inline LL C(LL N,LL M)
  {
      if(M<0 || M>N)return 0;
      LL x=S[N],y=S[N-M]*S[M]%Mod,res;
      res=x*power(y,Mod-2)%Mod;
      res=(res%Mod+Mod)%Mod;
      return res;
  }

//y=x+1
//y=x-M-2
inline void change(LL &x,LL &y,int f)
  {
      LL dx,dy;
      if(f==0)
        {
            dx=x;dy=y-1;
            x=dy;y=dx;
            y++;
            }
        else
          {
              dx=x;dy=y+M+2;
              x=dy;y=dx;
              y-=M+2;
          }
  }

int main()
  {
      freopen("bzoj4005.in","r",stdin);
      freopen("bzoj4005.out","w",stdout);
      LL i;
      S[0]=1;re(i,1,10000000)S[i]=S[i-1]*i%Mod;
      N=gll(),M=gll();
        ans=C(N+N+M+1,N);LL x,y;int f;
      x=N+M+1;y=N;f=0;
      while(1)
        {
            change(x,y,f);
            if(!(x>=0 && y>=0))break;
            if(f==0) ans=(ans-C(x+y,x))%Mod; else ans=(ans+C(x+y,x))%Mod;
            f^=1;
        }
      x=N+M+1;y=N;f=1;
      while(1)
        {
            change(x,y,f);
            if(!(x>=0 && y>=0))break;
            if(f==1) ans=(ans-C(x+y,x))%Mod; else ans=(ans+C(x+y,x))%Mod;
            f^=1;
        }
      ans=(ans%Mod+Mod)%Mod;
      cout<<ans<<endl;
      return 0;
  }
View Code

 

posted @ 2015-08-25 11:23  maijing  阅读(446)  评论(0编辑  收藏  举报