暴力枚举类型题小结

A. P2241 统计方形(数据加强版)

#include
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
//找规律....... 
using namespace std;
const int N = 1e5;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

int main(){

	ll n,m,sqr = 0,rec;
	cin>>n>>m;
	
	ll total = (n * (n + 1) / 2) * (m * (m + 1) / 2);
	
	for(int k = 1;k <= min(n,m);++k)sqr += (n - k + 1) * (m - k + 1);
	
	rec = total - sqr;
	
	cout<<sqr<<" "<<rec;
	return 0;
} 

B. P3392 涂国旗

#include
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
//AC原因 数据太弱 
using namespace std;
const int N = 55;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

char table[N][N];

struct node{
	int w,b,r;
}arr[N];

int main(){
	FAST;
	
	int n,m,ans = INF,cur;
	int w,b,r;
	cin>>n>>m;
	
	for(int i = 1;i <= n;i++){
		w = 0,b = 0,r = 0;
		for(int j = 1;j <= m;j++){
			cin>>table[i][j];
			if(table[i][j] == 'W')w++;
			if(table[i][j] == 'B')b++;
			if(table[i][j] == 'R')r++;
		}
		arr[i].w = w;
		arr[i].b = b;
		arr[i].r = r;
	}
		
	for(int i = 1;i <= n - 2;i++){
		for(int j = 1;j <= n - i - 1;j++){
			cur = 0;
			for(int k = 1;k <= i;k++)cur += arr[k].b + arr[k].r;
			for(int l = i + 1;l <= i + j;l++)cur += arr[l].w + arr[l].r;
			for(int q = i + j + 1;q <= n;q++)cur += arr[q].w + arr[q].b;
			ans = min(ans,cur);
		}
	}
	
	cout<<ans;
	return 0;
} 

C. P3799 妖梦拼木棒(组合数学)

#include
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long

//i != j 这样的组合的数量==长度为a的木棒数量×长度为b的木棒数量×长度为(a+b)的数量。
//i == j 所以这样的组合的数量==长度为a的木棒数量×(长度为b的木棒数量 -1)×长度为(a+b)的数量。

using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);

int comb(int x){
	return (x * ( x - 1) / 2) % mod;
}

int vis[N],num[N];

int main(){
	FAST;
	
	int n,ans = 0,maxn = -1;
	cin>>n;
	for(int i = 1;i <= n;i++){
		cin>>num[i];
		maxn = max(maxn,num[i]);
		vis[num[i]]++;
	}
	
	for(int i = 1;i <= maxn;i++){
		for(int j = i;j <= maxn;j++){
			if(i + j > 5000)break;
			if(i == j)ans = (ans + comb(vis[i]) * comb(vis[i + j])) % mod;
			else ans = (ans + (vis[i] * vis[j] % mod) * (comb(vis[i + j]) % mod)) % mod;
		}
	}
	
	cout<<ans;
	return 0;
} 

你可能感兴趣的:(暴力枚举)