Easy Equation

If you are not sure about the enumeration optimization details, you can write the most violent and simplest one first, and then optimize step by step

Easy Equation

Advanced thinking

①Because k is 0-d, as long as x+y+z is within 0-d, k can be satisfied, so as long as the sum is within 0-d, x, y, and z are valid, and 3 levels of nesting The loop respectively traverses x, y, and z from 0-a, 0-b, 0-c, and the sorting is not repeated.
Preliminary code

#include <iostream>
using namespace std;
int main()
{
    
    
    long long a,b,c,d;
    long long int sum = 0;
    while(cin >> a>> b>>c>>d)
    {
    
    
        sum = 0;
       for(int i =0;i <= a;i++)
       {
    
    
           for(int j =0;j <= b;j++)
           {
    
    
               for(int k = 0;k <= c;k++)
               {
    
    
                   if(i+j+k<=d)
                   {
    
    
                       sum++;
                   }
               }
           }
       }
       cout << sum <<endl;
    }
    system("pause");
    return 0;
}

②The title data is 0-10^6, so it is obviously necessary to optimize to only one for loop.
After finding that x is determined, the value of zx is compared with the value of y+z, and there are different formulas for the number of sequences satisfying zx>=y+z for different comparison results.
In the code below, j=zx;

#include <iostream>
using namespace std;
int main()
{
    
    
    long long int a,b,c,d,j;
    long long int sum = 0;

    while(cin >> a>> b>>c>>d)
    {
    
    
        sum = 0;
       for(long long int i =0;i <= a && i<=d;i++)
       {
    
    
           j = d-i;
           if(j == 0)
                sum++;
           else
           {
    
    
               if(j>=b+c)
                    sum = sum + (b+1)*(c+1);
                else
                {
    
    
                    if(c>=j && b>=j)
                        sum+=(j+2)*(j+1)/2;
                    else if(c>=j && b<j)
                        sum+=(2*j+2-b)*(b+1)/2;
                    else if(b>=j && c < j)
                        sum+=(2*j+2-c)*(c+1)/2;
                    else
                    {
    
    
                        for(int key = 0;key<=b;key++)
                        {
    
    
                            if(j>=key+c)
                                sum+=(c+1);
                            else
                            {
    
    
                                sum+=(j-key+1);
                            }
                            
                        }
                    }
                       
                    /*for(long long int k =0;k <= b && k<=j;k++)
                    {
                        if(k+c<=j)
                            sum+=(c+1);
                        else
                        {
                            sum+=(j-k+1);
                        }
                        
                    }*/
               }   
           }
           
       }
       cout << sum <<endl;
    }
    //system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/RunningBeef/article/details/109409235