0% found this document useful (0 votes)
94 views50 pages

CTDLGT

The document contains code for generating all possible combinations of 0s and 1s of length n. It uses a recursive backtracking approach, trying both 0 and 1 at each index i and recursively calling the function to generate combinations for the remaining indices i+1 to n. If i reaches n, the current combination is printed.

Uploaded by

phanduc280603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views50 pages

CTDLGT

The document contains code for generating all possible combinations of 0s and 1s of length n. It uses a recursive backtracking approach, trying both 0 and 1 at each index i and recursively calling the function to generate combinations for the remaining indices i+1 to n. If i reaches n, the current combination is printed.

Uploaded by

phanduc280603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

//SCTDL001

#include<bits/stdc++.h>
using namespace std;
int arr[10000];
void in(int n){
for( int i = 1; i <= n; i ++ ){
cout << arr[i] << " ";
}
cout << endl;
}
void dequy( int i, int n ){
for( int j = 0; j <= 1; j ++ ){
arr[i] = j;
if( i == n ) in(n);
else{
dequy( i+1, n );
}
}
}
int main(){
int t; cin >> t;
while( t -- ){
int N; cin >> N;
dequy(1,N);
}
}

//SCTDL002
#include<bits/stdc++.h>
using namespace std;

void quaylui(int i, string &aa, int arr[], int dem){


if(i==dem){
cout << aa << endl;
return;
}else{
for(int j=0; j<=1; j++){
aa[arr[i]] = j + '/0';
quaylui(i+1, aa, arr, dem);
}
}
}

int main(){
int t; cin >> t;
cin.ignore();
while(t--){
string s;
getline(cin, s);
stringstream ss(s);
string aa , bb;
while(ss >> bb){
aa+=bb;
}
int arr[100], dem=0;
for(int i=0; i<aa.size(); i++){
if(aa[i]=='?'){
arr[dem]=i;
dem++;
}
}
quaylui(0, aa, arr, dem);
}
}

//SCTDL002
#include<bits/stdc++.h>
using namespace std;
void value( int i, int n, int arr[], string s ){
if( i == n ) {
cout << s << endl;
return;
}
else{
for( int j = 0; j <= 1; j ++ ){
s[arr[i]] = j + '0';
value( i+1, n, arr, s );
}
}
}
int main(){
int t; cin >> t;
cin.ignore();
while( t-- ){
string s; getline(cin,s);
// xoa dau ' '
stringstream ss(s);
string s_new, s_phu;
while( ss >> s_phu ){
s_new += s_phu;
}
int N = 0; // so luong ?
int l = s_new.size();
int arr[100];
for( int i = 0; i < l; i ++ ){
if( s_new[i] == '?' ) arr[N++] = i; // gan gia tri cua mang
bang vi tri dau ? tren strng
}
value( 0, N, arr, s_new );
}
}

//SCTDL004
#include<bits/stdc++.h>
using namespace std;
int arr[10000];
void in(int n){
for( int i = 1; i <= n; i ++ ){
if( arr[i] == 0 ) cout << 'A';
else cout << 'B';
}
cout << " ";
}
void dequy( int i, int n ){
for( int j = 0; j <= 1; j ++ ){
arr[i] = j;
if( i == n ) in(n);
else{
dequy( i+1, n );
}
}
}
int main(){
int t; cin >> t;
while( t -- ){
int N; cin >> N;
dequy(1,N);
cout << endl;
}
}

//SCTDL005
#include<bits/stdc++.h>
using namespace std;
int check[1000] {0};
int arr[1000];
void in(int arr[],int n){
for( int i = 1; i <= n; i ++ ){
cout << arr[i];
}
cout << ' ';
}

void hoanvi( int i, int n ){


for( int j = 1; j <= n; j ++ ){
if( check[j] == 0 ){
check[j] = 1;
arr[i] = j;
if( i == n ) in(arr,n);
else hoanvi( i+1, n );
check[j] = 0;
}
}
}

int main(){
int t; cin >> t;
while( t-- ){
int n; cin >> n;
hoanvi(1,n);
cout << endl;
}
}
//SCTDL008
#include<bits/stdc++.h>
using namespace std;
void magray( string s ){
cout << s[0];
int l = s.size();
for( int i = 0; i < l-1; i ++ ){
if( s[i] == '0' && s[i+1] == '1' || s[i] == '1' && s[i+1] ==
'0' )
cout << "1";
else cout << "0";
}
}
int main(){
int t; cin >> t;
while( t-- ){
string s; cin >> s;
magray(s);
cout << endl;
}
}
//008
#include<bits/stdc++.h>
using namespace std;

int main(){
int t; cin >> t;
while( t-- ){
int n; cin >> n;
int a[n];
for( int i = 0; i < n; i ++ ){
cin >> a[i];
}
while( n > 0 ){
cout << "[";
for( int i = 0; i < n; i ++ ){
if( i == n - 1 ) cout << a[i];
else cout << a[i] << " ";
}
cout << "]" << endl;
for( int i = 0 ; i < n - 1; i ++ ){
a[i] += a[i+1];
}
n--;
}
}
}
//SCTDL003
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
int t; cin >> t;
cin.ignore();
while( t-- ){
cin >> s;
int l = s.size();
for( int i = l - 1; i >= 0; i -- ){
if( s[i] == '1' ) s[i] = '0';
else{
s[i] = '1';
break;
}
}
cout << s << endl;
}
}
//
#include<bits/stdc++.h>

