那些容易写错的模板
dong0717这个大菜鸡一堆不会的
1.线性递推求组合数
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
const int mod= 998244353 ;
int f[5000005 ],ff[5000005 ];
int c (int n ,int m ){
return f[n]* ff[m]% mod* ff[n- m]% mod;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
int t,n;
cin>> t>> n;
f[1 ]= f[0 ]= ff[1 ]= ff[0 ]= 1 ;
for (int i= 2 ;i<= 5 e 6 ;i++ ){
f[i]= f[i- 1 ]* i% mod;
ff[i]= (mod- mod/ i)* ff[mod% i]% mod;
}
for (int i= 1 ;i<= 5 e 6 ;i++ )
ff[i]= ff[i- 1 ]* ff[i]% mod;
int ans= 0 ;
while (t-- ){
int n,m;
cin>> n>> m;
ans^= c (n,m);
}
cout<< ans;
}
2.费马小定理求组合数
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
const int mod= 1 e 9 + 7 ;
int qwq (int x ,int y ){
int a= x,g= 1 ;
while (y> 0 ){
if (y% 2 == 1 )
g= g* a% mod;
a= a* a% mod;
y/= 2 ;
}
return g;
}
int c (int n ,int m ){
int x= 1 ;
for (int i= 1 ;i<= m;i++ )
x= x* i% mod;
for (int i= 1 ;i<= n- m;i++ )
x= x* i% mod;
x= qwq (x,mod- 2 );
int y= 1 ;
for (int i= 1 ;i<= n;i++ )
y= y* i% mod;
return x* y% mod;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
int t;
cin>> t;
int ans= 0 ;
while (t-- ){
int n,m;
cin>> n>> m;
ans^= c (n,m);
}
cout<< ans;
return 0 ;
}
3.扩展欧几里得
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
int x,y;
int exgcd (int a ,int b ){
if (b== 0 ){
x= 1 ;
y= 0 ;
return a;
}
int d= exgcd (b,a% b);
int t= x;
x= y;
y= t- a/ b* y;
return d;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
int t;
cin>> t;
while (t-- ){
int a,b,c;
cin>> a>> b>> c;
int d= exgcd (a,b);
if (c% d!= 0 ){
cout<< "-1 \n " ;
continue ;
}
x= x* c/ d;
y= y* c/ d;
int tx= b/ d,ty= a/ d;
int k= ceil ((1.0 - x)/ tx);
x+= tx* k;
y-= ty* k;
if (y<= 0 ){
y+= ty* ceil ((1.0 - y)/ ty);
cout<< x<< " " << y<< ' \n ' ;
}
else
cout<< (y- 1 )/ ty+ 1 << " " << x<< " " << (y- 1 )% ty+ 1 << " " << x+ (y- 1 )/ ty* tx<< " " << y<< ' \n ' ;
}
return 0 ;
}
4.exCRT
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int __int128
int x,y;
int exgcd (int a ,int b ){
if (b== 0 ){
x= 1 ;
y= 0 ;
return a;
}
int d= exgcd (b,a% b);
int t= x;
x= y;
y= t- a/ b* y;
return d;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
long long n;
cin>> n;
int a1= 1 ,b1= 0 ;
for (int i= 1 ;i<= n;i++ ){
long long a2,b2;
cin>> a2>> b2;
int d= exgcd (a1,(__int128)a2);
x= (b1- b2)/ d* x;
b1-= a1* x;
a1= a2/ d* a1;
b1= (b1% a1+ a1)% a1;
}
cout<< (long long )((b1% a1+ a1)% a1);
return 0 ;
}
5.矩阵快速幂
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
const int Mod= 1 e 9 + 7 ;
int n,k;
struct node {
int a[105 ][105 ];
node (){
memset (a,0 ,sizeof (a));
}
void build (){
memset (a,0 ,sizeof (a));
for (int i= 1 ;i<= n;i++ )
a[i][i]= 1 ;
}
};
node operator * (node x ,node y ){
node z;
for (int i= 1 ;i<= n;i++ )
for (int k= 1 ;k<= n;k++ )
for (int j= 1 ;j<= n;j++ )
z.a[i][j]= (z.a[i][j]+ x.a[i][k]* y.a[k][j])% Mod;
return z;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
cin>> n>> k;
node QwQ;
for (int i= 1 ;i<= n;i++ )
for (int j= 1 ;j<= n;j++ )
cin>> QwQ.a[i][j];
node awa;
awa.build ();
while (k> 0 ){
if (k& 1 )
awa= awa* QwQ;
QwQ= QwQ* QwQ;
k/= 2 ;
}
for (int i= 1 ;i<= n;i++ ){
for (int j= 1 ;j<= n;j++ )
cout<< awa.a[i][j]<< " " ;
cout<< ' \n ' ;
}
return 0 ;
}
6.马拉车
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
int p[30000005 ];
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
string t;
cin>> t;
string s;
s+= '~' ;
s+= '#' ;
for (int i= 0 ;i< t.size ();i++ ){
s+= t[i];
s+= '#' ;
}
s+= '!' ;
int mr= 0 ,mid= 0 ,ans= 0 ;
for (int i= 1 ;i+ 1 < s.size ();i++ ){
if (i<= mr)
p[i]= min (p[2 * mid- i],mr- i+ 1 );
else
p[i]= 1 ;
while (s[i- p[i]]== s[i+ p[i]])
p[i]++ ;
if (i+ p[i]- 1 >= mr){
mr= i+ p[i]- 1 ;
mid= i;
}
ans= max (ans,p[i]- 1 );
}
cout<< ans;
return 0 ;
}
7.tarjan缩点
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
const int N= 1 e 4 + 5 ;
vector<int> a[N],a2[N];
int qwq[N],dfn[N],low[N],d,rt[N],in[N],awa[N],f[N];
stack<int> st;
void dfs (int u ){
dfn[u]= low[u]=++ d;
st.push (u);
for (auto v:a[u]){
if (! dfn[v]){
dfs (v);
low[u]= min (low[u],low[v]);
}
else if (! rt[v])
low[u]= min (low[u],dfn[v]);
}
if (low[u]== dfn[u]){
rt[u]= u;
awa[u]+= qwq[u];
while (st.top ()!= u){
rt[st.top ()]= u;
awa[u]+= qwq[st.top ()];
st.pop ();
}
st.pop ();
}
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
int n,m;
cin>> n>> m;
for (int i= 1 ;i<= n;i++ )
cin>> qwq[i];
for (int i= 1 ;i<= m;i++ ){
int u,v;
cin>> u>> v;
a[u].push_back (v);
}
for (int i= 1 ;i<= n;i++ ){
if (! dfn[i])
dfs (i);
}
for (int i= 1 ;i<= n;i++ ){
for (auto j:a[i]){
if (rt[i]== rt[j])
continue ;
a2[rt[i]].push_back (rt[j]);
in[rt[j]]++ ;
}
}
queue<int> q;
for (int i= 1 ;i<= n;i++ ){
if (! in[i])
q.push (i);
f[i]= awa[i];
}
while (! q.empty ()){
int x= q.front ();
q.pop ();
for (auto u:a2[x]){
f[u]= max (f[u],f[x]+ awa[u]);
in[u]-- ;
if (in[u]== 0 )
q.push (u);
}
}
int ans= 0 ;
for (int i= 1 ;i<= n;i++ )
ans= max (ans,f[i]);
cout<< ans;
return 0 ;
}
8.KMP
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
int KMP[1000005 ];
signed main (){
string s,t;
cin>> s>> t;
KMP[0 ]= 0 ;
int j= 0 ;
for (int i= 1 ;i< t.size ();i++ ){
while (t[i]!= t[j]&& j> 0 )
j= KMP[j- 1 ];
if (t[i]== t[j])
j++ ;
KMP[i]= j;
}
j= 0 ;
for (int i= 0 ;i< s.size ();i++ ){
while (s[i]!= t[j]&& j> 0 )
j= KMP[j- 1 ];
if (s[i]== t[j])
j++ ;
if (j== t.size ()){
cout<< i- t.size ()+ 2 << ' \n ' ;
j= KMP[j- 1 ];
}
}
for (int i= 0 ;i< t.size ();i++ )
cout<< KMP[i]<< " " ;
return 0 ;
}
9.线段树1
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
#define lc (p ) p<< 1
#define rc (p ) p<< 1 | 1
const int N= 1 e 5 + 5 ;
struct node {
int l,r,ans,tag;
}tree[4 * N];
int a[N];
void push_up (int p ){
tree[p].ans= tree[lc (p)].ans+ tree[rc (p)].ans;
}
void build (int l ,int r ,int p ){
tree[p].l= l,tree[p].r= r;
if (l== r){
tree[p].ans= a[l];
return ;
}
int mid= (l+ r)/ 2 ;
build (l,mid,lc (p));
build (mid+ 1 ,r,rc (p));
push_up (p);
}
void f (int p ,int k ){
tree[p].ans+= k* (tree[p].r- tree[p].l+ 1 );
tree[p].tag+= k;
}
void push_down (int p ){
f (lc (p),tree[p].tag);
f (rc (p),tree[p].tag);
tree[p].tag= 0 ;
push_up (p);
}
void update (int l ,int r ,int k ,int p ){
if (l<= tree[p].l&& r>= tree[p].r){
tree[p].ans+= k* (tree[p].r- tree[p].l+ 1 );
tree[p].tag+= k;
return ;
}
push_down (p);
int mid= (tree[p].l+ tree[p].r)/ 2 ;
if (l<= mid)
update (l,r,k,lc (p));
if (r> mid)
update (l,r,k,rc (p));
push_up (p);
}
int query (int l ,int r ,int p ){
if (l<= tree[p].l&& r>= tree[p].r)
return tree[p].ans;
int res= 0 ;
push_down (p);
int mid= (tree[p].l+ tree[p].r)/ 2 ;
if (l<= mid)
res+= query (l,r,lc (p));
if (r> mid)
res+= query (l,r,rc (p));
return res;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
int n,q;
cin>> n>> q;
for (int i= 1 ;i<= n;i++ )
cin>> a[i];
build (1 ,n,1 );
while (q-- ){
int op;
cin>> op;
if (op== 1 ){
int a,b,u;
cin>> a>> b>> u;
update (a,b,u,1 );
}
else {
int x,y;
cin>> x>> y;
cout<< query (x,y,1 )<< ' \n ' ;
}
}
return 0 ;
}
10.线段树2
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
#define lc p<< 1
#define rc p<< 1 | 1
const int N= 100005 ;
struct node {
int sum,add,mul,l,r;
}tree[N* 4 ];
int a[N];
int n,m,mod;
void push_up (int p ){
tree[p].sum= (tree[lc].sum+ tree[rc].sum)% mod;
}
void build (int l ,int r ,int p ){
tree[p].l= l,tree[p].r= r,tree[p].mul= 1 ;
if (l== r){
tree[p].sum= a[l];
return ;
}
int mid= (l+ r)/ 2 ;
build (l,mid,lc);
build (mid+ 1 ,r,rc);
push_up (p);
}
void push_down (int p ){
if (tree[p].mul!= 1 ){
tree[lc].mul= (tree[lc].mul* tree[p].mul)% mod;
tree[rc].mul= (tree[rc].mul* tree[p].mul)% mod;
tree[lc].sum= (tree[lc].sum* tree[p].mul)% mod;
tree[rc].sum= (tree[rc].sum* tree[p].mul)% mod;
tree[lc].add= (tree[lc].add* tree[p].mul)% mod;
tree[rc].add= (tree[rc].add* tree[p].mul)% mod;
tree[p].mul= 1 ;
}
if (tree[p].add!= 0 ){
int len1= tree[lc].r- tree[lc].l+ 1 ;
int len2= tree[rc].r- tree[rc].l+ 1 ;
tree[lc].sum= (tree[lc].sum+ tree[p].add* len1)% mod;
tree[rc].sum= (tree[rc].sum+ tree[p].add* len2)% mod;
tree[lc].add= (tree[lc].add+ tree[p].add)% mod;
tree[rc].add= (tree[rc].add+ tree[p].add)% mod;
tree[p].add= 0 ;
}
}
void update1 (int l ,int r ,int k ,int p ){//加法
if (l<= tree[p].l&& r>= tree[p].r){
int len= tree[p].r- tree[p].l+ 1 ;
tree[p].sum= (tree[p].sum+ k* len)% mod;
tree[p].add= (tree[p].add+ k)% mod;
return ;
}
push_down (p);
int mid= (tree[p].l+ tree[p].r)/ 2 ;
if (l<= mid)
update1 (l,r,k,lc);
if (r> mid)
update1 (l,r,k,rc);
}
void update2 (int l ,int r ,int k ,int p ){//乘法
if (l<= tree[p].l&& r>= tree[p].r){
tree[p].sum= (tree[p].sum* k)% mod;
tree[p].add= (tree[p].add* k)% mod;
tree[p].mul= (tree[p].mul* k)% mod;
return ;
}
push_down (p);
int mid= (tree[p].l+ tree[p].r)/ 2 ;
if (l<= mid)
update2 (l,r,k,lc);
if (r> mid)
update2 (l,r,k,rc);
}
int query (int l ,int r ,int p ){
if (l<= tree[p].l&& r>= tree[p].r)
return tree[p].sum;
push_down (p);
int res= 0 ;
int mid= (tree[p].l+ tree[p].r)/ 2 ;
if (l<= mid)
res= (res+ query (l,r,lc))% mod;
if (r> mid)
res= (res+ query (l,r,rc))% mod;
return res;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 ),cout.tie (0 );
cin>> n>> m>> mod;
for (int i= 1 ;i<= n;i++ )
cin>> a[i];
build (1 ,n,1 );
while (m-- ){
int op;
cin>> op;
if (op== 1 ){
int x,y,k;
cin>> x>> y>> k;
update2 (x,y,k% mod,1 );
}
if (op== 2 ){
int x,y,k;
cin>> x>> y>> k;
update1 (x,y,k% mod,1 );
}
if (op== 3 ){
int x,y;
cin>> x>> y;
cout<< query (x,y,1 )% mod<< ' \n ' ;
}
}
return 0 ;
}
11.高斯消元
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
double a[105 ][105 ],eps= 1 e- 7 ;
signed main (){
int n;
cin>> n;
for (int i= 1 ;i<= n;i++ )
for (int j= 1 ;j<= n+ 1 ;j++ )
cin>> a[i][j];
for (int i= 1 ;i<= n;i++ ){
int Max= i;
for (int j= i+ 1 ;j<= n;j++ )
if (fabs (a[j][i])> fabs (a[Max][i]))
Max= j;
for (int j= 1 ;j<= n+ 1 ;j++ )
swap (a[i][j],a[Max][j]);
if (fabs (a[i][i])< eps){
cout<< "No Solution" ;
return 0 ;
}
for (int j= n+ 1 ;j>= i;j-- )
a[i][j]/= a[i][i];
for (int j= 1 ;j<= n;j++ ){
if (i== j)
continue ;
double t= a[j][i]/ a[i][i];
for (int k= 1 ;k<= n+ 1 ;k++ )
a[j][k]-= t* a[i][k];
}
}
for (int i= 1 ;i<= n;i++ )
printf (" %.2f\n " ,a[i][n+ 1 ]);
return 0 ;
}