Question:
Answer to this question: 2430
Problem solving ideas:
- First programming:
given by the title n n The value of n is 16 16 16 digits, C C There are types in C language l o n g long long l o n g long long i n t int int, whose range is − 9223372036854775808 -9223372036854775808 −9223372036854775808~ 9223372036854775807 9223372036854775807 9223372036854775807 is 19 19 19 bit values can be stored completely n n n; Observe the example given by the topic: no de reprocessing is required. That is, although the factors are the same, they are located in different positions, belonging to two different schemes. Write a program to calculate n n For all factors of n, it is found that the number of factors is 128 128 128 can simply use the violent method (triple cycle) when it meets the requirements " n = "n= "n = factor 1 ∗ * * factor 2 ∗ * * factor 3 " " ”Statistical variable s u m sum sum plus one. The code is as follows:
#include<stdio.h> #include<math.h> #define n 2021041820210418 long long int inshu[1000]; int sum = 0; int main(){ long long int geshu; int i,j,z; geshu = factor(n); for(i=0;i<geshu;i++){ for(j=0;j<geshu;j++){ for(z=0;z<geshu;z++){ if(inshu[i] * inshu[j] *inshu[z] == n){ sum++; //printf("%lld * %lld *%lld = %lld %lld \n",inshu[i],inshu[j],inshu[z],n,sum);// Detection code } } } } printf("%d\n",sum); return 0; } int factor(long long int N){ //Find the function of all factors of N; long long int i,k; k = 0; for(i=1; i<=sqrt(N); i++){ //I found half the factor here if(N%i == 0){ inshu[k++] = i; } } if(sqrt(N) == inshu[k-1]){ i = k-1; } //When this number is the square term of a number, you need to subtract a number from the factor to avoid repetition of the factor else{ i = k; } while(--i >= 0){ //Make up the other half of the factor inshu[k++] = N / inshu[i]; } return k; }
- Second programming (first optimization):
the time complexity of the violence solution method is O ( n 3 ) O(n^3) O(n3), resulting in time consumption. Optimize the previous algorithm by calling f a c t o r ( ) factor() factor() function, find out all the factors of n, and put n n n split into " n = "n= "n = factor 1 ∗ * * factor 2 ∗ * * factor 3 " " ”Formula of; Called on factor 2 f a c t o r ( ) factor() factor() finds all the factors of factor 2 and splits factor 2 into " " "Factor 2 = = =Factor 21 ∗ * * factor 22 " " ”Formula of; By combining the two equations, we can get " n = "n= "n = factor 1 ∗ * * factor 2 ∗ * * factor 3 " " ””, we can get the answer by counting the number of formulas. code:
#include<stdio.h> #include<math.h> #define n 2021041820210418 int sum = 0; //Variable of the number of statistical formulas long long int *factor(long long int N,long long int inshu[1000]); int main(){ long long int yinshu[1000] = ; //Store all factors of n; int i,j; factor(n,yinshu); for(i=0; i<yinshu[999]; i++){ long long int zinshu[1000] = ; //A factor array that stores all factors factor(n/yinshu[i],zinshu); for(j=0;j<zinshu[999]; j++){ sum++; //printf("%lld * %lld * %lld = %lld %d \n",yinshu[i],zinshu[j],n/yinshu[i]/zinshu[j],n,sum); // Detection code } } printf("%d\n",sum); return 0; } long long int *factor(long long int N,long long int inshu[1000]){ long long int i,k; k = 0; for(i=1;i<=sqrt(N);i++){ //Half the factors are found here if(N%i == 0){ inshu[k++] = i; } } if(sqrt(N) == inshu[k-1]){ i = k-1; } //When this number is the square of a number, you need to subtract a number from the factor else{ i = k; } while(--i >= 0){ //Make up the other half of the factor inshu[k++] = N / inshu[i]; } inshu[999] = k; //Before calculation, run the program and find that there are no more than 900 n factors. It can be seen that k is the number of N factors, and store it with the last value of inshu array in order to pass it back to the main function. return inshu; }
- Third programming (second optimization)
through in-depth analysis and comparison of the above two algorithms, the above algorithms can be further simplified.
optimization process: since no weight removal is required, it is assumed that f 1 ∗ f 2 ∗ f 3 = n f1 * f2 * f3 = n f1 * f2 * f3=n, according to this formula, another five formulas can be obtained directly:“ f 1 ∗ f 3 ∗ f 2 = n f1 * f3 * f2 = n f1∗f3∗f2=n "," f 2 ∗ f 1 ∗ f 3 = n f2 * f1 * f3 = n f2∗f1∗f3=n"," f 2 ∗ f 3 ∗ f 1 = n f2 * f3 * f1 = n f2∗f3∗f1=n"," f 3 ∗ f 1 ∗ f 2 = n f3 * f1 * f2 = n f3 * f1 * f2=n "and“ f 3 ∗ f 2 ∗ f 1 = n f3 * f2 * f1 = n f3∗f2∗f1=n".
note:
(1) when f 1 = f 2 = f 3 f1 =f2 = f3 When f1=f2=f3, f 1 ∗ f 2 ∗ f 3 = n f1 * f2 *f3 = n f1 * f2 * f3=n can only be calculated as one formula;
(2) when f 1 = f 2 f1 = f2 f1=f2 or f 1 = f 3 f1=f3 f1=f3 or f 2 = f 3 f2=f3 f2=f3, f 1 ∗ f 2 ∗ f 3 = n f1 * f2 * f3 = n f1 * f2 * f3=n after changing the position, three formulas are obtained;
(3) when f 1 ≠ f 2 ≠ f 3 f1 \neq f2 \neq f3 When f1 = f2 = f3, f 1 ∗ f 2 ∗ f 3 = n f1 * f2 * f3 = n f1 * f2 * f3=n, which can be calculated as six different formulas;
(4) in order to avoid double calculation, it is necessary to ensure f 1 ≤ f 2 ≤ f 3 f1 \le f2 \le f3 f1≤f2≤f3;
the above two algorithms use functions and arrays to calculate and store factors, which consumes a lot of storage space. The process of calculating factors is particularly simple, that is, taking this number( n n n) Divide by a number less than it( i i i) , if it can be divided( n m o d i = 0 n \bmod i = 0 nmodi=0), then i i i is its first factor, n / i n/i n/i is its second factor( j j j) It can be realized with one cycle, and the cycle condition is set( i ≤ s q r t ( n ) i \le sqrt(n) i ≤ sqrt(n)), which ensures that the first factor is less than the second factor; then use the same cycle for the second factor to find the factor of the second factor. The third factor is n / i / j n/i/j n/i/j.
Specific code:
#include<stdio.h> #include<math.h> #define n 2021041820210418 int main(){ long long int i,j; int sum = 0; //Variable of the number of statistical formulas for(i=1;i<=sqrt(n);i++){ if(n%i == 0){ //i is the first factor of n for(j=i;j<=sqrt(n/i);j++){ //j cycles from i, ensuring i < = j if(n/i%j == 0){ //j is the second factor of n if(i==j && j==n/i/j) else if(i==j || j==n/i/j || i==n/i/j) else //Printf ("% LLD% LLD% LLD% d \ n", I, J, N / I / J, sum); / / detection code } } } } printf("%d",sum); }