Kaidul

E #230 Div 2

Feb 18th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MEMSET_INF 127
  6. #define MEMSET_HALF_INF 63
  7. #define stream istringstream
  8. #define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
  9. #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++)
  10. #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
  11. #define INF (1<<30)
  12. #define PI acos(-1.0)
  13. #define pb push_back
  14. #define ppb pop_back
  15. #define all(x) x.begin(),x.end()
  16. #define mem(x,y) memset(x,y,sizeof(x))
  17. #define memsp(x) mem(x,MEMSET_INF)
  18. #define memdp(x) mem(x,-1)
  19. #define memca(x) mem(x,0)
  20. #define eps 1e-9
  21. #define pii pair<int,int>
  22. #define pmp make_pair
  23. #define ft first
  24. #define sd second
  25. #define vi vector<int>
  26. #define vpii vector<pii>
  27. #define si set<int>
  28. #define msi map<string , int >
  29. #define mis map<int , string >
  30. typedef long long i64;
  31. typedef unsigned long long ui64;
  32. /** function **/
  33. #define SDi(x) sf("%d", &x)
  34. #define SDl(x) sf("%lld", &x)
  35. #define SDs(x) sf("%s", x)
  36. #define SD2(x,y) sf("%d%d", &x, &y)
  37. #define SD3(x,y,z) sf("%d%d%d", &x, &y, &z)
  38. #define pf printf
  39. #define print(x) pf("%d ", x)
  40. #define println(x) pf("%d\n", x)
  41. #define sf scanf
  42. #define READ(f) freopen(f, "r", stdin)
  43. #define WRITE(f) freopen(f, "w", stdout)
  44.  
  45. const i64 INF64 = (i64)1E18;
  46.  
  47. template<class T> inline T gcd(T a,T b) {
  48.     if(a<0)return gcd(-a,b);
  49.     if(b<0)return gcd(a,-b);
  50.     return (b==0)?a:gcd(b,a%b);
  51. }
  52. template<class T> inline T lcm(T a,T b) {
  53.     if(a<0)return lcm(-a,b);
  54.     if(b<0)return lcm(a, -b);
  55.     return a*(b/gcd(a,b));
  56. }
  57. template<class T> inline T sqr(T x) {
  58.     return x*x;
  59. }
  60. template<class T> T power(T N,T P) {
  61.     return (P==0)?  1: N*power(N,P-1);
  62. }
  63. template<class T> bool inside(T a,T b,T c) {
  64.     return (b>=a && b<=c);
  65. }
  66. double _dist(double x1,double y1,double x2,double y2) {
  67.     return sqrt(sqr(x1-x2)+sqr(y1-
  68.                                y2));
  69. }
  70. int distsq(int x1,int y1,int x2,int y2) {
  71.     return sqr(x1-x2)+sqr(y1-y2);
  72. }
  73. i64 toInt64(string s) {
  74.     i64 r=0;
  75.     istringstream sin(s);
  76.     sin>>r;
  77.     return r;
  78. }
  79. double Log(i64 N, i64 B) {
  80.     return (log10l(N)) / (log10l(B));
  81. }
  82. string itoa(long long a) {
  83.     if(a==0) return "0";
  84.     string ret;
  85.     for(long long i=a; i>0; i=i/10)
  86.         ret.push_back((i%10)+48);
  87.     reverse(ret.begin(),ret.end());
  88.     return ret;
  89. }
  90. vector< string > token( string a, string b ) {
  91.     const char *q = a.c_str();
  92.     while( count( b.begin(), b.end(), *q ) ) q++;
  93.     vector< string > oot;
  94.     while( *q ) {
  95.         const char *e = q;
  96.         while( *e && !count( b.begin(), b.end(), *e ) )
  97.             e++;
  98.         oot.push_back( string( q, e ) );
  99.         q = e;
  100.         while( count( b.begin(), b.end(), *q ) )
  101.             q++;
  102.     }
  103.     return oot;
  104. }
  105.  
  106. int isvowel(char s) {
  107.     s=tolower(s);
  108.     if(s=='a' or s=='e' or s=='i' or s=='o' or s=='u')
  109.         return 1;
  110.     return 0;
  111. }
  112. int isupper(char s) {
  113.     if(s>='A' and s<='Z') return 1;
  114.     return 0;
  115. }
  116. template<class T> struct Fraction {
  117.     T a,b;
  118.     Fraction(T a=0,T b=1);
  119.     string
  120.     toString();
  121. };//NOTES:Fraction
  122. template<class T> Fraction<T>::Fraction(T a,T b) {
  123.     T d=gcd(a,b);
  124.     a/=d;
  125.     b/=d;
  126.     if (b<0) a=-a, b=-b;
  127.     this->a=a;
  128.     this->b=b;
  129. }
  130. template<class T> string Fraction<T>::toString() {
  131.     ostringstream
  132.     sout;
  133.     sout<<a<<"/"<<b;
  134.     return sout.str();
  135. }
  136. template<class T> Fraction<T> operator+(Fraction<T> p,Fraction<T> q) {
  137.     return
  138.         Fraction<T>(p.a*q.b+q.a*p.b,p.b*q.b);
  139. }
  140. template<class T> Fraction<T> operator-(Fraction<T> p,Fraction<T> q) {
  141.     return
  142.         Fraction<T>(p.a*q.b-q.a*p.b,p.b*q.b);
  143. }
  144. template<class T> Fraction<T> operator*(Fraction<T> p,Fraction<T> q) {
  145.     return
  146.         Fraction<T>(p.a*q.a,p.b*q.b);
  147. }
  148. template<class T> Fraction<T> operator/(Fraction<T> p,Fraction<T> q) {
  149.     return
  150.         Fraction<T>(p.a*q.b,p.b*q.a);
  151. }
  152.  
  153. /** BitMask **/
  154. int Set(int N, int pos) {
  155.     return N = N | (1 << pos);
  156. }
  157. int Reset(int N,int pos) {
  158.     return N = N & ~(1 << pos);
  159. }
  160. int Check(int N, int pos) {
  161.     return (N & (1 << pos));
  162. }
  163. int toggle(int N, int pos) {
  164.     if( Check(N, pos) )
  165.         return N = Reset(N, pos);
  166.     return N = Set(N, pos);
  167. }
  168.  
  169. const i64 INFFF = 1e16;
  170.  
  171. struct matrix {
  172.     i64 v[5][5];
  173.     int row, col; // number of row and column
  174. };
  175. i64 mod = 1000000007;
  176.  
  177. // multiplies two matrices and returns the result
  178. matrix multiply(matrix &a, matrix &b) {
  179.     assert(a.col == b.row);
  180.     matrix r;
  181.     r.row = a.row;
  182.     r.col = b.col;
  183.     for (int i = 0; i < r.row; i++) {
  184.         for (int j = 0; j < r.col; j++) {
  185.             i64 sum = 0;
  186.             for (int k = 0; k < a.col;  k++) {
  187.                 sum += a.v[i][k] * b.v[k][j];
  188.                 sum %= mod;
  189.             }
  190.             r.v[i][j] = sum;
  191.         }
  192.     }
  193.     return r;
  194. }
  195.  
  196. string binary(int p) {
  197.     string ret = "";
  198.     while (p > 0) {
  199.         ret += (p % 2 == 0) ? "0" : "1";
  200.         p /= 2;
  201.     }
  202.     reverse(ret.begin(), ret.end());
  203.     return ret;
  204. }
  205.  
  206. matrix power(matrix mat, int p) {
  207.     string bin = binary(p);
  208.     matrix ret = mat;
  209.     for (int i = 1; i < bin.size(); i++) {
  210.         ret = multiply(ret, ret);
  211.         if (bin[i] == '1') {
  212.             ret = multiply(ret, mat);
  213.         }
  214.     }
  215.     return ret;
  216. }
  217. i64 x, y;
  218. void fib(int n) {
  219.     if (n == 1) {
  220.         x = 1;
  221.         y = 1;
  222.         return;
  223.     }
  224.     if (n == 2) {
  225.         x = 2;
  226.         y = 1;
  227.         return;
  228.     }
  229.     if (n == 3) {
  230.         x = 3;
  231.         y = 2;
  232.         return;
  233.     }
  234.     matrix mat;
  235.     mat.row = mat.col = 2;
  236.     mat.v[0][0] = mat.v[0][1] = mat.v[1][0] = 1;
  237.     mat.v[1][1] = 0;
  238.     mat = power(mat, n - 1);
  239.     x = mat.v[0][0] + mat.v[0][1];
  240.     y = mat.v[1][0] + mat.v[1][1];
  241.     x %= mod;
  242.     y %= mod;
  243. }
  244.  
  245. int main() {
  246.     int n, k;
  247.     cin >> n >> k;
  248.     i64 ans = 0;
  249.     for(int i = n; i > 0;) {
  250.         fib(i);
  251.         ans += x * pow(i, k);
  252.         ans %= mod;
  253.         --i;
  254.         if(i <= 0) break;
  255.         ans += y * pow(i, k);
  256.         ans %= mod;
  257.         --i;
  258.     }
  259.     cout << ans << endl;
  260.     return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment