0% found this document useful (0 votes)
242 views

Code Ptit

The document contains code snippets for generating combinations, permutations, binary strings and other sequences in C++. Specifically: - It includes code for generating the next permutation, combination or subset by incrementing the elements in order. - Other snippets show how to generate all binary strings of length N with K 1's, all strings of length N over an alphabet like {'A','B'}, and sequences satisfying other constraints. - Recursive backtracking approaches are used to enumerate all possible solutions by trying all choices at each step. So in summary, the document provides algorithms and code implementations for common enumeration and generation problems involving sequences and discrete structures.

Uploaded by

đinh trung
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views

Code Ptit

The document contains code snippets for generating combinations, permutations, binary strings and other sequences in C++. Specifically: - It includes code for generating the next permutation, combination or subset by incrementing the elements in order. - Other snippets show how to generate all binary strings of length N with K 1's, all strings of length N over an alphabet like {'A','B'}, and sequences satisfying other constraints. - Recursive backtracking approaches are used to enumerate all possible solutions by trying all choices at each step. So in summary, the document provides algorithms and code implementations for common enumeration and generation problems involving sequences and discrete structures.

Uploaded by

đinh trung
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Bài 1:

Bài 2: Thuật toán sinh

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int a[1000],n,ok;

void in(){

fort1(i,1,n) cout << a[i] << " ";

cout << endl;

void kt(){

fort1(i,1,n) a[i]=0;

void sinh(){

int i=n;

while(i>=1&&a[i]==1){

a[i]=0;

--i;

}if(i==0) ok=0;

else a[i]=1;

int check(){

int l=1,r=n;

while(l<r){

if(a[l]!=a[r]) return 0;

l++;r--;

}return 1;
}

int main(){

cin >> n;

kt();

ok=1;

while(ok){

if(check()) in();

sinh();

Bài 3: Tổng dãy con = K

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int a[1000],n,ok,k,b[1000],cnt=0;

void in(){

fort1(i,1,n) {

if(a[i]) cout << b[i] << " ";

cout << endl;

void kt(){

fort1(i,1,n) a[i]=0;

void nhap(){

fort1(i,1,n) cin >> b[i];


}

void sinh(){

int i=n;

while(i>=1&&a[i]==1){

a[i]=0;

--i;

}if(i==0) ok=0;

else a[i]=1;

int check(){

int sum=0;

fort1(i,1,n){

sum+=a[i]*b[i];

}if(sum!=k) return 0;

return 1;

int main(){

cin >> n >> k;

kt();

nhap();

ok=1;

while(ok){

if(check()) {

in();

++cnt;

sinh();

}cout << cnt;

}
Bài 6: Xâu nhị phân kế tiếp

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int main(){

int t;

cin >> t;

while(t--){

string s;

cin >> s;

forg1(i,s.length()-1,0){

if(s[i]=='1') s[i]='0';

else if(s[i]=='0'){

s[i]='1';

break;

}cout << s << endl;

Bài 7: Tập con kế tiếp

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)


#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int a[1000],n,k;

void sinh(){

int i=k;

while(i>=1&&a[i]==n-k+i) --i;

if(i==0){

fort1(j,1,k) a[j]=j;

else {

++a[i];

fort1(j,i+1,k) a[j]=a[j-1]+1;

int main(){

int t;

cin >> t;

while(t--){

cin >> n >> k;

fort1(i,1,k) cin >> a[i];

sinh();

fort1(i,1,k) cout << a[i] << " ";

cout << endl;

Bài 8: Hoán vị kế tiếp

#include <bits/stdc++.h>

using namespace std;


typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int a[1000],n,t;

int main(){

cin >> t;

while(t--){

cin >> n;

fort(i,0,n) cin >> a[i];

next_permutation(a,a+n);

fort(i,0,n) cout << a[i] << " ";

cout << endl;

Bài 9: Sinh tổ hợp

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

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

void in(){

fort1(i,1,k) cout << a[i];

cout << " ";

void Try(int i){


fort1(j,a[i-1]+1,n-k+i){

a[i]=j;

if(i==k) in();

else Try(i+1);

int main(){

cin >> t;

a[0]=0;

while(t--){

cin >> n >> k;

Try(1);

cout << endl;

Bài 10: Sinh hoán vị

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int a[1000],n,t,check[1000];

void in(){

fort1(i,1,n) cout << a[i];

cout << " ";

void kt(){

fort1(i,1,n) check[i]=1;
}

void Try(int i){

fort1(j,1,n){

if(check[j]){

a[i]=j;

check[j]=0;

if(i==n) in();

else Try(i+1);

check[j]=1;

int main(){

cin >> t;

while(t--){

cin >> n ;

kt();

Try(1);

cout << endl;

Bài 11: Hoán vị ngược

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)


int a[1000],n,t,check[1000];

void in(){

fort1(i,1,n) cout << a[i];

cout << " ";

void kt(){

fort1(i,1,n) check[i]=1;

void Try(int i){

forg1(j,n,1){

if(check[j]){

a[i]=j;

check[j]=0;

if(i==n) in();

else Try(i+1);

check[j]=1;

int main(){

cin >> t;

while(t--){

cin >> n ;

kt();

Try(1);

cout << endl;

}
Bài 12: Xâu AB có độ dài N

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

char a[1000];

int n,t;

void in(){

fort1(i,1,n) cout << a[i];

cout << " ";

void Try(int i){

fort1(j,'A','B'){

a[i]=j;

if(i==n) in();

else Try(i+1);

int main(){

cin >> t;

while(t--){

cin >> n;

Try(1);

cout << endl;

}
Bài 13: Xâu nhị phân có K bit 1

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

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

void in(){

fort1(i,1,n) cout << a[i];

cout << endl;

int check(){

int cnt=0;

fort1(i,1,n) {

if(a[i]==1) ++cnt;

}if(cnt==k) return 1;

return 0;

void Try(int i){

fort1(j,0,1){

a[i]=j;

if(i==n) {

if(check()) in();

}
else Try(i+1);

int main(){

cin >> t;

while(t--){

cin >> n >> k;

Try(1);

Câu 14: HAHAHA

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int n,t;

char s[2]={'A','H'},a[1000];

void in(){

fort1(i,1,n) cout << a[i];

cout << endl;

int check(){
if(a[1]!='H'||a[n]!='A') return 0;

fort1(i,1,n) {

if(a[i]=='H'&&a[i+1]=='H') return 0;

}return 1;

void Try(int i){

fort1(j,0,1){

a[i]=s[j];

if(i==n) {

if(check()) in();

else Try(i+1);

int main(){

cin >> t;

while(t--){

cin >> n;

Try(1);

Câu 15: Xâu nhị phân trước

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)


int t;

string s;

void sinh(){

forg1(i,s.length()-1,0){

if(s[i]=='1') {

s[i]='0';

break;

}else if(s[i]=='0') s[i]='1';

int main(){

cin >> t;

while(t--){

cin >> s;

sinh();

cout << s << endl;

Câu 16: Tổ hợp tiếp theo

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int n,k,t,a[1000],cnt,b[1000];

void nhap(){

fort1(i,1,k) {
cin >> a[i];

b[i]=a[i];

void sinh(){

int i=k;

while(i>=1&&a[i]==n-k+i) --i;

if(i!=0) {

++a[i];

fort1(j,i+1,n) a[j]=a[j-1]+1;

void cmp(){

fort1(i,1,k){

fort1(j,1,k){

if(a[i]==b[j]) --cnt;

int main(){

cin >> t;

while(t--){

cin >> n >> k;

nhap();

cnt=k;

sinh();

cmp();

if(cnt) cout << cnt << endl;

else cout << k << endl;

}
Câu 17: Số thứ tự hoán vị

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

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

void ktao(){

fort1(i,1,n) a[i]=i;

int so(){

fort1(i,1,n){

if(a[i]!=b[i]) return 0;

}return 1;

void sinh(){

int i=n-1;

while(i>=1&&a[i]>a[i+1]) --i;

if(i==0) ok=0;

else {

int j=n;

while(a[j]<a[i]) --j;

swap(a[i],a[j]);

int l=i+1,r=n;

while(l<r){

swap(a[l],a[r]);
++l,--r;

int main(){

int t;

cin >> t ;

while(t--){

cin >> n;

fort1(i,1,n) cin >> b[i];

ok=1;

ll idx=1;

ktao();

while(ok&&so()==0){

++idx;

sinh();

}cout << idx << endl;

Câu 18: Số thứ tự tổ hợp

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

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

void ktao(){ fort1(i,1,k) a[i]=i;


}

int so(){

fort1(i,1,k){

if(a[i]!=b[i]) return 0;

}return 1;

void sinh(){

int i=k;

while(i>=1&&a[i]==n-k+i) --i;

if(i==0) ok=0;

else {

++a[i];

fort1(j,i+1,k) a[j]=a[j-1]+1;

int main(){

int t;

cin >> t ;

while(t--){

cin >> n >> k;

fort1(i,1,k) cin >> b[i];

ok=1;

ll idx=1;

ktao();

while(ok&&so()==0){

++idx;

sinh();

}cout << idx << endl;

}
Câu 19: Đặt tên _ 1

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

#define t() int t; cin >> t; while(t--)

#define faster() cin.tie(0); ios_base::sync_with_stdio(false); cout.tie(0);

int n,k;

string a[35];

int b[35];

set<string> s;

void nhap(){

fort1(i,1,n) {

string x;

cin >> x;

s.insert(x);

void in(){

fort1(i,1,k) cout << a[b[i]]<< " ";

cout << endl;

void Try(int i){

fort1(j,b[i-1]+1,n-k+i){

b[i]=j;
if(i==k) in();

else Try(i+1);

int main(){

faster();

cin >> n >> k;

nhap();

int m=1;

for(auto x : s) {

a[m++] = x;

n=s.size();

b[0]=0;

Try(1);

Câu 20: Đặt tên - 2

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

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

void in(){
fort1(i,1,k) cout << (char)(a[i]+64);

cout << endl;

void kt(){

fort1(i,1,k) a[i]=i;

void sinh(){

int i=k;

while(a[i]==n-k+i&&i>=1) --i;

if(i==0) ok=0;

else {

++a[i];

fort1(j,i+1,k) a[j]=a[j-1] + 1;

int main(){

cin >> t;

while(t--){

cin >> n >> k;

ok=1;

kt();

while(ok){

in();

sinh();

Câu 21: Phát lộc

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

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int n;

char s[2]={'6','8'},a[1000];

void in(){

fort1(i,1,n) cout << a[i];

cout << endl;

int check(){

if(a[1]!='8'||a[n]!='6') return 0;

fort1(i,1,n) {

if(a[i]=='8'&&a[i+1]=='8') return 0;

if(a[i]=='6'&&a[i+1]=='6'&&a[i+2]=='6'&&a[i+3]=='6') return 0;

return 1;

void Try(int i){

fort1(j,0,1){

a[i]=s[j];

if(i==n) {

if(check()) in();

else Try(i+1);

}
}

int main(){

cin >> n;

Try(1);

Câu 22: Hoán vị dãy số

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

int n,a[10001],check[1000];

int b[10001];

void in(){

fort1(i,1,n) cout << b[a[i]] << " ";

cout << endl;

void ktao(){

fort1(i,1,n) check[i]=1;

void Try(int i){

fort1(j,1,n){

if(check[j]){

a[i]=j;

check[j]=0;
if(i==n) in();

else Try(i+1);

check[j]=1;

int main(){

cin >> n;

fort1(i,1,n) cin >> b[i];

sort(b+1,b+n+1);

ktao();

Try(1);

Câu 23: Liệt kê tổ hợp

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

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

set<int> s;

void ktao(){

fort1(i,1,k) a[i]=i;

void sinh(){

int i=k;
while(i>=1&&a[i]==n-k+i) --i;

if(i==0) ok=0;

else {

++a[i];

fort1(j,i+1,k) a[j]=a[j-1]+1;

void in(){

fort1(i,1,k) cout << b[a[i]] << " ";

cout << endl;

int main(){

cin >> n >> k;

fort1(i,1,n){

int x;

cin >> x;

s.insert(x);

}n=s.size();l=1;

for(int x : s) {

b[l++]=x;

ok=1;

ktao();

while(ok){

in();

sinh();

}
Câu 24: Dãy số 1

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fort(i,a,b) for(int i=a;i<b;++i)

#define forg(i,a,b) for(int i=a;i>b;--i)

#define fort1(i,a,b) for(int i=a;i<=b;++i)

#define forg1(i,a,b) for(int i=a;i>=b;--i)

#define t() int t; cin >> t; while(t--)

#define faster() cin.tie(0); ios_base::sync_with_stdio(false); cout.tie(0);

void ql(int a[], int n)

if (n>0) {

cout << "[";

fort(i,0,n-1)

cout << a[i] << " ";

cout << a[n-1] << "]" << endl;

fort(i,0,n-1)

a[i]=a[i]+a[i+1];

ql(a,n-1);

int main(){

faster();

t(){

int n;

cin >> n;
int a[n];

fort(i,0,n) cin >> a[i];

ql(a,n);

You might also like