using namespace std;

string s;

void sinh(){
int i = s.size() - 1;
while(i >= 0 && s[i] == '1') i--;
if(i < 0){
for(int j = 0; j < s.size(); j++) s[j] = '0';
}
else{
s[i] = '1';
for(int j = i + 1; j < s.size(); j++) s[j] = '0';
}
}

void in(){
cout << s << endl;
}

int main(){
int t;
cin >> t;
while(t--){
cin >> s;
sinh();
in();
}
}
//
#include<bits/stdc++.h>
using namespace std;
int n, k;
int a[1000];
void in(){
for( int i = 1; i <= k; i ++ ){
cout << a[i];
}
cout <<" ";
}
void tohop( int i ){
for( int j = 1 + a[i-1]; j <= n - k + i; j ++ ){
a[i] = j;
if( i == k ) in();
else tohop( i + 1 );
}
}
int main(){
int t; cin >> t;
while( t-- ){
cin >> n >> k;
a[0] = 0;
tohop(1);
cout << endl;
}
}
//
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
int t; cin >> t;
while( t-- ){
cin >> s;
int l = s.size();
int i = l - 1;
while( s[i] == '1' && i >= 0 ) i--;
if( i < 0 ){
for( int j = 0; j < l; j ++ ){
s[j] = '0';
}
}
else{
s[i] = '1';
for( int j = i + 1; j < l; j ++ ){
s[j] = '0';
}
}
cout << s << endl;
}
}

//SCTDL007
#include<bits/stdc++.h>
using namespace std;

int n;
int check[1000] {0};
int arr[1000];
void in(){
for( int i = 1; i <= n; i ++ ){
cout << arr[i];
} cout << " ";
}

void hoanvinguoc( int i, int n ){


for( int j = n; j >= 1; j-- ){
if( check[j] == 0 ){
check[j] = 1;
arr[i] = j;
if( i == n ) in();
else hoanvinguoc( i + 1, n );
check[j] = 0;
}
}
}

int main(){
int t; cin >> t;
while( t -- ){
cin >> n;
hoanvinguoc(1,n);
cout << endl;
}
}

//SCTDL019
#include<bits/stdc++.h>
using namespace std;
int n, a[1005];

int main(){
int t; cin >> t;
while( t-- ){
int n; cin >> n;
for( int i = 1; i <= n; i++ ) cin >> a[i];
if( next_permutation( a + 1, a + n + 1) ){
for( int i = 1; i <= n; i++ ){
cout << a[i] << " ";
}
}
else{
for( int i = 1; i <= n; i ++ ){
cout << i << " ";
}
}
cout << endl;
}
}

//IDDEMO
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100000] {0};
void solve( int n ){
cout << "[";
for( int i = 0; i < n - 1; i ++ ){
cout << a[i] << " ";
a[i] += a[i+1];
}
cout << a[n-1];
cout << "]" << endl;

if( n == 1 ) return;
else solve(n-1);
}
int main(){
int t; cin >> t;
while( t-- ){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> a[i];
solve(n);
}
return 0;
}

//SCTDL031
#include<bits/stdc++.h>
using namespace std;
int a[10000];
int b[10000];
void solve( int n ){
// chuyen so sang nhi phan
int i = 0;
while( n > 0 ){
a[i++] = n % 2;
n /= 2;
}
b[i-1] = a[i-1];
for( int j = i - 2; j >= 0; j -- ){
if( a[j] == a[j+1] ) b[j] = 0;
else b[j] = 1;
}
int sum = 0;
for( int j = 0; j < i; j ++ ){
sum += b[j] * pow( 2, j );
}
cout << sum << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
int n; cin >> n;
solve(n);
}
return 0;
}

//SCTDL027
#include<bits/stdc++.h>
using namespace std;
void solve( int a[], int n){
int m = INT_MIN;
for( int i = 0; i < n; i ++ ){
int sum = 0;
for( int j = i; j < n; j ++ ){
sum += a[j];
m = max( m , sum );
}
}
cout << m << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
int n; cin >> n;
int a[n];
for( int i = 0; i < n; i ++ ) cin >> a[i];
solve(a,n);
}
}

//SCTDL006
#include<bits/stdc++.h>
using namespace std;

void Try2(int n, int j, int arr[]){


for(int i=arr[j-1]; i>0; i--){
if(n-i==0){
arr[j] = i;
cout << "(";
for(int k=0; k<=j; k++){
cout << arr[k];
}
cout <<")";
}else if(n-i>0){
arr[j] = i;
Try2(n-i, j+1, arr);
}else{
}
}
}

void Try1(int n, int j, int arr[]){


for(int i=n; i>0; i--){
arr[j] = i;
if(n-i==0){
cout << "(" << arr[j] << ")";
}else{
Try2(n-i, j+1, arr);
}
}
}

int main(){
int t; cin >> t;
while(t--){
int n; cin >> n;
int arr[10];
int j=0;
Try1(n, j, arr);
cout << endl;
}
}

//SCTDL012
#include<bits/stdc++.h>
using namespace std;
bool check;
int n,l;
int a[10000];

void dequy( int i ){


for( int j = 0; j <= 1; j ++ ){
a[i] = j;
if( i == l ){
int res = 0;
for( int k = 1; k <= l; k++ ){
res = res * 10 + a[k];
}
res *= 9;
if( res % n == 0 && res >= n ) {
check = true;
cout << res;
break;
}
}
else dequy(i+1);
}
}
int main(){
int t; cin >> t;
while( t-- ){
cin >> n;
check = false;
l = 1;
while( check == false ){
dequy(1);
l++;
}
cout << endl;
}
}

//SCTDL023
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
bool hang[100], cot[100], c_chinh[1000], c_nguoc[1000] ;
// c_ chinh = hang - cot + n
// c_nguoc = hang + cot - 1
void dequy( int i ){
for( int j = 1; j <= n; j++ ){
if( !hang[j] && !cot[j] && !c_chinh[i-j+n] && !c_nguoc[i+j-
1] ){
hang[j] = cot[j] = c_chinh[i-j+n] = c_nguoc[i+j-1] =
true;
if( i == n ) cnt++;
else dequy(i+1);
hang[j] = cot[j] = c_chinh[i-j+n] = c_nguoc[i+j-1] =
false;
}
}
}

int main(){
int t; cin >> t;
while( t -- ){
cin >> n;
cnt = 0;
dequy(1);
cout << cnt << endl;
}
}
//
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
bool hang[100], cot[100], c_chinh[1000], c_nguoc[1000] ;
// c_ chinh = hang - cot + n
// c_nguoc = hang + cot - 1
void dequy( int i ){
for( int j = 1; j <= n; j++ ){
if( !hang[i] && !cot[j] && !c_chinh[i-j+n] && !c_nguoc[i+j-
1] ){
hang[i] = cot[j] = c_chinh[i-j+n] = c_nguoc[i+j-1] =
true;
if( i == n ) cnt++;
else dequy(i+1);
hang[i] = cot[j] = c_chinh[i-j+n] = c_nguoc[i+j-1] =
false;
}
}
}

int main(){
int t; cin >> t;
while( t -- ){
cin >> n;
cnt = 0;
dequy(1);
cout << cnt << endl;
}
}

//SCTDL016
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1e9+7;
int N, K;
ll mt[10][10], mtp[10][10];
void mul_mt( ll x[10][10], ll y[10][10] ){
ll tmp[10][10];
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
ll res = 0;
for( int k = 0; k < N; k++ ){
res += ( x[i][k] * y[k][j] ) % MOD ;
res %= MOD;
}
tmp[i][j] = res;
}
}
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
mt[i][j] = tmp[i][j];
}
}
}
void pow_mt( ll mt[10][10], int K ){
if( K <= 1 ) return;
pow_mt( mt, K/2 );
mul_mt( mt , mt );
if( K % 2 != 0 ) mul_mt( mt, mtp );
}
void solve(){
cin >> N >> K;
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
cin >> mt[i][j];
mtp[i][j] = mt[i][j];
}
}
pow_mt(mt,K);
int sum = 0;
for( int i = 0; i < N; i ++ ){
sum += (mt[i][i]%MOD);
sum %= MOD;
}
cout << sum << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL014
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1e9+7;
int N, K;
ll mt[10][10], mtp[10][10];
void mul_mt( ll x[10][10], ll y[10][10] ){
ll tmp[10][10];
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
ll res = 0;
for( int k = 0; k < N; k++ ){
res += ( x[i][k] * y[k][j] ) % MOD ;
res %= MOD;
}
tmp[i][j] = res;
}
}
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
mt[i][j] = tmp[i][j];
}
}
}
void pow_mt( ll mt[10][10], int K ){
if( K <= 1 ) return;
pow_mt( mt, K/2 );
mul_mt( mt , mt );
if( K % 2 != 0 ) mul_mt( mt, mtp );
}
void solve(){
cin >> N >> K;
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
cin >> mt[i][j];
mtp[i][j] = mt[i][j];
}
}
pow_mt(mt,K);
int sum = 0;
int j = N - 1;
for( int i = 0; i < N; i ++ ){
sum += (mt[i][j--]%MOD);
sum %= MOD;
}
cout << sum << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL021
#include<bits/stdc++.h>
using namespace std;

#define ll long long


const ll MOD = 1e9+7;
ll a, tmp, n, res;

void mul_a( ll x, ll y ){
res = ( ( ( x%MOD ) * ( y % MOD ) ) % MOD );
res %= MOD;
a = res;
}

void pow_an( ll n ){
if( n == 1 ) return;
pow_an(n/2);
mul_a(a,a);
if( n % 2 != 0 ) mul_a(a,tmp);
}

void solve(){
cin >> a >> n;
if( n == 0 ) cout << 1 << endl;
else{
tmp = a;
pow_an(n);
cout << a << endl;
}
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL025
#include<bits/stdc++.h>
using namespace std;

#define ll long long


int a[10000];
ll n;

void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> a[i];
int DLN = INT_MIN, sum = 0;
for( int i = 0; i < n; i ++ ){
sum = max( a[i] , sum + a[i] );
DLN = max( sum, DLN );
}
cout << DLN << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

// SCTDL039
#include<bits/stdc++.h>
using namespace std;

#define ll long long


const ll MOD = 1e9+7;
ll n, a[10000];

void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> a[i];
sort(a,a+n);
ll sum = 0;
for( int i = 0; i < n; i++ ){
sum += ( ( (a[i]%MOD) * (i%MOD) ) % MOD );
sum %= MOD;
}
cout << sum << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL038
#include<bits/stdc++.h>
using namespace std;
string s;

void snn( string s ){


for( int i = 0; i < s.size(); i++ ){
if( s[i] =='5' ) s[i] = '3';
}
int sum = 0;
stringstream ss(s);
string tmp;
while( ss >> tmp ){
sum += stoi(tmp);
}
cout << sum << " ";
}

void sln( string s ){


for( int i = 0; i < s.size(); i++ ){
if( s[i] =='3' ) s[i] = '5';
}
int sum = 0;
stringstream ss(s);
string tmp;
while( ss >> tmp ){
sum += stoi(tmp);
}
cout << sum;
}

void solve(){
getline(cin,s);
snn(s);
sln(s);
cout << endl;
}
int main(){
int t; cin >> t;
cin.ignore();
while( t-- ){
solve();
}
}

//SCTDL037
#include<bits/stdc++.h>
using namespace std;

int N;
int a[] ={ 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 };

void solve(){
cin >> N;
int res = 0;
for( int i = 0; i < 10; i ++ ){
res += N / a[i];
N %= a[i];
}
cout << res << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL040
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
int n, a[10000];
void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> a[i];
sort( a, a + n );
int n1 = 0;
for( int i = 0; i < n; i += 2 ) n1 = n1 * 10 + a[i];
int n2 = 0;
for( int i = 1; i < n; i += 2 ) n2 = n2 * 10 + a[i];
cout << n1 + n2 << endl;
}
int main(){
int t; cin >> t;
while(t--){
solve();
}

//SCTDL042
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
ll n, a[10000], b[10000];
void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> a[i];
for( int i = 0; i < n; i ++ ) cin >> b[i];
sort( a, a + n );
sort( b, b + n, greater<ll>() );
ll res = 0;
for( int i = 0; i < n; i ++ ){
res += a[i] * b[i];
}
cout << res << endl;
}
int main(){
int t; cin >> t;
while(t--){
solve();
}
}

//SCTDL044
#include<bits/stdc++.h>
using namespace std;
int n, a[10000];
int res;
void noiday( int i ){
if( i == n - 1 ) return;
else{
sort( a + i, a + n );
a[i+1] += a[i];
res += a[i+1];
noiday(i+1);
}
}
void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> a[i];
res = 0;
noiday(0);
cout << res << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL060
#include<bits/stdc++.h>
using namespace std;
int n, a[1000];
void solve(){
cin >> n;
int res = 0;
for( int i = 0; i < n; i ++ ){
cin >> a[i];
if( a[i] == 0 ) res ++;
}
cout << res << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL061
#include<bits/stdc++.h>
using namespace std;
string s1, s2;
int n, a[1000][1000];

void ktao(){
for( int i = 0; i < s1.size(); i++ ){
for( int j = 0; j < s2.size(); j ++ ){
a[i][j] = 0;
}
}
}
void xaudainhat(){
int l1 = s1.size(), l2 = s2.size();
for( int i = 1; i <= l1; i++ ){
for( int j = 1; j <= l2; j++ ){
if( s1[i-1] == s2[j-1] ) a[i][j] = a[i-1][j-1] + 1;
else a[i][j] = max( a[i-1][j], a[i][j-1] );
}
}
}
void solve(){
cin >> s1 >> s2;
ktao();
xaudainhat();
cout << a[s1.size()][s2.size()] << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL049
#include<bits/stdc++.h>
using namespace std;
int n, k, a[1000];
void bin_search( int l, int r ){
if( l <= r ){
int m = ( l + r ) / 2;
if( a[m] == k ) cout << m + 1 << endl;
else{
if( a[m] > k ) r = m - 1;
else l = m + 1;
bin_search(l,r);
}
}
else cout << "NO" << endl;
}
void solve(){
cin >> n >> k;
for( int i = 0; i < n; i ++ ) cin >> a[i];
int l = 0, r = n - 1;
bin_search(l,r);

}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL014
#include<bits/stdc++.h>
using namespace std;

#define ll long long


const ll MOD = 1e9+7;
ll n, k, mt_c[10][10], mt_p[10][10];

void mul_mt( ll x[10][10], ll y[10][10] ){


ll tmp[10][10];
for( int i = 0; i < n; i ++ ){
for( int j = 0; j < n; j ++ ){
ll res = 0;
for( int k = 0; k < n; k ++ ){
res += ( ( x[i][k] * y[k][j] ) % MOD );
res %= MOD;
}
tmp[i][j] = res;
}
}

for( int i = 0; i < n; i ++ ){


for( int j = 0; j < n; j ++ ){
mt_c[i][j] = tmp[i][j];
}
}
}

void pow_mt( int k ){


if( k == 1 ) return;
pow_mt( k / 2 );
mul_mt( mt_c, mt_c );
if( k % 2 != 0 ) mul_mt( mt_c, mt_p );
}

void solve(){
cin >> n >> k;
for( int i = 0 ; i < n; i ++ ){
for( int j = 0; j < n; j ++ ) {
cin >> mt_c[i][j];
mt_p[i][j] = mt_c[i][j];
}
}
pow_mt( k );

ll res = 0;
int j = n - 1;
for( int i = 0; i < n; i++ ){
res += mt_c[i][j] % MOD;
res %= MOD;
j--;
}
cout << res << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL043
#include<bits/stdc++.h>
using namespace std;

#define ll long long


int n, cnt;

struct congViec
{
int si; // thoi gian bat dau cong viec
int fi; // thoi gian ket thuc cong viec
};
congViec cv[1000];

bool cmp( congViec x, congViec y ){


return x.fi < y.fi; // sap xep theo thu tu tang dan cua thoi gian
ket thuc
}

void sxcv(){
int i = 0;
for( int j = 1; j < n; j ++ ){
if( cv[j].si >= cv[i].fi ){
cnt++;
i = j;
}
}
}

void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ) cin >> cv[i].si;
for( int i = 0; i < n; i ++ ) cin >> cv[i].fi;
sort( cv, cv + n, cmp );
cnt = 1;
sxcv();
cout << cnt << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL090
#include<bits/stdc++.h>
using namespace std;
stack<char> st;
string s;

bool check( char c ){


if( ( c == '}' && st.top() == '{' ) || ( c == ']' && st.top() == '['
) || ( c == ')' && st.top() == '(' ) ) return true;
return false;
}

void solve(){
cin >> s;
int l = s.size();
int ok = 1;
for( int i = 0; i < l; i ++ ){
if( s[i] == '{' || s[i] == '[' || s[i] == '(' ){
st.push(s[i]);
}
else{
if( check(s[i]) ) st.pop();
else{
ok = 0;
cout << "NO" << endl;
break;
}
}
}
if( ok ){
if(!st.empty() ) cout << "NO" << endl;
else cout << "YES" << endl;
}
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL092
#include<bits/stdc++.h>
using namespace std;

string s,res;
stack<char> st;

int douutien( char c ){


if( c == '*' || c == '/' ) return 3;
else if( c == '+' || c == '-' ) return 2;
return 1;
}

void solve(){
cin >> s;
res = "";
for( int i = 0; i < s.size(); i++ ){
if( isalpha(s[i]) ) res += s[i];
else if( s[i] == '(' ) st.push(s[i]);
else if( s[i] ==')' ){
while( !st.empty() && st.top() != '(' ){
res += st.top();
st.pop();
}
st.pop();
}
else if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] ==
'/' ){
while( !st.empty() && douutien( s[i] ) <=
douutien( st.top() ) ){
res += st.top();
st.pop();
}
st.push(s[i]);
}
}
while( !st.empty() ){
if( st.top() != '(' ) res += st.top();
st.pop();
}
cout << res << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL093
#include<bits/stdc++.h>
using namespace std;
string s, res;
stack<string> st;
void solve(){
cin >> s;
int l = s.size();
for( int i = l - 1; i >= 0; i -- ){
if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' )
{
string a = st.top(); //toan hang thu 1
st.pop();
string b = st.top(); //toan hang thu 2
st.pop();
string tmp = a + b + s[i];
st.push( tmp );
}
else st.push( string(1,s[i]) ); // chuyen tu char sang string
}
cout << st.top() << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL094
#include<bits/stdc++.h>
using namespace std;
string s;
stack<string> st;
void solve(){
cin >> s;
int l = s.size();
for( int i = 0; i < l; i ++ ){
if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'){
string a = st.top(); // toan hang thu 1
st.pop();
string b = st.top(); // toan hang thu 2
st.pop();
string tmp = '(' + b + s[i] + a + ')';
st.push(tmp);
}
else st.push( string(1,s[i]) );
}
cout << st.top() << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL082
#include<bits/stdc++.h>
using namespace std;
int n, a[1000];
stack<int> st;
void solve(){
cin >> n;
for( int i = 0; i < n; i ++ ){
cin >> a[i];
st.push(a[i]);
}
while( !st.empty() ){
cout << st.top() << " ";
st.pop();
}
cout << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL084
#include<bits/stdc++.h>
using namespace std;

int n, k, a[1000], ok;


int res;

void kiemtra(){
res = 0;
for( int i = 1; i <= k; i++ ){
res = res*10 + a[i];
}
res *= 9;
if( res % n == 0 && res >= n ) {
cout << res << endl;
ok = 1;
}
}

void sinh_nhi_phan( int i ){


for( int j = 0; j <= 1; j ++ ){
a[i] = j;
if( i == k ) kiemtra();
else sinh_nhi_phan( i+1 );
}
}

void solve(){
cin >> n;
ok = 0;
k = 1;
while( ok == 0 ){
sinh_nhi_phan(1);
k++;
}
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}
//SCTDL055
#include<bits/stdc++.h>
using namespace std;

#define ll long long


const ll MOD = 1e9+7;
ll n;

void fibo( int n ){


ll f1 = 1, f2 = 1;
if( n == 1 || n == 2 ) cout << 1 << endl;
else{
ll i = 3, res;
while( i <= n ){
res =( f1 % MOD + f2 % MOD ) % MOD ;
f1 = f2;
f2 = res;
i++;
}
cout << res << endl;
}
}

void solve(){
cin >> n;
fibo(n);
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL056
#include<bits/stdc++.h>
using namespace std;

#define ll long long


const ll MOD = 1e9+7;
string s1, s2;

void value(){
ll res1 = 0, res2 = 0;
for( int i = s1.size() - 1; i >= 0; i -- ){
if( s1[i] == '1' ) res1 += pow(2,s1.size() - 1 - i);
}
for( int i = s2.size() - 1; i >= 0; i -- ){
if( s2[i] == '1' ) res2 += pow(2,s2.size() - 1 - i);
}
cout << res1 * res2 << endl;
}

void solve(){
cin >> s1 >> s2;
value();
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL046
#include<bits/stdc++.h>
using namespace std;
int n, s, m;
// n: so luong thuc co the mua trong 1 ngay
// s: so ngay can sinh ton
// m: so luong thuc tieu thu trong 1 ngay
void solve(){
cin >> n >> s >> m;
// vi du: 30 ngay se co 4 tuan ~ 4 ngay chu nhat -> 30 - |30/7| = 30
- 4
if( m * s > ( s - (s/7) ) * n ) cout << -1 << endl;
else{
for( int i = 1; i <= s; i++ ){
if( n * i >= s * m ){
cout << i << endl;
break;
}
}
}
}
int main(){
int t; cin >> t;
while(t--){
solve();
}
}

//SCTDL044
#include<bits/stdc++.h>
using namespace std;

int n;
priority_queue< int, vector<int>, greater<int> > pq;

void solve(){
cin >> n;
for( int i = 0; i < n; i++ ){
int x; cin >> x;
pq.push(x);
}
int val = 0;
while( pq.size() > 1 ){
int res = pq.top();
pq.pop();
res += pq.top();
pq.pop();
val += res;
pq.push(res);
}
cout << val << endl;
pq.pop();
}

int main(){
int t; cin >> t;
while(t--){
solve();
}
}

//SCTDL097
#include<bits/stdc++.h>
using namespace std;

string s;
stack<string> st;

void solve(){
cin >> s;
for( int i = 0; i < s.size(); i ++ ){
if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'
|| s[i] =='%' || s[i] == '^' ){
string a = st.top(); st.pop();
string b = st.top(); st.pop();
string tmp = s[i] + b + a;
st.push(tmp);
}
else st.push( string(1,s[i]) );
}
cout << st.top() << endl;
st.pop();
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL096
#include<bits/stdc++.h>
using namespace std;
stack<string> st;
string s;
void solve(){
cin >> s;
for( int i = s.size(); i >= 0; i-- ){
if( s[i] == '+' || s[i] =='-' || s[i] == '*' || s[i] == '/' ||
s[i] == '%' || s[i] == '^' ){
string a = st.top();
st.pop();
string b = st.top();
st.pop();
string tmp = '(' + a + s[i] + b + ')';
st.push(tmp);
}
else st.push( string(1,s[i]) );
}
cout << st.top() << endl;
st.pop();
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL097
#include<bits/stdc++.h>
using namespace std;
string s;
stack<string> st;

void solve(){
cin >> s;
for( int i = 0; i < s.size(); i ++ ){
if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'
|| s[i] =='%' || s[i] == '^' ){
string a = st.top(); st.pop();
string b = st.top(); st.pop();
string tmp = s[i] + b + a;
st.push(tmp);
}
else st.push( string(1,s[i]) );
}
cout << st.top() << endl;
st.pop();
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

// SCTDL047
#include<bits/stdc++.h>
using namespace std;
int n, res4, res7;
int a, b;
bool th1, th2;
void in(){
for( int i = 1; i <= a; i ++ ) cout << 4;
for( int i = 1; i <= b; i ++ ) cout << 7;
cout << endl;
}
void timso4itnhat(){
b = 0;
int kq1 = ( n - 7 * b ) / 4;
int kq2 = ( n - 7 * b ) % 4;
while( kq1 != 0 && kq2 != 0 ){
b++;
kq1 = ( n - 7 * b ) / 4;
kq2 = ( n - 7 * b ) % 4;
}
a = kq1;
if( 4 * a + 7 * b == n ) in();
else {
th2 = false;
return;
}
}
void timso7itnhat(){
a = 0;
int kq1 = ( n - 4 * a ) / 7;
int kq2 = ( n - 4 * a ) % 7;
while( kq1 != 0 && kq2 != 0 ){
a++;
kq1 = ( n - 4 * a ) / 7;
kq2 = ( n - 4 * a ) % 7;
}
b = kq1;
if( 4 * a + 7 * b == n ) in();
else {
th1 = false;
return;
}
}
void solve(){
cin >> n;
th1 = th2 = true;
timso7itnhat();
if( !th1 ) timso4itnhat();
if( !th1 && !th2 ){
cout << -1 << endl;
}
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL030
#include<bits/stdc++.h>
using namespace std;

int k, idx;

void solve(){
cin >> k;
string s; cin >> s;
int idx = 0;
while( k != 0 && idx != s.size() ){
char sln = s[idx];
int j;
for( int i = idx + 1; i < s.size(); i ++ ){
if( s[i] >= sln ){
sln = s[i];
j = i;
}
}
if( sln != s[idx] ){
swap( s[idx], s[j] );
k --;
}
else{
idx ++;
}
}
cout << s << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL074
#include<bits/stdc++.h>
using namespace std;

int n, m;

void solve(){
int n, m;
cin >> n >> m;
int a[100005] {0};
for( int i = 0; i < n; i ++ ){
int x; cin >> x;
a[x]++;
}
for( int i = 0; i < m; i ++ ){
int x; cin >> x;
a[x] ++;
}
for( int i = 0; i <= 100000; i++ ){
if( a[i] > 0 ) cout << i << " ";
} cout << endl;
for( int i = 0; i <= 100000; i++ ){
if( a[i] > 1 ) cout << i << " ";
} cout << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL041
#include<bits/stdc++.h>
using namespace std;

int v, e;

// v: dinh
// e: canh

void solve(){
int v, e;
cin >> v >> e;
vector<int> vt[v+1];
for( int i = 1; i <= e; i ++ ){
int x, y;
cin >> x >> y;
vt[x].push_back(y);
vt[y].push_back(x);
}
for( int i = 1; i <= v; i ++ ){
sort( vt[i].begin(), vt[i].end() );
}
for( int i = 1; i <= v; i++ ){
cout << "D" << i << ": ";
for( int j : vt[i] ) cout << j << " ";
cout << endl;
}
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL067
#include<bits/stdc++.h>
using namespace std;

int mt_c[501][501], mt_dd[501][501];


int n, m;
// n: hang
// m: cot
void ktao(){
for( int i = 0; i <= n; i++ ){
for( int j = 0; j <= m; j++ ){
mt_c[i][j] = 0;
}
}
}

void nhap_mt(){
for( int i = 1; i <= n; i++ ){
for( int j = 1; j <= m; j++ ){
cin >> mt_c[i][j];
mt_dd[i][j] = mt_c[i][j];
}
}
}

void solve(){
cin >> n >> m;
ktao();
nhap_mt();
int res = 0;
for( int i = 1; i <= n; i++ ){
for( int j = 1; j <= m; j++ ){
if( mt_c[i][j] == 0 ) continue;
else{
if( mt_c[i][j] == mt_c[i-1][j-1] && mt_c[i][j] ==
mt_c[i][j-1] && mt_c[i][j] == mt_c[i-1][j] ){
mt_dd[i][j] = min({mt_dd[i-1][j-1], mt_dd[i-
1][j], mt_dd[i][j-1]}) + 1;
}
res = max( res, mt_dd[i][j] );
}
}
}
cout << res << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL062
#include<bits/stdc++.h>
using namespace std;
int n, k, s, a[100], b[100];
int res, cnt;
// 4 6 8 2 12
void dequy( int i, int x ){
for( int j = x; j < n - k + i; j++ ){
b[i] = a[j];
res += b[i];
x = j;
if( i == k ){
if( res == s ) cnt++;
}
else dequy(i+1,x+1);
res -= b[i];
}
}

void solve(){
cin >> n >> k >> s;
for( int i = 0; i < n; i ++ ) cin >> a[i];
res = 0, cnt = 0;
dequy(1,0);
cout << cnt << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL053
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> v;

void vi_tri_trung_tam( ll n ){
int gt = 2;
v.push_back(gt);
n /= 2;
while( n/2 > 0 ){
gt *= 2;
v.push_back(gt);
n /= 2;
}
}

ll bit( int i, ll n, int p ){


if( i % 2 != 0 ) return 1;
else{
if( i < v[p] ) bit( i, n/2, p-1 );
else if( i == v[p] ) return n%2;
else if( i > v[p] ) bit( v[p] * 2 - i, n/2, p-1 );
}
}

void solve(){
int n, l, r;
cin >> n >> l >> r;
vi_tri_trung_tam(n);
int res = 0;
int p = v.size() - 1;
for( int i = l; i <= r; i++ ){
res += bit( i, n, p );
}
cout << res << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}
//
#include<bits/stdc++.h>
using namespace std;

#define ll long long


vector<ll> v;

void vi_tri_trung_tam( ll n ){
int gt = 2;
v.push_back(gt);
n /= 2;
while( n/2 > 0 ){
gt *= 2;
v.push_back(gt);
n /= 2;
}
}

ll bit( int i, ll n, int idx ){


if( i % 2 != 0 ) return 1;
else{
if( i < v[idx] ) bit( i, n/2, idx - 1 );
else if( i == v[idx] ) return n % 2;
else if( i > v[idx] ) bit( v[idx] * 2 - i, n/2, idx - 1 );
}
}

void solve(){
ll n, l, r;
cin >> n >> l >> r;

vi_tri_trung_tam( n );

int res = 0;
int idx = v.size() - 1;

for( int i = l; i <= r; i ++ ){


res += bit(i,n,idx);
}
cout << res << endl;
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

// DAYSO004
#include<bits/stdc++.h>
using namespace std;
int n, a[100005],b[100005];

void day_con_lon_nhat(){
int m = b[0];
for( int i = 1; i < n; i++ ){
for( int j = 0; j < i; j++ ){
if( a[j] < a[i] ) b[i] = max( b[i], b[j] + 1 );
}
m = max( m, b[i] );
}
cout << m << endl;
}

void solve(){
cin >> n;
for( int i = 0; i < n; i++ ){
cin >> a[i];
b[i] = 1;
}
day_con_lon_nhat();
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//MATRAN001
#include<bits/stdc++.h>
using namespace std;

int n, m, mt[1000][1000], res;


// n: hang
// m: cot

void nhap_ma_tran(){
for( int i = 0; i < n; i++ ){
for( int j = 0; j < m; j++ ){
cin >> mt[i][j];
}
}
}

void duong_di_dai_nhat(){
if( n == 1 ){
res = INT_MIN;
for( int i = 0; i < m; i++ ){
res = max( res, mt[0][i] );
}
cout << res << endl;
}
else if( m == 1 ){
res = 0;
for( int i = 0; i < n; i++ ){
res += mt[i][0];
}
cout << res << endl;
}
else{
for( int i = 1; i < n; i++ ){
for( int j = 0; j < m; j++ ){
if( j == 0 ) mt[i][j] += max( mt[i-1][j], mt[i-1]
[j+1] );
else if( j == m - 1 ) mt[i][j] += max( mt[i-1][j-
1], mt[i-1][j] );
else mt[i][j] += max( { mt[i-1][j-1], mt[i-1][j],
mt[i-1][j+1] } );
}
}
res = INT_MIN;
for( int i = 0; i < m; i++ ){
res = max( res, mt[n-1][i] );
}
cout << res << endl;
}
}

void solve(){
cin >> n >> m;
nhap_ma_tran();
duong_di_dai_nhat();
}

int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}

//SCTDL022
#include<bits/stdc++.h>
using namespace std;
int m, sl, res, a[10005];
void khoi_tao(){
a[0] = 1; a[1] = 2;
int i = 1;
while( a[i] < m ){
i++;
a[i] = a[i-1] + a[i-2];
} sl = i;
}
void tong_bang_m( int m, int i ){
for( int j = i; j <= sl; j++ ){
int x = m - a[j];
if( x == 0 ) res++;
else if( x < 0 ) return;
else tong_bang_m( x, j+1 );
}
}
void solve(){
cin >> m;
khoi_tao();
res = 0;
tong_bang_m(m,0);
cout << res << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}
//
#include<bits/stdc++.h>
using namespace std;
//b20
void Try(int arr[], int m, int dem, int j, int& res){
for(int i=j; i<=dem; i++){
int x = m-arr[i];
if(x==0){
res++;
}else if(x<0){
return;
}else{
Try(arr, x, dem, i+1, res);
}
}
}

int main(){
int t; cin >> t;
while(t--){
int m, n;
cin >> m >> n;
int arr[50];
int dem=1;
arr[dem] = 1;
while(arr[dem] < m){
dem++;
arr[dem] = pow(dem, n);
}
int res = 0;
Try(arr, m, dem, 1, res);
cout << res << endl;
}
}
//
#include<bits/stdc++.h>
using namespace std;
int m, n, sl, res, a[10005];
void khoi_tao(){
int i = 1;
a[i] = 1;
while( a[i] < m ){
i++;
a[i] = pow( i, n );
} sl = i;
}
void tong_luy_thua( int m, int i ){
for( int j = i; j <= sl; j++ ){
int x = m - a[j];
if( x == 0 ) res++;
else if( x < 0 ) return;
else tong_luy_thua( x, j+1 );
}
}
void solve(){
cin >> m >> n;
khoi_tao();
res = 0;
tong_luy_thua(m,1);
cout << res << endl;
}
int main(){
int t; cin >> t;
while( t-- ){
solve();
}
}
//
#include<bits/stdc++.h>
using namespace std;

void Try(int arr[], int m, int dem, int j, int& res){


for(int i=j; i<=dem; i++){
int x = m-arr[i];
if(x==0){
res++;
}else if(x<0){
return;
}else{
Try(arr, x, dem, i+1, res);
}
}
}

int main(){
int t; cin >> t;
while(t--){
int m, n;
cin >> m >> n;
int arr[50];
int dem=1;
arr[dem] = 1;
while(arr[dem] < m){
dem++;
arr[dem] = pow(dem, n);
}
int res = 0;
Try(arr, m, dem, 1, res);
cout << res << endl;
}
}

//SCTDL105.106
#include<bits/stdc++.h>

using namespace std;


vector <int> ke[1005];

bool ok[1005];

void DFS(int u){

ok[u]= true;

cout << u << ' ';

for(int i = 0; i< ke[u].size(); i++){

if (!ok[ke[u][i]]){

DFS(ke[u][i]);

int main(){

ios_base::sync_with_stdio(0); cin.tie(0);

int t;

cin >> t;

while( t-- ){

for(int i = 0; i<1001 ; i++)

ke[i].clear();

memset (ok, false, sizeof(ok));

int a, b, nguon;

cin >> a >> b>> nguon;

for(int i= 0; i<b; i++){

int u,v; // canh u,v

cin >> u>>v;


ke[u].push_back(v);

ke[v].push_back(u);

DFS(nguon);

cout << '\n';

return 0;

//BFS

#include<bits/stdc++.h>// hang doi

using namespace std;

vector <int> ke[1005];

bool ok[1005];

void BFS(int u){

queue<int> q;

q.push(u);

while(q.size()>0){

int top=q.front(); q.pop();

cout << top << ' ';

ok[top]= true;

for(int i = 0; i< ke[top].size(); i++){

if (!ok[ke[top][i]]){

ok[ke[top][i]]=true;

q.push(ke[top][i]);

}
}

int main(){

ios_base::sync_with_stdio(0); cin.tie(0);//loai bo dong bo hoa giua xuat/nhap->tang toc do doc


vaghi.Khi mot ham nhap or xuat duojc goi se thuc thi ngay kp cho gl khac,tang toc do nhap/xuat

int t;

cin >> t;

while( t-- ){

for(int i = 0; i<1001 ; i++)

ke[i].clear();

memset (ok, false, sizeof(ok));

int a, b, nguon;

cin >> a >> b>> nguon;

for(int i= 0; i<b; i++){

int u,v; // canh u,v

cin >> u>>v;

ke[u].push_back(v);

ke[v].push_back(u);

BFS(nguon);

cout << '\n';

return 0;
}

You might also like