Positive integer n split into several different number of squares --DFS && play table

Consider a positive integer n into several different numbers of squares of, for example 30 = 1 + 2 ^ 2 ^ 2 ^ 2 = 1 + 5 2 + 3 + 2 ^ 2 ^ 2 + 2 ^ 4, and 8 such resolution does not exist.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 1000 + 10;
 5 bool vis[maxn];
 6 vector<int>res;
 7 
 8 bool dfs(int n)
 9 {
10     //printf("%d\n", n);
11     if(n == 0)  return  true;
12     for(int i = 1; i * i <= n;i++)
 13 is      {
 14          IF (! VIS [I])
 15          {
 16              VIS [I] = to true ;
 . 17              res.push_back (I);
 18 is              IF (DFS (n-- I * I))   return  to true ;   // a satisfies the decomposition conditions to return 
. 19              VIS [I] = to false ;
 20 is              res.pop_back ();
 21 is          }
 22 is      }
 23 is      return  to false ;
 24  }
 25  
26 is  int main ()
 27  {
28     int cnt = 0;
29     for(int i = 1; i < 1000;i++)
30     {
31         memset(vis, 0, sizeof(vis));
32         if(!dfs(i))  printf("%d  %d\n",++cnt, i);
33     }
34 
35     return 0;
36 }

Only the number 31 can not break down: 2,3,6,7,8,11,12,15,18,19, ..., 108,112,128, in 10 ^ 18 is only 31, there is no greater validation.

Topic Link

Guess you like

Origin www.cnblogs.com/lfri/p/11140578.html