跳到正文

算法·理论

算法模板合集

_Michael0727_约 16 分钟
原文
文章信息与版本

文章信息

发布
2024年10月22日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 16 分钟

版本

  1. v1当前

这篇专栏记录了各种常用的算法模板,可以自取


高精度

高精加

C++
# include <bits/stdc++.h>
using namespace std ;
int a [5005] , b [5005] , c [5005] , cura , curb ;
void s2ia ( string s )
{
	for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		cura ++ ;
		a [cura] = s [i] - '0' ;
	}
}
void s2ib ( string s )
{
	for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		curb ++ ;
		b [curb] = s [i] - '0' ;
	}
}
void pls ( )
{
	for ( int i = 1 ; i <= max ( cura , curb ) ; i ++ )
	{
		c [i] += a [i] + b [i] ;
		c [i + 1] += ( c [i] / 10 ) ;
		c [i] %= 10 ;
	}
	bool flg = 1 ;
	for ( int i = max ( cura , curb ) + 1 ; i >= 1 ; i -- )
	{
		if ( ! c [i] && flg )
		{
			continue ;
		}
		flg = 0 ;
		cout << c [i] ;
	}
	if ( flg )
	{
		cout << 0 << endl ;
	}
}
int main ( )
{
	string ta , tb ;
	cin >> ta >> tb ;
	s2ia ( ta ) ;
	s2ib ( tb ) ;
	pls ( ) ;
	return 0 ;
}

高精减

C++
# include <bits/stdc++.h>
using namespace std ;
int a [5005] , b [5005] , c [5005] , cura , curb ;
void s2ia ( string s )
{
	for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		cura ++ ;
		a [cura] = s [i] - '0' ;
	}
}
void s2ib ( string s )
{
	for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		curb ++ ;
		b [curb] = s [i] - '0' ;
	}
}
void sbt ( )
{
	for ( int i = 1 ; i <= max ( cura , curb ) ; i ++ )
	{
		if ( a [i] < b [i] )
		{
			a [i + 1] -- ;
			a [i] += 10 ;
		}
		c [i] += a [i] - b [i] ;
	}
	bool flg = 1 ;
	for ( int i = max ( cura , curb ) ; i >= 1 ; i -- )
	{
		if ( ( ! c [i] ) && flg )
		{
			continue ;
		}
		flg = 0 ;
		cout << c [i] ;
	}
	if ( flg )
	{
		cout << 0 << endl ;
	}
}
int main ( )
{
	string ta , tb ;
	cin >> ta >> tb ;
	s2ia ( ta ) ;
	s2ib ( tb ) ;
	sbt ( ) ;
	return 0 ;
}

高精乘

C++
# include <bits/stdc++.h>
using namespace std ;
int a [5005] , b [5005] , c [5005] , cura , curb ;
void s2ia ( string s )
{
	for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		cura ++ ;
		a [cura] = s [i] - '0' ;
	}
}
void s2ib ( string s )
{
	for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		curb ++ ;
		b [curb] = s [i] - '0' ;
	}
}
void mul ( )
{
	for ( int i = 1 ; i <= cura ; i ++ )
	{
		for ( int j = 1 ; j <= curb ; j ++ )
		{
			c [i + j - 1] += a [i] * b [j] ;
		}
	}
	for ( int i = 1 ; i < cura + curb ; i ++ )
	{
		c [i + 1] += c [i] / 10 ;
		c [i] %= 10 ;
	}
	bool flg = 1 ;
	for ( int i = cura + curb ; i >= 1 ; i -- )
	{
		if ( ( ! c [i] ) && flg )
		{
			continue ;
		}
		flg = 0 ;
		cout << c [i] ;
	}
	if ( flg )
	{
		cout << 0 << endl ;
	}
}
int main ( )
{
	string ta , tb ;
	cin >> ta >> tb ;
	s2ia ( ta ) ;
	s2ib ( tb ) ;
	mul ( ) ;
	return 0 ;
}

高精除 (高精除以低精)

