Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author : unlucky_13
- Problem_link :
- Category :
- Algorithm_Used : Backtracking
- */
- #include<cstdio>
- #include<sstream>
- #include<cstdlib>
- #include<cctype>
- #include<cmath>
- #include<algorithm>
- #include<set>
- #include<queue>
- #include<stack>
- #include<list>
- #include<iostream>
- #include<fstream>
- #include<numeric>
- #include<string>
- #include<vector>
- #include<cstring>
- #include<map>
- #include<iterator>
- #define LL long long int
- //const long long int inf = 2147483647 ;
- //const int minx=;
- //const int maxn=;
- using namespace std;
- int res ,peg;
- int v[111] ;
- bool is_perfect(const int& x) {
- int y = (int) sqrt(x * 1.0);
- return y * y == x;
- }
- void backtrack(int n){
- bool flag = false ;
- int i = 1 ;
- while(!flag && i<=peg)
- {
- if(v[i]==-1 || is_perfect(v[i]+n)) flag = true ;
- else i++ ;
- }
- if(flag==false) res = max(res,n-1) ; //no move was possible
- else {
- v[i]=n ;
- backtrack(n+1) ;
- }
- }
- int main() {
- freopen("C:\\Users\\Mazhar\\Desktop\\Text_Files\\in.txt", "r", stdin);
- int tc ;
- scanf("%d",&tc) ;
- while(tc--)
- {
- res = 0 ;
- scanf("%d",&peg) ;
- for(int i=1;i<=peg;i++) v[i]=-1 ;
- backtrack(1) ;
- printf("%d\n",res) ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment