阿萨FSDF

furuozhan 2021-07-30 13:55:09 4

#include <bits/stdc++.h> using namespace std; int n, a[10000], ci[10000]; int poww[1000] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216,33554432 }; bool f = 0; void dfs(int k, int he) { if (he == n) { for (int i = k - 1; i >= 1; i--) cout << a[i] << " "; f = 1; return; } for (int i = 1;; i++) { int tt = he + poww[i]; if (tt > n) break; if (i > ci[k - 1]) { ci[k] = i; a[k] = tt - he; dfs(k + 1, tt); } } } int main() { freopen("power.in", "r", stdin); freopen("power.out", "w", stdout); cin >> n; dfs(1, 0); if (f == 0) cout << "-1"; return 0; }

{{ vote && vote.total.up }}

共 1 条回复

huzhehan
#include <bits/stdc++.h> using namespace std; int n, a[10000], ci[10000]; int poww[1000] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216,33554432 }; bool f = 0; void dfs(int k, int he) { if (he == n) { for (int i = k - 1; i >= 1; i--) cout << a[i] << " "; f = 1; return; } for (int i = 1;; i++) { int tt = he + poww[i]; if (tt > n) break; if (i > ci[k - 1]) { ci[k] = i; a[k] = tt - he; dfs(k + 1, tt); } } } int main() { freopen("power.in", "r", stdin); freopen("power.out", "w", stdout); cin >> n; dfs(1, 0); if (f == 0) cout << "-1"; return 0; }