C++
# include <bits/stdc++.h>
using namespace std ;
long long a [5005] , b , c [5005] , cura ;
void s2ia ( string s )
{
	for ( long long i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		cura ++ ;
		a [cura] = s [i] - '0' ;
	}
}
void div ( )
{
	long long r = 0 ;
	for ( long long i = cura + 1 ; i >= 1 ; i -- )
	{
		r *= 10 ;
		r += a [i] ;
		c [i] = r / b ;
		r %= b ;
	}
	bool flg = 1 ;
	for ( long long i = cura ; i >= 1 ; i -- )
	{
		if ( ! c [i] && flg )
		{
			continue ;
		}
		flg = 0 ;
		cout << c [i] ;
	}
	if ( flg )
	{
		cout << 0 ;
	}
}
int main ( )
{
	string ta ;
	cin >> ta >> b ;
	s2ia ( ta ) ;
	if ( ta [0] == 0 || b == 0 )
	{
		cout << 0 << endl ;
		return 0 ;
	}
	div ( ) ;
	return 0 ;
}

高精模 (高精模低精)

C++
# include <bits/stdc++.h>
using namespace std ;
long long a [5005] , b , c [5005] , cura ;
void s2ia ( string s )
{
	for ( long long i = s . size ( ) - 1 ; i >= 0 ; i -- )
	{
		cura ++ ;
		a [cura] = s [i] - '0' ;
	}
}
void mod ( )
{
	long long r = 0 ;
	for ( long long i = cura + 1 ; i >= 1 ; i -- )
	{
		r *= 10 ;
		r += a [i] ;
		c [i] = r / b ;
		r %= b ;
	}
	bool flg = 1 ;
	for ( long long i = cura ; i >= 1 ; i -- )
	{
		if ( ! c [i] && flg )
		{
			continue ;
		}
		flg = 0 ;
	}
	if ( flg )
	{
		cout << 0 ;
	}
	cout << r << endl ;
}
int main ( )
{
	string ta ;
	cin >> ta >> b ;
	s2ia ( ta ) ;
	if ( ta [0] == 0 || b == 0 )
	{
		cout << 0 << endl ;
		return 0 ;
	}
	mod ( ) ;
	return 0 ;
}

图论

Floyd

C++
# include <bits/stdc++.h>
using namespace std ;
int main ( )
{
	int n , m ;
	cin >> n >> m ;
	int g [n] [n] ;
	memset ( g , 0x7f , sizeof ( g ) ) ;
	for ( int i = 1 ; i <= n ; i ++ )
	{
		g [i] [i] = 0 ;
	}
	while ( m -- )
	{
		int x , y , z ;
		cin >> x >> y >> z ;
		g [x] [y] = g [y] [x] = z ;
	}
	int src , tgt ;
	cin >> src >> tgt ;
	for ( int k = 1 ; k <= n ; k ++ )
	{
		for ( int i = 1 ; i <= n ; i ++ )
		{
			for ( int j = i + 1 ; j <= n ; j ++ )
			{
				if ( ! ( j - k ) || ! ( i - k ) )
				{
					continue ;
				}
				g [i] [j] = min ( g [i] [j] , g [i] [k] + g [j] [k] ) ;
				g [j] [i] = g [i] [j] ;
			}
		}
	}
	cout << g [src] [tgt] << endl ;
	return 0 ;
}

Dijkstra

C++
# include <bits/stdc++.h>
using namespace std ;
struct edg
{
	int v , w ;
} ;
long long n , m , s , dis [114514] ;
bool vis [114514] ;
vector < edg > e [114514] ;
void dks ( int n , int s )
{
	memset ( dis , 1e9 + 7 , sizeof ( dis ) ) ;
	dis [s] = 0 ;
	for ( int i = 1 ; i <= n ; i ++ )
	{
	    long long u = 0 , t = 2147483647 ;
		for ( int j = 1 ; j <= n ; j ++ )
		{
			if ( ! vis [j] && dis [j] < t )
			{
				u = j ;
				t = dis [j] ;
			}
		}
		vis [u] = 1 ;
		for ( auto e1 : e [u] )
		{
			int v = e1 . v , w = e1 . w ;
			dis [v] = min ( dis [v] , dis [u] + w ) ;
		}
	}
}
int main ( )
{
	cin >> n >> m >> s ;
	for ( long long i = 1 ; i <= m ; i ++ )
	{
		int u ;
		edg e1 ;
		cin >> u >> e1 . v >> e1 . w ;
		e [u] . push_back ( e1 ) ;
	}
	dks ( n , s ) ;
	for ( long long i = 1 ; i <= n ; i ++ )
	{
		cout << min ( dis [i] , ( long long ) ( 2147483647 ) ) << ' ' ;
	}
	cout << endl ;
	return 0 ;
}

