当前位置:网站首页>Problem - 1646C. Factorials and Powers of Two - Codeforces
Problem - 1646C. Factorials and Powers of Two - Codeforces
2022-07-06 09:28:00 【你好_Ä】
Problem - 1646C. Factorials and Powers of Two - Codeforces
A number is called powerful if it is a power of two or a factorial. In other words, the number m is powerful if there exists a non-negative integer d such that m=2d or m=d!, where d!=1⋅2⋅…⋅d (in particular, 0!=1). For example 1, 4, and 6 are powerful numbers, because 1=1!, 4=22, and 6=3! but 7, 10, or 18 are not.
You are given a positive integer n. Find the minimum number k such that n can be represented as the sum of k distinct powerful numbers, or say that there is no such k.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.
A test case consists of only one line, containing one integer n (1≤n≤1012).
Output
For each test case print the answer on a separate line.
If n can not be represented as the sum of distinct powerful numbers, print −1.
Otherwise, print a single positive integer — the minimum possible value of k.
Example
input
4
7
11
240
17179869184
output
2
3
4
1
问题解析
这题是说,给你一个数,让你看这个数最少可以由几个2的幂(1,2,4,8等)和阶乘(1,2,6,24)等组成。
首先,每个数都必然会被组合出来,因为所有的数都可以用二进制表示,二进制下有x个1,就说明这个数可以用x个2的次幂来组成,那么我们就是要找在阶乘的组合下,能不能找到更小的x。
题目的数组范围是10^12 ,那么阶乘最大就到15!,算上0!,一共是16个数。我们可以用二进制枚举的方法来枚举(从1枚举到216,看二进制位上哪个位置有1,我们就取哪个数,这样就可以得到这16个数所有的组合)。算出合后,计算:(所用阶乘数+n减去合后用二进制取得的1个数),并维护最小值即可。时间复杂度为O(n*216 )
AC代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 5050;
int lowbit(ll x)
{
int cnt = 0;
while (x)
{
if (x % 2 == 1)cnt++;
x /= 2;
}
return cnt;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
vector<ll>ans(16);
ans[0] = 1;
for (int i = 1; i <= 15; i++)ans[i] = ans[i - 1] * i;
int t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
ll res = lowbit(n), sum = 0, cnt = 0;
for (ll i = 1; i <= (1 << 16); i++)
{
sum = 0, cnt = 0;
for (ll j = 0; j <= 15; j++)
{
if (((i >> j) & 1))
{
sum += ans[j];
cnt++;
}
}
if (sum <= n)
{
res = min(cnt + lowbit(n - sum), res);
}
}
cout << res << endl;
}
return 0;
}
边栏推荐
- Some problems encountered in installing pytorch in windows11 CONDA
- QT实现圆角窗口
- What is the difficulty of programming?
- AcWing:第56场周赛
- 快速转 TypeScript 指南
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- Codeforces Round #803 (Div. 2)A~C
- Vs2019 initial use
- Information security - Analysis of security orchestration automation and response (soar) technology
- 【练习-7】Crossword Answers
猜你喜欢
读取和保存zarr文件
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Pytorch extract skeleton (differentiable)
Sword finger offer II 019 Delete at most one character to get a palindrome
渗透测试 ( 4 ) --- Meterpreter 命令详解
1005. Maximized array sum after K negations
Data storage in memory & loading into memory to make the program run
Pyside6 signal, slot
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
随机推荐
Penetration test (7) -- vulnerability scanning tool Nessus
Openwrt source code generation image
【练习-7】Crossword Answers
Vs2019 initial use
807. Maintain the urban skyline
Determine the Photo Position
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Candy delivery (Mathematics)
Information security - threat detection - detailed design of NAT log access threat detection platform
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
[exercise-2] (UVA 712) s-trees
MySQL授予用户指定内容的操作权限
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
[exercise-8] (UVA 246) 10-20-30== simulation
628. Maximum product of three numbers
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
Is the sanic asynchronous framework really so strong? Find truth in practice
409. Longest palindrome
Sword finger offer II 019 Delete at most one character to get a palindrome