3 条题解
-
7
乱搞题依旧 tzq 专长。
题解真是拉完了,我们直接考虑打表。可以写出:
#include <bits/stdc++.h> using namespace std; #define int long long const int N=105; int n,vis[N],a[N],b[N],anss=0; void work() { int ans1=0; memset(b,0,sizeof b); for(int i=1; i<=n; i++) { b[a[i]]=1; for(int j=a[i]+1; j<=n; j++)ans1+=b[j]; } if(ans1!=n-1)return; for(int k=1; k<n; k++) { for(int i=1; i<=k; i++)b[i]=a[i]; sort(b+1,b+k+1); int ans1=0; for(int i=1; i<=k; i++)ans1+=(b[i]==i); if(ans1==k)return; } for(int i=1; i<=n; i++)cout<<a[i]<<' '; cout<<'\n'; for(int i=n-3; i>=0; i--)cout<<((anss>>i)&1)<<' '; cout<<'\n'; anss++; } void dfs(int x) { if(x==n+1) { work(); return; } for(int i=1; i<=n; i++) { if(!vis[i]) { vis[i]=1; a[x]=i; dfs(x+1); vis[i]=0; } } } signed main() { cin>>n; dfs(1); return 0; }以上代码输出所有的方案和对应的 的值。
当 时输出:2 3 4 5 6 7 1 0 0 0 0 0 2 3 4 5 7 1 6 0 0 0 0 1 2 3 4 6 1 7 5 0 0 0 1 0 2 3 4 7 1 5 6 0 0 0 1 1 2 3 5 1 6 7 4 0 0 1 0 0 2 3 5 1 7 4 6 0 0 1 0 1 2 3 6 1 4 7 5 0 0 1 1 0 2 3 7 1 4 5 6 0 0 1 1 1 2 4 1 5 6 7 3 0 1 0 0 0 2 4 1 5 7 3 6 0 1 0 0 1 2 4 1 6 3 7 5 0 1 0 1 0 2 4 1 7 3 5 6 0 1 0 1 1 2 5 1 3 6 7 4 0 1 1 0 0 2 5 1 3 7 4 6 0 1 1 0 1 2 6 1 3 4 7 5 0 1 1 1 0 2 7 1 3 4 5 6 0 1 1 1 1 3 1 4 5 6 7 2 1 0 0 0 0 3 1 4 5 7 2 6 1 0 0 0 1 3 1 4 6 2 7 5 1 0 0 1 0 3 1 4 7 2 5 6 1 0 0 1 1 3 1 5 2 6 7 4 1 0 1 0 0 3 1 5 2 7 4 6 1 0 1 0 1 3 1 6 2 4 7 5 1 0 1 1 0 3 1 7 2 4 5 6 1 0 1 1 1 4 1 2 5 6 7 3 1 1 0 0 0 4 1 2 5 7 3 6 1 1 0 0 1 4 1 2 6 3 7 5 1 1 0 1 0 4 1 2 7 3 5 6 1 1 0 1 1 5 1 2 3 6 7 4 1 1 1 0 0 5 1 2 3 7 4 6 1 1 1 0 1 6 1 2 3 4 7 5 1 1 1 1 0 7 1 2 3 4 5 6 1 1 1 1 1可以发现,第一个形如
1 1 ... 1 0的二进制段,假设其 的个数为 ,则前 个数的方案为 。选取
3 1 5 2 7 4 6,其 值的后面 位为 ,对应 的方案为3 1 5 2 4,发现这和5 2 7 4 6相对顺序相同,不难想到直接递归即可。具体地,你在选数时选择当前还未选择的第 和 大的数即可。
对 做除二操作, 模拟上述过程,时间复杂度 。Code:
#include<bits/stdc++.h> using namespace std; #define int long long const int N=50005; int n,v[N],vis[N];//二进制从低到高第i位 struct bigint { int a[N]; } k,now,U; bigint read() { bigint x=U; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()); for(; c>='0'&&c<='9'; c=getchar()) { for(int i=N-1; i; i--)x.a[i]=x.a[i-1]; x.a[0]=c^48; } return x; } int div2(bigint x) { int g=x.a[0]&1; for(int i=N-1; i>=0; i--) { int z=x.a[i]; if((z&1)&&i)x.a[i-1]+=10; x.a[i]/=2; } now=x; return g; } bigint del1(bigint x) { int now=0; while(1) { x.a[now]--; if(x.a[now]<0) { x.a[now]+=10; now++; } else break; } return x; } void sol() { cin>>n; k=del1(read()); for(int i=1; i<=n-2; i++) { v[i]=div2(k); k=now; } //for(int i=n-2;i>=1;i--){cout<<v[i]<<' ';} for(int i=n-2; i>=1; i--) { int w=0; while(v[i-w]==1)w++; int cnt=0,nw=0; for(int j=1; j<=n; j++) { if(cnt==w+2)break; if(vis[j]==0) { cnt++; nw=j; } } cout<<nw<<' '; vis[nw]=1; cnt=0; for(int j=1; j<=n; j++) { if(cnt==w)break; if(vis[j]==0) { cnt++; cout<<j<<' '; vis[j]=1; } } i=i-w; } if(vis[n]==0)cout<<n<<' '; for(int j=1; j<n; j++)if(vis[j]==0)cout<<j<<' '; } signed main() { freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout); int t=1; while(t--)sol(); return 0; } -
1
找规律
===== n = 2 ===== 2 1 ===== n = 3 ===== 2 3 1 3 1 2 ===== n = 4 ===== 2 3 4 1 2 4 1 3 3 1 4 2 4 1 2 3 ===== n = 5 ===== 2 3 4 5 1 2 3 5 1 4 2 4 1 5 3 2 5 1 3 4 3 1 4 5 2 3 1 5 2 4 4 1 2 5 3 5 1 2 3 4 ===== n = 6 ===== 2 3 4 5 6 1 2 3 4 6 1 5 2 3 5 1 6 4 2 3 6 1 4 5 2 4 1 5 6 3 2 4 1 6 3 5 2 5 1 3 6 4 2 6 1 3 4 5 3 1 4 5 6 2 3 1 4 6 2 5 3 1 5 2 6 4 3 1 6 2 4 5 4 1 2 5 6 3 4 1 2 6 3 5 5 1 2 3 6 4 6 1 2 3 4 5发现第一个数为 的方案数为
总方案数为
将 的对应反转一下变为
2 3 4 5 6 1 10000 2 3 4 6 1 5 1111 2 3 5 1 6 4 1110 2 3 6 1 4 5 1101 2 4 1 5 6 3 1100 2 4 1 6 3 5 1011 2 5 1 3 6 4 1010 2 6 1 3 4 5 1001 3 1 4 5 6 2 1000 3 1 4 6 2 5 111 3 1 5 2 6 4 110 3 1 6 2 4 5 101 4 1 2 5 6 3 100 4 1 2 6 3 5 11 5 1 2 3 6 4 10 6 1 2 3 4 5 1太史了 再减
2 3 4 5 6 1 1111 2 3 4 6 1 5 1110 2 3 5 1 6 4 1101 2 3 6 1 4 5 1100 2 4 1 5 6 3 1011 2 4 1 6 3 5 1010 2 5 1 3 6 4 1001 2 6 1 3 4 5 1000 3 1 4 5 6 2 111 3 1 4 6 2 5 110 3 1 5 2 6 4 101 3 1 6 2 4 5 100 4 1 2 5 6 3 11 4 1 2 6 3 5 10 5 1 2 3 6 4 1 6 1 2 3 4 5 0发现规律
第一个为 ,然后为 到 ,后面的数也是求排列,递归下去处理
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 5002; int n; string _K; int K[N], Kl, nb; int l, a[N]; set <int> st; // 这个是为了方便求递归下去的排列对应的数 vector <int> ans; inline void solve(int w){ vector <int> v; for(auto s : st){ v.push_back(s); } if(!w){ // 赛时脑子抽了写的,可以删掉 ans.push_back(v[1]); ans.push_back(v[0]); return; } for(int i = 1; i <= w; ++ i){ // 这里是找当前排列的第一个数是什么 if(a[w - i]){ ans.push_back(v[i]); st.erase(v[i]); for(int j = 0; j <= i - 2; ++ j){ ans.push_back(v[j]); st.erase(v[j]); } solve(w - i); return; } } // 后面没有排列了,不用递归 w = v.size() - 1; ans.push_back(v[w]); for(int i = 0; i < w; ++ i){ ans.push_back(v[i]); } } int main(){ freopen("permutation.in", "r", stdin); freopen("permutation.out", "w", stdout); ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> _K; Kl = _K.size() - 1; for(int i = 0; i <= Kl; ++ i){ K[i] = _K[Kl - i] - '0'; } -- K[0]; for(int i = 0; i <= Kl && K[i] < 0; ++ i){ -- K[i + 1]; K[i] += 10; } if(Kl && !K[Kl]) -- Kl; l = -1; while(K[Kl]){ nb = 0; for(int i = Kl; i >= 0; -- i){ nb = (nb ? 10 : 0) + K[i]; K[i] = nb >> 1; nb &= 1; } a[++ l] = nb; while(Kl && !K[Kl]) -- Kl; } l = max(0, l); for(int i = 0; i < n - 2; ++ i){ a[i] ^= 1; } // 前面都是 k 转二进制 for(int i = 1; i <= n; ++ i){ st.insert(i); } solve(n - 2); for(auto s : ans) cout << s << ' '; return 0; } /* g++ permutation.cpp -o permutation -lm -O2 -std=c++14 -Wall ./permutation */
- 1
信息
- ID
- 72
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 5
- 标签
- 递交数
- 48
- 已通过
- 18
- 上传者

