iscsi

fast robot

Dec 3rd, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cctype>
  12. #include <cmath>
  13. #include <cstring>
  14. using namespace std;
  15. typedef unsigned long long LL;
  16. #define FOR(k,a,b) for(int k(a); k < (b); ++k)
  17. #define FORD(k,a,b) for(int k(a-1); k >= (b); --k)
  18. #define REP(k,a) for(int k=0; k < (a); ++k)
  19. #define ABS(a) ((a)>0?(a):-(a))
  20. const int mod=1e9+7;
  21. const int mSize=5002;
  22.  
  23. char *ipos, *opos, InpFile[200000], OutFile[200000];
  24. inline int readInt()
  25. {
  26.     while(*ipos <= 32) ++ipos;
  27.     int x=0;
  28.     char c;
  29.     while( 1 )
  30.     {
  31.         c=*ipos++;
  32.         if (c<=32) return x;
  33.         x*=10;
  34.         x+=c-'0';
  35.     }
  36. }
  37.  
  38. inline void writeInt(int a)
  39. {
  40.     opos+=sprintf(opos,"%d\n",a);
  41. }
  42.  
  43. inline void InitIO() {
  44.     ipos = InpFile;
  45.     opos = OutFile;
  46.     fread_unlocked(InpFile,200000,1,stdin);
  47. }
  48. inline void FlushIO() {
  49.     fwrite_unlocked(OutFile,opos-OutFile,1,stdout);
  50. }
  51.  
  52. int inverse(int a)
  53. {
  54.     int n=mod-2;
  55.     LL tmp=a,res=1;
  56.     while(n)
  57.     {
  58.         if(n&1)
  59.             res*=tmp;
  60.         res%=mod;
  61.         n>>=1;
  62.         tmp*=tmp;
  63.         tmp%=mod;
  64.     }
  65.     return res;
  66. }
  67.  
  68. int main()
  69. {
  70.     int inv[mSize],fact[mSize];
  71.     fact[0]=1;
  72.     inv[0]=1;
  73.     FOR(i,1,mSize)
  74.     {
  75.         fact[i]=(fact[i-1]*(LL)i)%mod;
  76.         inv[i]=inverse(fact[i]);
  77.     }
  78.     InitIO();
  79.     int N,K,K1,K2;
  80.     LL res=0;
  81.     while(true)
  82.     {
  83.         /*scanf("%d %d",&N,&K);*/
  84.         N=readInt();
  85.         K=readInt();
  86.         if(N==0 && K==0)
  87.             break;
  88.         if(N==1 || K>2*N-3)
  89.         {
  90.             //because k positive
  91.             writeInt(0);
  92.             continue;
  93.         }
  94.         --K;
  95.         K1=K/2;
  96.         K2=(K+1)/2;
  97.         res=2LL*fact[N-2]*inv[K1];
  98.         res%=mod;
  99.         res*=inv[N-2-K1];
  100.         res%=mod;
  101.         res*=fact[N-2];
  102.         res%=mod;
  103.         res*=inv[K2];
  104.         res%=mod;
  105.         res*=inv[N-2-K2];
  106.         res%=mod;
  107.         writeInt(res);
  108.     }
  109.     FlushIO();
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment