"Lanqiao Cup" enumeration method (1) - buy a hundred chickens for a hundred dollars, arithmetic prime number sequence

1. Enumeration method

The enumeration method, also known as the violent algorithm, refers to the method of examining all possible situations of a certain type of events one by one, and then drawing general conclusions. The idea of ​​the enumeration method is to enumerate all possible answers to the question one by one, and then judge whether the answer is satisfied according to the conditions, keep the satisfied ones, and discard the unsatisfied ones.
The enumeration method is more intuitive, and the algorithm is easy to understand, but in actual use, the enumeration method should minimize the number of variables and search space, so that the efficiency of the algorithm can be improved.

2. Buy a hundred chickens for a hundred dollars

Zhang Qiujian, an ancient Chinese mathematician, once proposed the famous problem of "buying a hundred chickens for a hundred dollars" in the book "Suanjing". The description of the problem is as follows: one chicken is worth five
; Three, it’s worth one; if you buy a hundred chickens for a hundred dollars, how much are the wengs, hens, and chicks? The
data scale of this question is relatively small, and it can be enumerated directly by using modern computer algorithms, because the number of roosters, hens, and chicks is all within 0 ~100 only, so the whole space can be enumerated directly. The case code is as follows:

for(i=0; i<=100;i++)
   for(j=0;j<=100;j++ )
     for(k=0; k<=100;k+ +)
      if(5* i+3* j+k/3==100 && k%3==0 && i+j+k==100)
      printf("公鸡号2d只,母鸡号2d只,小鸡号2d只\n",i,j,k);

If the known conditions of this question remain unchanged, the data scale will be enlarged. If it becomes a problem of "buying ten thousand chickens for ten thousand dollars", please calculate how many ways there are in total.
At this time, it is necessary to reduce the search space through transformation.
In this question, there are three variables i, j, and k. In fact, as long as the values ​​of any two variables are determined, the value of the other variable has been determined. For example: i=1, j=2, then k can only be equal to 100-1-2=97 to meet the requirements, so the search space can be reduced through the relationship between variables.
Through the analysis, it can be seen that the variable k of the chicken does not need to search the entire space, because the value of k must be a multiple of 3 to meet the condition, so that the search space can be further reduced, so the problem of "buying ten thousand chickens for ten thousand dollars" can be as follows solve.

int count=0;
for(-i=0;i<=10000;i++)
   for(k=0;k<=10000;k=k+3)
   {
    j=10000-i-k;
    1f(j< 0) continue;
    1f(5* i+3* j+k/3==10000)
   count++;
  }
printf("%d", count);

If the scale of this question is further expanded to "buy a million chickens for a million dollars", then using the above algorithm will time out, and it is necessary to further reduce the scale of the enumeration.
Through the analysis of the topic, we can see that if you want to buy the same number of chickens with a certain amount of money, chickens are essential (because only with chickens can the number be balanced), and it is a multiple of 3. Through analysis, it can be seen that:
three chicks + one hen = four chickens, and these four chickens are worth four Wen, which just reaches a balance.
Six chicks + one rooster = seven chickens, and these four chickens cost seven coins, just to achieve a balance.
That is to say, if you want to buy the same number of chickens with a certain amount of money, only these two groups can reach a balance;
three chicks + one hen...... ①Six
chicks + A rooster... ②This
problem becomes the number of solution spaces for the equation 4x+7y=1000000 (x represents the number of groups ①, y represents the number of groups ②). In this equation, 4 is a factor of 1000000, and 7 is a prime number. It is easy to get the law of the solution space: x
y
250000 0
249993 4
249986 8
249979 12
249972 16
249965 20 ...
This solution space, namely the hen The group can be reduced by 7 groups each time, and the rooster group can be increased by 4 groups each time to achieve a balance, so the program can be further simplified as:

int count=0;
for(x= 250000;x>=0;  x=x-7)
count+ + ;
printf("id", count) ;

It is further simplified to
printf("ad", 1000000/28+1);
please analyze and think about this formula yourself.
It can be seen from this question that the enumeration method is very flexible on the one hand, and on the other hand, different algorithms need to be explored according to the scale of the problem. This is also the place where candidates are often tested in algorithm competitions. You must learn in the later learning process Analyze the scale of the problem.
On most algorithm competition test platforms, the number of operations per second is about 1e7. Under this limitation, an algorithm with a certain time complexity has a processing upper limit of data size. The specific time complexity and data size upper limit are shown in the following table.

 3. Arithmetic prime sequence

[Problem description]
2,3,5,7,11,13,.. are prime numbers.
7,37,67,97,127.157. Such an arithmetic sequence composed entirely of prime numbers is called an arithmetic prime sequence.
The above sequence has a tolerance of 30 and a length of 6.
year 2004. Collaborating with Terence Tao, Green proved that there is an arithmetic prime sequence of any length. This is an amazing achievement in the field of number theory!
With this theory as the basis, please use the computer in your hand to search with confidence: what is the minimum tolerance value of the arithmetic prime number sequence with a length of 10? Note: an integer
needs to be submitted , do not fill in any redundant content and description text.

[hint]

This question is about prime numbers, so the first step is to learn the algorithm for judging prime numbers. There are two commonly used prime number judgment algorithms: one is the enumeration method based on the definition of prime numbers, and the other is the screening method.
The idea of ​​the enumeration method based on the definition of prime numbers is very simple. To determine whether n is a prime number, you only need to enumerate from 2 to n-1 whether there are numbers that can be divisible by n. This method will not be described in detail here, and the screening method will be mainly introduced here.
(1) Screening method
The screening method is very suitable for finding whether each number in an integer interval is a prime number, and the larger the interval, the higher the efficiency. The sieving method is said to have been invented by Eratosthenes (about 274 BC-194 BC) in ancient Greece, so it is also called the sieve of Eratosthenes.
The specific method is: first arrange the N natural numbers in order, because 1 is not a prime number, nor is it a composite number, so cross out 1 and start from 2.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22... N The
second number 2 is a prime number, keep it, and cross out all the numbers after 2 that are divisible by 2.
2 3 5 7 9 11 13 15 17 19 21 ...N
At this time, the first number after 2 that is not crossed out is 3, keep 3, and cross out all the numbers after 3 that are divisible by 3 .
2 3 5 7 11 13 17 19 ... N
3 The first number that is not crossed out after 3 is 5, keep 5, and then cross out all the numbers after 5 that are divisible by 5.
Doing this all the time will screen out all composite numbers that do not exceed N, and the remaining numbers are all prime numbers that do not exceed N.
Because the Greeks wrote numbers on waxed boards, and every time a number was crossed out, a small dot was marked on it. After the work of seeking prime numbers was completed, many small dots were like a sieve, so the Ela Tosthenes' method is called "the sieve of Eratosthenes", or "sieve method" for short.
(2) The idea of ​​the program
After all the prime numbers in 2~N are judged, the enumeration can be carried out by violent algorithm. There are currently 3 uncertain variables: the range of N, the tolerance, and the starting value of the sequence of prime numbers.
The range of N can be set as a constant, which needs to be large enough to satisfy the sequence you are looking for.
The tolerance and the initial value of the prime number sequence are respectively set as two variables, and the enumeration method is used to carry out two layers of circulation. During the loop process, judge whether there is an arithmetic prime number sequence satisfying the length of 10, and if so, output its tolerance, which is the minimum value of the tolerance.

Arithmetic Prime Sequence (Answer)

#include <iostream>
#include <cmath> 
using namespace std;
const int N=10000; 
int a[N+1];
//筛选法找2~N之间的素数 
void primes()
{
 for(int i=2;i<=N;i++) //全部置1 
  a[i]=1;  //用1表示是数,0表示不是素数
 for(int i=2;i<=N;i++)
 {
  if(a[i]) // 如果a[i]是素数 
  {
   for(int j=2*i;j<=N;j=j+i) // a[i]的倍速不是素数 
   a[j]=0;
  }
  } 
}
//找从素整数n开始,满足差值是m的10个连续素数
bool isOK(int n,int  m)  
{
 for(int i=1;i<10;i++)
 {
  if(!a[n]) return false; //如果不是素数,返回false 
  else n=n+m; //否则看差值为m的下一个数是否为素数 
 } 
 return true;//满足条件 
 } 
int main()
{
 primes(); //筛选法找到所有的素数 
 //从差值为2开始找,有没有差值为2的10个连续的素数
 //如果没有,那么看差值是3,有没有差值是3的10个连续素数……
 /*for(int i=2;i<=10000;i++) //输出所有的素数 
 if(a[i])
   cout<<i<<endl;
 */
 int cha=2;
 while(1 ) 
 {
  for(int i=2;i<=N;i++)
  {
   //i是素数,并且差值是cha的10个素数 
   if(a[i]&&isOK(i,cha)) 
   {
    cout<<cha<<endl;
    return 0; 
    } 
  }
  cha++;//差值加1,继续找 
 } 
 return 0;
 }

Receive 300G high-quality programming materials for free icon-default.png?t=N2N8https://jq.qq.com/?_wv=1027&k=s6zhEe1L

Guess you like

Origin blog.csdn.net/yx5666/article/details/129817788