有待整理的模板
矩阵快速幂
1 #include<iostream> 2 #include<cstring> 3 #include<queue> 4 #include<cstdio> 5 #include<cmath> 6 #include<algorithm> 7 #define N 10 8 using namespace std; 9 struct Matrix{ 10 LL m[N][N]; 11 }init,unit; 12 int MOD; 13 Matrix Mult(Matrix m1,Matrix m2,int n=2){ 14 Matrix ans; 15 for(int i=0;i<n;i++) 16 for(int j=0;j<n;j++){ 17 ans.m[i][j]=0; 18 for(int k=0;k<n;k++) 19 ans.m[i][j]=((LL)ans.m[i][j]+m1.m[i][k]*m2.m[k][j])%MOD; 20 } 21 return ans; 22 } 23 Matrix Pow(Matrix m1,int b,int n=2){ 24 Matrix ans; 25 for(int i=0;i<n;i++) 26 for(int j=0;j<n;j++) 27 ans.m[i][j]=(i==j); 28 while(b){ 29 if(b&1) 30 ans=Mult(ans,m1,n); 31 m1=Mult(m1,m1,n); 32 b>>=1; 33 } 34 return ans; 35 } 36 Matrix Add(Matrix m1,Matrix m2,int n=2){ 37 Matrix ans; 38 for(int i=0;i<n;i++) 39 for(int j=0;j<n;j++) 40 ans.m[i][j]=((LL)m1.m[i][j]+m2.m[i][j])%MOD; 41 return ans; 42 } 43 Matrix slove(Matrix init,int k,int n=2){ 44 if(k==1) 45 return init; 46 Matrix temp=slove(init,k>>1,n); 47 temp=Add(temp,Mult(temp,Pow(init,k>>1))); 48 if(k&1) 49 return Add(temp,Pow(init,k)); 50 else 51 return temp; 52 } 53 int main(){ 54 int k,b,n; 55 while(scanf("%d%d%d%d",&k,&b,&n,&MOD)!=EOF){ 56 init.m[0][0]=init.m[0][1]=init.m[1][0]=1; 57 init.m[1][1]=0; 58 unit.m[0][0]=unit.m[1][1]=1; 59 unit.m[1][0]=unit.m[0][1]=0; 60 Matrix t; 61 t=Pow(init,b); 62 init=Pow(init,k); 63 //init.m[0][1]即第k项fib,为A^k 64 init=Add(unit,slove(init,n-1)); //B=A^k, B^0+B^1+B^2……+B^n-1 65 init=Mult(init,t); 66 printf("%I64d\n",init.m[0][1]); 67 } 68 return 0; 69 }
快速幂
1 long long quick_mod(long long a,long long b,long long m) { 2 long long ans = 1; 3 while (b) { 4 if (b&1) { 5 ans = (ans * a) % m; 6 b--; 7 } 8 b/=2; 9 a = a * a% m; 10 } 11 return ans; 12 }
组合数求模(逆元)
1 ll powmod(ll a, ll n) 2 { 3 ll ret = 1; 4 for (; n; n>>=1, a=a*a%mod) 5 if (n&1) 6 ret = ret*a%mod; 7 return ret % mod; 8 } 9 void init(ll m, ll k) 10 { 11 C[0] = 1; 12 for (ll i=1;i<=k;i++) 13 C[i] = ( C[i-1] * (m-i+1) %mod )* inv[i]% mod; 14 } 15 16 memset(inv, 0, sizeof inv); 17 for (int i=1;i<N;i++) inv[i] = powmod(i, mod-2)%mod;
分数表示模板
1 #include <cstdio> 2 #include <cstring> #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 500; 6 7 struct fraction { 8 long long numerator; // 分子 9 long long denominator; // 分母 10 fraction() { 11 numerator = 0; 12 denominator = 1; 13 } 14 fraction(long long num) { 15 numerator = num; 16 denominator = 1; 17 } 18 fraction(long long a, long long b) { 19 numerator = a; 20 denominator = b; 21 this->reduction(); 22 } 23 24 void operator = (const long long num) { 25 numerator = num; 26 denominator = 1; 27 this->reduction(); 28 } 29 30 void operator = (const fraction &b) { 31 numerator = b.numerator; 32 denominator = b.denominator; 33 this->reduction(); 34 } 35 36 fraction operator + (const fraction &b) const { 37 long long gcdnum = __gcd(denominator, b.denominator); 38 fraction tmp = fraction(numerator*(b.denominator/gcdnum) + b.numerator*(denominator/gcdnum), denominator/gcdnum*b.denominator); 39 tmp.reduction(); 40 return tmp; 41 } 42 43 fraction operator + (const int &b) const { 44 return ((*this) + fraction(b)); 45 } 46 47 fraction operator - (const fraction &b) const { 48 return ((*this) + fraction(-b.numerator, b.denominator)); 49 } 50 51 fraction operator - (const int &b) const { 52 return ((*this) - fraction(b)); 53 } 54 55 fraction operator * (const fraction &b) const { 56 fraction tmp = fraction(numerator*b.numerator, denominator * b.denominator); 57 tmp.reduction(); 58 return tmp; 59 } 60 61 fraction operator * (const int &b) const { 62 return ((*this) * fraction(b)); 63 } 64 65 fraction operator / (const fraction &b) const { 66 return ((*this) * fraction(b.denominator, b.numerator)); 67 } 68 69 void reduction() { 70 if (numerator == 0) { 71 denominator = 1; 72 return; 73 } 74 long long gcdnum = __gcd(numerator, denominator); 75 numerator /= gcdnum; 76 denominator /= gcdnum; 77 } 78 void print() { 79 if (denominator == 1) printf("%lld\n", numerator); 80 else { 81 long long num = numerator/denominator; 82 long long tmp = num; 83 int len = 0; 84 while (tmp) { 85 len++; 86 tmp/=10; 87 } 88 89 for (int i = 0; i < len; i++) printf(" "); 90 if (len != 0) printf(" "); 91 printf("%lld\n",numerator%denominator); 92 93 if (num != 0) printf("%lld ", num); 94 tmp = denominator; 95 while (tmp) { 96 printf("-"); 97 tmp/=10; 98 } 99 puts(""); 100 101 for (int i = 0; i < len; i++) printf(" "); 102 if (len != 0) printf(" "); 103 printf("%lld\n",denominator); 104 } 105 } 106 } f[maxn]; 107 108 int main() { 109 int n; 110 while (scanf("%d", &n) != EOF) 111 { 112 f[0] = 0; 113 for (int i = 1; i <= n; i++) 114 f[i] = f[i-1] + fraction(1, i); 115 f[n] = f[n] * n; 116 117 f[n].print(); 118 } 119 return 0; 120 }