Showing posts with label Linear Algebra. Show all posts
Showing posts with label Linear Algebra. Show all posts

Saturday, January 26, 2019

[UVa] 13298 - Fibonacci Family Formula

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 13298 - Fibonacci Family Formula
Source            : UVA Online Judge
Category          : Linear Algebra
Algorithm         : Matrix Exponentiation
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. #define ll         long long int  
  6. #define mod        1000000009  
  7.   
  8. static const int maxn = 100 + 2;  
  9.   
  10. struct Mat  
  11. {  
  12.       int row, column;  
  13.       ll v[maxn][maxn];  
  14. };  
  15.   
  16. inline Mat multiply(const Mat &A, const Mat &B)  
  17. {  
  18.       Mat ret;  
  19.       ret.row = A.row;  
  20.       ret.column = B.column;  
  21.       for (int i = 0; i < A.row; i++)  
  22.       {  
  23.             for (int j = 0; j < B.column; j++)  
  24.             {  
  25.                   ll sum = 0;  
  26.                   for (int k = 0; k < A.column; k++)  
  27.                   {  
  28.                         sum += (A.v[i][k] * B.v[k][j]) % mod;  
  29.                         sum %= mod;  
  30.                   }  
  31.                   ret.v[i][j] = sum;  
  32.             }  
  33.       }  
  34.       return ret;  
  35. }  
  36.   
  37. inline Mat modPow(Mat mat, ll p)  
  38. {  
  39.       if (p == 1) return mat;  
  40.       if (p & 1) return multiply(mat, modPow(mat, p-1));  
  41.       Mat ret = modPow(mat, p >> 1);  
  42.       return multiply(ret, ret);  
  43. }  
  44.   
  45. ll arr[maxn];  
  46.   
  47. inline void Array(int k)  
  48. {  
  49.       arr[0] = 1;  
  50.       for (int i = 1; i < maxn; i++)  
  51.       {  
  52.             int temp = k;  
  53.             arr[i] = 0;  
  54.             for (int j = i-1; j >= 0 && temp; j--)  
  55.             {  
  56.                   arr[i] = (arr[i] + arr[j]) % mod;  
  57.                   --temp;  
  58.             }  
  59.       }  
  60. }  
  61.   
  62. int main()  
  63. {  
  64.       // freopen("in.txt", "r", stdin);  
  65.       int k;  
  66.       ll n;  
  67.       while (scanf("%d %lld", &k, &n) == 2)  
  68.       {  
  69.             if (k == 0 && n == 0) break;  
  70.             Mat mat;  
  71.             mat.row = k;  
  72.             mat.column = k;  
  73.             Array(k);  
  74.             for (int i = 0; i < k; i++)  
  75.             {  
  76.                   for (int j = 0; j < k; j++)  
  77.                   {  
  78.                         mat.v[i][j] = 0;  
  79.                         if (i == 0) mat.v[i][j] = 1;  
  80.                         else if (i == j + 1) mat.v[i][j] = 1;  
  81.                   }  
  82.             }  
  83.             if (n <= k)  
  84.             {  
  85.                   printf("%lld\n", arr[n]);  
  86.             }  
  87.             else  
  88.             {  
  89.                   mat = modPow(mat, n-k+1);  
  90.                   ll sum = 0;  
  91.                   for (int i = 0; i < k; i++)  
  92.                   {  
  93.                         sum += (mat.v[0][i] * arr[k-1-i]) % mod;  
  94.                         sum %= mod;  
  95.                   }  
  96.                   printf("%lld\n", sum);  
  97.             }  
  98.       }  
  99. }  

[UVa] 13277 - XOR Path

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 13277 - XOR Path 
Source            : UVA Online Judge
Category          : Linear Algebra, Graph Theory
Algorithm         : Fast Walsh-Hadamard transform, dfs 
Verdict           : Accepted

  1. #include <bits/stdc++.h>  
  2.   
  3. using namespace std;  
  4.   
  5. #define ll         long long  
  6.   
  7. static const int maxn = 1e5 + 5;  
  8. static const int N    = 1<<16;  
  9.   
  10. template <typename T> struct FWT  
  11. {  
  12.       void fwt(T io[], int n)  
  13.       {  
  14.             for (int d = 1; d < n; d <<= 1)  
  15.             {  
  16.                   for (int i = 0, m = d<<1; i < n; i += m)  
  17.                   {  
  18.                         for (int j = 0; j < d; j++)  
  19.                         {  
  20.                               T x = io[i+j], y = io[i+j+d];  
  21.                               io[i+j] = (x+y), io[i+j+d] = (x-y);  // xor  
  22.                               //io[i+j] = x+y;                     // and  
  23.                               //io[i+j+d] = x+y;                   // or  
  24.                         }  
  25.                   }  
  26.             }  
  27.       }  
  28.       void ufwt(T io[], int n)  
  29.       {  
  30.             for (int d = 1; d < n; d <<= 1)  
  31.             {  
  32.                   for (int i = 0, m = d<<1; i < n; i += m)  
  33.                   {  
  34.                         for (int j = 0; j < d; j++)  
  35.                         {  
  36.                               T x = io[i+j], y = io[i+j+d];  
  37.                               io[i+j] = (x+y)>>1, io[i+j+d] = (x-y)>>1; // xor  
  38.                               //io[i+j] = x-y;                          // and  
  39.                               //io[i+j+d] = y-x;                        // or  
  40.                         }  
  41.                   }  
  42.             }  
  43.       }  
  44.       void convolution(T a[], T b[], int n)  
  45.       {  
  46.             fwt(a, n);  
  47.             fwt(b, n);  
  48.             for (int i = 0; i < n; i++) a[i] = a[i] * b[i];  
  49.             ufwt(a, n);  
  50.       }  
  51.       void self_convolution(T a[], int n)  
  52.       {  
  53.             fwt(a, n);  
  54.             for (int i = 0; i < n; i++) a[i] = a[i] * a[i];  
  55.             ufwt(a, n);  
  56.       }  
  57. };  
  58.   
  59. struct node  
  60. {  
  61.     int v, w;  
  62.     node() {}  
  63.     node(int v, int w)  
  64.     {  
  65.         this->v = v;  
  66.         this->w = w;  
  67.     }  
  68. };  
  69.   
  70. vector <node> graph[maxn];  
  71. FWT <ll> fwt;  
  72. ll arr[N+5];  
  73.   
  74. void dfs(int u = 1, int p = -1, int x = 0)  
  75. {  
  76.       arr[x]++;  
  77.       for (auto &it : graph[u])  
  78.       {  
  79.             int v = it.v;  
  80.             int w = it.w;  
  81.             if (v == p) continue;  
  82.             dfs(v, u, x^w);  
  83.       }  
  84. }  
  85.   
  86. int main()  
  87. {  
  88.       //freopen("in.txt", "r", stdin);  
  89.   
  90.       int tc;  
  91.       scanf("%d", &tc);  
  92.       for (int tcase = 1; tcase <= tc; tcase++)  
  93.       {  
  94.             int tnode;  
  95.             scanf("%d", &tnode);  
  96.             for (int e = 1; e < tnode; e++)  
  97.             {  
  98.                   int u, v, w;  
  99.                   scanf("%d %d %d", &u, &v, &w);  
  100.                   graph[u].push_back(node(v, w));  
  101.                   graph[v].push_back(node(u, w));  
  102.             }  
  103.             dfs();  
  104.             fwt.self_convolution(arr, N);  
  105.             ll zero = (arr[0] - tnode) >> 1;  
  106.             printf("Case %d:\n", tcase);  
  107.             printf("%lld\n", zero);  
  108.             for (int i = 1; i < (1<<16); i++) printf("%lld\n", arr[i]>>1);  
  109.             for (int i = 0; i < maxn; i++) graph[i].clear();  
  110.             fill(begin(arr), end(arr), 0);  
  111.       }  
  112. }