1 #define LL long long 2 //快速幂 a^p%Mod 3 LL pow_mod(LL a, LL p, LL Mod){ 4 LL X = 1LL; 5 if(a == 0) return 0; 6 while(p){ 7 if(p&1) X = X*a%Mod; 8 a = a*a%Mod; 9 p >>= 1; 10 } 11 return X; 12 } 13 //扩展欧几里得 14 void gcd(LL a, LL b, LL& d, LL& x, LL& y){ 15 if(!b) { d = a, x = 1, y = 0;} 16 else { gcd(b, a%b, d, y, x); y -= x*(a/b);} 17 } 18 //计算模n下a的逆 19 //第一种利用GCD 20 LL inv(LL a, LL n){ 21 LL d, x, y; 22 gcd(a, n, d, x, y); 23 return d == 1 ? (x+n)%n : -1; 24 } 25 //第二种利用欧拉定理 26 //pow_mod(a, n-2, n)
KMP
1 const int maxn = 1e5+10; 2 int Next[maxn]; 3 int KMP(char* aim, char* str){ 4 memset(Next, 0, sizeof(Next)); 5 int num = 0, len = strlen(aim), Len = strlen(str); 6 for(int i = 1; i < len; i++){ 7 int j = Next[i]; 8 while(j && aim[i] != aim[j]) j = Next[j]; 9 Next[i+1] = (aim[i] == aim[j]) ? j+1 : 0; 10 } 11 int j = 0; 12 for(int i = 0; i < Len; i++){ 13 while(j && aim[j] != str[i]) j = Next[j]; 14 if(aim[j] == str[i]) j++; 15 if(j == len) num++; 16 } 17 return num; 18 }
欧拉筛
1 const int maxn = 1e5+10; 2 int p[maxn], phi[maxn]; 3 bool vis[maxn]; 4 int Euler(int n){ 5 int i, j, k; 6 phi[1] = 1; 7 for (int i = 2; i < n; ++i){ 8 if (!vis[i]){ 9 p[cnt++] = i; 10 phi[i] = i - 1; 11 } 12 for (int j = 0; j < cnt && i * p[j] < n; ++j){ 13 vis[i * p[j]] = true; 14 if (i % p[j]) phi[i * p[j]] = phi[i] * phi[p[j]]; 15 else { 16 phi[i * p[j]] = phi[i] * p[j]; 17 break; 18 } 19 } 20 } 21 return cnt; 22 }
输入输出外挂
1 int read(){ 2 int x=0,f=1,ch=getchar(); 3 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 4 while(ch<=‘9‘&&ch>=‘0‘){x=(x<<1)+(x<<3)+ch-‘0‘,ch=getchar();} 5 return x*f; 6 } 7 void out(int x){ 8 if(x > 9) out(x/10); 9 putchar(x%10+‘0‘); 10 }
测试时间
1 printf("%.3lf\n", (double)clock()/CLOCKS_PER_SEC);
1 struct Node{ 2 ll x[16][16]; 3 }; 4 Node mul(Node xx, Node yy){ 5 Node X; 6 for(int i = 0; i < d; ++i){ 7 for(int j = 0; j < d; ++j){ 8 X.x[i][j] = 0; 9 for(int k = 0; k < d; ++k){ 10 X.x[i][j] = (X.x[i][j] + (xx.x[i][k]*yy.x[k][j])%m) % m; 11 } 12 } 13 } 14 return X; 15 } 16 Node q_m(Node A, ll x){ 17 Node AAA; 18 for(int i = 0; i < d; ++i){ 19 for(int j = 0; j < d; ++j){ 20 AAA.x[i][j] = (i == j); 21 //printf("%lld ", AAA.x[i][j]); 22 } 23 //puts(""); 24 } 25 while(x){ 26 if(x&1) AAA = mul(AAA, A); 27 A = mul(A, A); 28 x >>= 1; 29 } 30 return AAA; 31 }
只有不断学习才能进步!