답안 #524432

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524432 2022-02-09T08:14:57 Z EliteCallsYou 아름다운 순열 (IZhO12_beauty) C++17
0 / 100
3000 ms 296 KB
# include <bits/stdc++.h>
using namespace std ;
//############################################################################
# define read(a)/**/for(int i=0;i<int(sizeof(a)/sizeof(a[0]));i++){cin>>a[i];}
# define print(x)/**/for(auto zx:x){cout<<zx<<' ';}
const char* tochar(string a){return a.c_str();}
void files(){freopen("mootube.in","r",stdin);freopen("mootube.out","w",stdout);}
void speed(){ios_base::sync_with_stdio(false);cin.tie(0);}
int gcd(int a,int b){if(!b){return a;}a%=b;return gcd(b,a);}
int lcm(int a,int b){return a /gcd(a,b) * b;}
//###########################################################################
const int N = 2501 ;
const int MOD = 1e9+7 ;
//###########################################################################
void hack(){}
//###########################################################################

int ans, n, ar[21] ;
bool used[21] ;

void rec ( int pos, int last ){
	if ( pos == n ){
		ans ++ ;
		return ;
	}
	for ( int i = 0 ; i < n ; i ++ ){
		if ( used[i] ){continue;}
		if ( last == -1 ){
			used[i] = true ;
			rec(pos+1,ar[i]) ;
			used[i] = false ;
		}
		else{
			int a = 0, b = 0 ;
			int cur_a = ar[i], cur_b = last ;
			while ( true ){
				if ( !cur_a && !cur_b ){
					break ;
				}
				if ( cur_a ){
					if ( cur_a % 2 == 1 ){
						a ++ ;
					}
					cur_a /= 2 ;
				}
				if ( cur_b ){
					if ( cur_b % 2 == 1 ){
						b ++ ;
					}
					cur_b /= 2 ;
				}
			}
			if ( a == b ){
				used[i] = true ;
				rec(pos+1,ar[i]) ;
				used[i] = false ;
				continue ;
			}
			///////////////////////
			a = b = 0 ;
			cur_a = ar[i], cur_b = last ;

			while ( true ){
				if ( !cur_a && !cur_b ){
					break ;
				}
				if ( cur_a ){
					if ( cur_a % 3 == 1 ){
						a ++ ;
					}
					cur_a /= 3 ;
				}
				if ( cur_b ){
					if ( cur_b % 3 == 1 ){
						b ++ ;
					}
					cur_b /= 3 ;
				}
			}
			if ( a == b ){
				used[i] = true ;
				rec(pos+1,ar[i]) ;
				used[i] = false ;
			}
			///////////////////////
		}
	}
}

int main(){
	cin >> n ;
	for ( int i = 0 ; i < n ; i ++ ){
		cin >> ar[i] ;
	}
	rec(0,-1) ;
	cout << ans << '\n' ;
}

Compilation message

beauty.cpp: In function 'void files()':
beauty.cpp:7:21: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | void files(){freopen("mootube.in","r",stdin);freopen("mootube.out","w",stdout);}
      |              ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp:7:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | void files(){freopen("mootube.in","r",stdin);freopen("mootube.out","w",stdout);}
      |                                              ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 288 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 8 ms 296 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 458 ms 276 KB Output is correct
9 Correct 4 ms 204 KB Output is correct
10 Correct 620 ms 272 KB Output is correct
11 Execution timed out 3067 ms 204 KB Time limit exceeded
12 Halted 0 ms 0 KB -