动态规划

最长不下降子序列

C++
# include <bits/stdc++.h>
using namespace std ;
int main ( )
{
	int n ;
	cin >> n ;
	int a [n + 1] = { -1 } , dp [n + 1] = { 0 } , ans = -1 ;
	for ( int i = 1 ; i <= n ; i ++ )
	{
		cin >> a [i] ;
		for ( int j = 0 ; j < i ; j ++ )
		{
			if ( a [i] >= /*只是上升就去掉'='号*/ a [j] )
			{
				dp [i] = max ( dp [i] , dp [j] + 1 ) ;
			}
		}
		ans = max ( dp [i] , ans ) ;
	}
	cout << ans << endl ;
	return 0 ;
}

背包问题

0/1背包

C++
# include <bits/stdc++.h>
using namespace std ;
int main ( )
{
	int m , n ;
	cin >> n >> m ;
	int dp [m + 1] ;
	memset ( dp , 0 , sizeof ( dp ) ) ;
	for ( int i = 1 ; i <= n ; i ++ )
	{
		int w , c ;
		cin >> w >> c ;
		for ( int j = m ; j >= w ; j -- )
		{
			dp [j] = max ( dp [j - w] + c , dp [j] ) ;
		}
	}
	cout << dp [m] << endl ;
	return 0 ;
}

完全背包

C++
# include <bits/stdc++.h>
using namespace std ;
int main ( )
{
	int m , n ;
	cin >> m >> n ;
	int w [n + 1] , v [n + 1] , dp [m + 1];
	memset ( dp , 0 , sizeof ( dp ) ) ;
	for ( int i = 1 ; i <= n ; i ++ )
	{
		cin >> w [i] >> v [i] ;
	}
	for ( int i = 1 ; i <= n ; i ++ )
	{
		for ( int j = w [i] ; j <= m ; j ++ )
		{
			dp [j] = max ( dp [j] , dp [j - w [i]] + v [i] ) ;
		}
	}
	cout << dp [m] << endl ;
	return 0 ;
}

多重背包

C++
# include <bits/stdc++.h>
using namespace std ;
int main ( )
{
	int n , m ;
	cin >> n >> m ;
	int v [n + 1] , w [n + 1] , s [n + 1] , dp [m + 1] ;
	for ( int i = 1 ; i <= n ; i ++ )
	{
		cin >> v [i] >> w [i] >> s [i] ;
	}
	for ( int i = 1 ; i <= n ; i ++ )
	{
		for ( int j = m ; j >= 0 ; j -- )
		{
			for ( int k = 0 ; k < s [i] ; k ++ )
			{
				if ( j - k * v [i] < 0 )
				{
					break ;
				}
				else
				{
					dp [j] = max ( dp [j] , dp [j - k * v [i]] + k * w [i] ) ;
				}
			}
		}
	}
	cout << dp [m] << endl ;
	return 0 ;
}

其他

快速幂

C++
# include <bits/stdc++.h>
using namespace std ;
long long fmd ( long long bse , long long pwr , long long mod )
{
    if ( ! bse )
    {
        return 0 ;
    }
	if ( ! pwr )
	{
		return 1 ;
	}
	unsigned long long ans = 1 ;
	while ( pwr )
    {
	    if ( pwr & 1 )
		{
			ans = ( ans * bse ) % mod ;
		}
	    pwr >>= 1 ;
	    bse = ( bse * bse ) % mod ;
    }
    return ans % mod ;
}
int main ( )
{
	long long a , b , p ;
	cin >> a >> b >> p ;
	cout << a << '^' << b << " mod " << p << '='  ;
	a %= p ;
	cout << fmd ( a , b , p ) << endl ;
    return 0 ;
}

Upd : 2024/10/24

原文