Guest User

sequence

a guest
Nov 17th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<cstring>
  3. #include<math.h>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<string>
  7. #include<vector>
  8. #include<stdlib.h>
  9. #include<set>
  10. #include<map>
  11. #include<utility>
  12. #include<time.h>
  13. #include<queue>
  14. #include<limits.h>
  15. using namespace std;
  16. typedef vector<int>::iterator vit;
  17. typedef string::iterator sit;
  18. typedef vector<int>::reverse_iterator rvit;
  19. typedef long long ll;
  20. typedef long double ld;
  21. const int N=1100;
  22. const ll MOD=1000000000+7;
  23. ll mod(ll x)
  24. {
  25.     x%=MOD;
  26.     if(x<0)
  27.         x+=MOD;
  28.     return x;
  29. }
  30. struct TPos
  31. {
  32.     ll total,num;
  33.     TPos()
  34.     {
  35.         total=num=0;    
  36.     }  
  37.     void update_total(ll x)
  38.     {
  39.         total+=x;
  40.         total=mod(total);    
  41.     }
  42.     void update_num(ll x)
  43.     {
  44.         num+=x;
  45.         num=mod(num);    
  46.     }
  47. };
  48. TPos d[N][N];
  49. int n;
  50. ll c,ans=0;
  51. void push(int pos, int len)
  52. {
  53.     int lim=len;
  54.     if(pos+lim>n)
  55.         lim=n-pos;
  56.     ll total_score=d[pos][len].total,num_score=d[pos][len].num;
  57.     if(pos==0)
  58.     {
  59.         total_score*=c;
  60.         num_score*=c;
  61.     }
  62.     else
  63.     {
  64.         total_score*=(c-1);
  65.         num_score*=(c-1);
  66.     }
  67.     for(int i=1;i<=lim;++i)
  68.     {
  69.         d[pos+i][len].update_total(total_score);
  70.         d[pos+i][len].update_num(num_score);    
  71.         if(i==len)
  72.         {
  73.             ll z=pos==0?c:c-1;
  74.             d[pos+i][len].update_num((d[pos][i].total-d[pos][i].num)*z);    
  75.         }
  76.     }
  77. }
  78. void gen()
  79. {
  80.    
  81.     for(int j=1;j<=n;++j)
  82.     {
  83.         d[0][j].total=1;
  84.         for(int i=0;i<n;++i)
  85.         {
  86.             push(i,j);    
  87.         }
  88.     }    
  89.     ans=0;
  90.     for(int j=1;j<=n;++j)
  91.     {
  92.         ans+=j*d[n][j].num;
  93.         ans=mod(ans);    
  94.     }
  95. }
  96.  
  97. const bool D=0;
  98. int main()
  99. {
  100.     if(D)
  101.     {
  102.         freopen("test.in","r",stdin);
  103.         freopen("test.out","w",stdout);
  104.     }
  105.     else
  106.     {
  107.         freopen("sequence.in","r",stdin);
  108.         freopen("sequence.out","w",stdout);
  109.     }
  110.     scanf("%d%I64d",&n,&c);
  111.     gen();
  112.     printf("%I64d\n",ans);
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment