Algorithm - violence enumeration

Posted by levidyllan on Sun, 20 Feb 2022 14:28:39 +0100

Basic conditions of enumeration:

(1) time conditions:

The first is the time condition. Generally speaking, the mainstream OJ can run operations with operands less than 10 ^ 7 under the time limit of 1000ms (generally, it is safer to operate within 10 ^ 6). Therefore, it is best to look at the data range before adopting the enumeration method to ensure that the execution operands of the whole program will not exceed the order of 10 ^ 6-10 ^ 7. If so, try to change the enumeration dimension or use other algorithms.

(2) programming conditions:

The second is the implementation conditions of programming. In terms of programming implementation, generally speaking, violent enumeration requires two conditions. One is that the scope of enumeration generally needs to be continuous. If the scope of enumeration is discrete, it is generally difficult to use the for loop to enumerate all States, so the integrity of the solution cannot be guaranteed (but sometimes the data seems discrete, but it can actually become continuous after processing). The second condition is that the enumeration content needs to be known and cannot be unknown when enumerating to a certain place (but this is generally met).

Problem solving ideas

(1) Enumeration variable found (push unknown from known as far as possible)

(2) Find and try to narrow the scope (according to the meaning of the question, but not too precise, it can be too large, and the condition can be judged in if)

(3) Operate according to the meaning of the question (prune as much as possible)

catalogue

P2241 statistical square

P1618 triple strike (upgraded version)

P3392 painting national flag

P3799 demon dream stick

D: Cargo placement

1, P2241 statistical square

Readily available:

The length and width of the matrix composed of sub rectangles are obtained by subtracting different numbers from the length and width of the original rectangle, that is, (n-b)*(m-a) (a ≠ b)

The length and width of the matrix composed of sub squares are obtained by subtracting the same number from the length and width of the original rectangle, that is (n-b)*(m-a) (a=b)

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,m,zfx=0,jx=0;    //The thief is big, remember long long
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			if(i==j) zfx+=(n-i)*(m-j);
			else jx+=(n-i)*(m-j);
		}
	cout<<zfx<<" "<<jx;
	return 0;
	
}

P1618 triple strike (upgraded version)

Idea: because there are just three groups of numbers, accounting for 1 - 9, use special judgment to observe whether they account for 1 - 9

for(j=1;j<=9;j++)    
    if(num[j]==0)

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,i,j=0,num[10]={0},t=0;
	cin>>a>>b>>c;
	for(i=1;i<=999;i++)
	{
		if(a*i>100&&c*i<999)
		{
			int x=a*i;
			num[x/100]++;
			num[x/10%10]++;
			num[x%10]++;
			
			int y=b*i;
			num[y/100]++;
			num[y/10%10]++;
			num[y%10]++;
			
			int z=c*i;
			num[z/100]++;
			num[z/10%10]++;
			num[z%10]++;
		}
		else continue;
		
		for(j=1;j<=9;j++)    //Core special judgment
			if(num[j]==0)
				break;
		if(j>=10)
		{
			cout<<a*i<<" "<<b*i<<" "<<c*i<<endl;
			t=1;
		}
		for(j=1;j<=9;j++)
			num[j]=0;
		
	}
	if(t==0)
		cout<<"No!!!";
	return 0;
} 

P3392 painting national flag

Just enumerate the boundaries of each color.

#include<bits/stdc++.h>
using namespace std;
const int N=51;
int w[N],b[N],r[N];
int n,m;
int main()
{
	char ch;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++) //Number of three colors per line
		{
			cin>>ch;
			if(ch=='W') w[i]++;
			if(ch=='B') b[i]++;
			if(ch=='R') r[i]++;
		}
	}
	int minv=10000000;
	for(int i=1;i<=n-2;i++) //White boundary, n-2 at least two lines are not white
	{
		for(int j=i+1;j<=n-1;j++) //i+1 starts after the white line, and at least one line after n-1 is not blue
		{
			int sum=0;
			for(int k=1;k<=n;k++) //To judge, you only need to find the line painted with white and blue, and the rest is red
			{
				if(k<=i) sum+=m-w[k];
				else if(k<=j) sum+=m-b[k];
				else sum+=m-r[k];
			}
			minv=min(minv,sum); //Judge the total number of less colored
		}
	}
	cout<<minv<<endl;
	return 0;
}

P3799 demon dream stick

Luogu P3799 demon dream stick_ Study_ Study_ Blog of X - CSDN bloghttps://blog.csdn.net/Study_Study_X/article/details/122931184 I don't understand, but I think it's a good question

D: cargo placement

#include<iostream>
#include<cmath>
using namespace std;
typedef  long long ll;
ll n = 2021041820210418;
ll a[100010],c,res; 
int main()
{

  for(int i = 1; i <=sqrt(n); i++)//Must sqrt or explode long long 
  {
    if(n % i == 0)
	{
      a[++c] = i;
      a[++c] = n/i;//Find the factors on the left and right sides of sqrt 
    }
  }
  for(int l = 1; l <= c; l++)
  	for(int w = 1; w <= c; w++)
  		for(int h = 1; h <= c; h++)
    		if(a[l]*a[w]*a[h] == n) res++;
 
  cout <<res;
  return 0;
}
2430

Topics: C C++ Algorithm