当前位置:网站首页>Problem - 1646C. Factorials and Powers of Two - Codeforces
Problem - 1646C. Factorials and Powers of Two - Codeforces
2022-07-06 16:14:00 【Hello_ Ä】
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
Problem analysis
The question is , Give you a number , Let me show you how many this number can be at least 2 The power of (1,2,4,8 etc. ) And factorial (1,2,6,24) Other components .
First , Every number must be combined , Because all numbers can be expressed in binary , There is x individual 1, It shows that this number can be used x individual 2 To the power of , So we are looking for the combination of factorials , Can you find something smaller x.
The array range of topics is 10^12 , Then the factorial maximum is 15!, You count 0!, Is the total 16 Number . We can use binary enumeration method to enumerate ( from 1 Enumerate to 216, See which position on the binary bit has 1, Let's take whichever number , So you can get this 16 Number all combinations ). Calculate the sum , Calculation :( Order multiplier used +n After subtracting and combining, it is obtained in binary 1 Number ), And maintain the minimum value . The time complexity is O(n*216 )
AC Code
#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;
}
边栏推荐
- Hdu-6025-prime sequence (girls' competition)
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- Codeforces Round #798 (Div. 2)A~D
- 1323. Maximum number of 6 and 9
- 2078. Two houses with different colors and the farthest distance
- Quick to typescript Guide
- Openwrt source code generation image
- Sword finger offer II 019 Delete at most one character to get a palindrome
- 1529. Minimum number of suffix flips
- 1903. Maximum odd number in string
猜你喜欢

1529. Minimum number of suffix flips

“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看

pytorch提取骨架(可微)

QT实现圆角窗口

969. Pancake sorting

QWidget代码设置样式表探讨

1689. Ten - the minimum number of binary numbers

1013. Divide the array into three parts equal to and

Some problems encountered in installing pytorch in windows11 CONDA

渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
随机推荐
Opencv learning log 29 -- gamma correction
b站 实时弹幕和历史弹幕 Protobuf 格式解析
[exercise-8] (UVA 246) 10-20-30== simulation
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
If you want to apply for a programmer, your resume should be written like this [essence summary]
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
图图的学习笔记-进程
Pyside6 signal, slot
(POJ - 3685) matrix (two sets and two parts)
Advancedinstaller安装包自定义操作打开文件
Frida hook so layer, protobuf data analysis
渗透测试 ( 4 ) --- Meterpreter 命令详解
Analysis of protobuf format of real-time barrage and historical barrage at station B
HDU - 6024 building shops (girls' competition)
Sanic异步框架真的这么强吗?实践中找真理
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
QT实现圆角窗口
Penetration test (7) -- vulnerability scanning tool Nessus
Common configuration files of SSM framework