Codeforces Round #774 (Div. 2) C. Factorials and Powers of Two(暴力/dfs/位运算)
include <bits/stdc++.h>
define gcd(a, b) __gcd(a, b)
define INF 0x3f3f3f3f3f
define eps 1e-6
define PI acos(-1.0)
define pb push_back
define fst first
define sec second
define eif else if
define de(x) cout << x << ' '
define en cout << '\n'
define fuck cout << "fuck\n"
define rep(i, x, y) for (int i = x; i < y; i++)
define red(i, x, y) for (int i = x - 1; i >= y; i--)
define mem(a, x) memset(a, x, sizeof(a))
define IOS cin.tie(0), ios::sync_with_stdio(false)
define maxn 200005
define mod 1000000007
typedef long long ll;
define pll pair<ll, ll>
using namespace std;
ll n, ans;
ll a[105];
ll count(ll x) {
ll cnt = 0;
while(x) {
cnt += (x & 1);
x >>= 1;
}
return cnt;
}
void dfs(ll x, ll sum, ll cnt) {
if(x == 16) {
//cout << x << endl;
ll res = n - sum;
ans = min(ans, count(res) + cnt);
return;
}
if(sum > n) return;
dfs(x + 1, sum, cnt);
if(x >= 3 && sum + a[x] <= n) dfs(x + 1, sum + a[x], cnt + 1);//避免2^0, 2^1与1!, 2!冲突
}
void solve() {
cin >> n;
ans = count(n);
a[1] = 1ll;
for(int i = 2; i <= 15; i++) {
a[i] = a[i - 1] * i;
}
dfs(1, 0, 0);
cout << ans << endl;
}
signed main() {
int T = 1;
cin >> T;
while (T--) {
solve();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-03-05 CH1807 Necklace (字符串Hash + 最小表示法)
2020-03-05 P1403 [AHOI2005]约数研究(筛法)
2020-03-05 P1029 最大公约数和最小公倍数问题(数论水题)
2020-03-05 洛谷P1147连续自然数和(前缀和)