当前位置:网站首页>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;
}
边栏推荐
- 2027. Minimum number of operations to convert strings
- 2078. Two houses with different colors and the farthest distance
- Penetration test (8) -- official document of burp Suite Pro
- Opencv learning log 26 -- detect circular holes and mark them
- 1323. Maximum number of 6 and 9
- E. Breaking the Wall
- 7-1 understand everything (20 points)
- Flask框架配置loguru日志库
- Sword finger offer II 019 Delete at most one character to get a palindrome
- Understand what is a programming language in a popular way
猜你喜欢
Ball Dropping
Flag framework configures loguru logstore
860. Lemonade change
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Openwrt build Hello ipk
力扣——第298场周赛
628. Maximum product of three numbers
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Frida hook so layer, protobuf data analysis
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
随机推荐
【练习-6】(Uva 725)Division(除法)== 暴力
QT实现窗口渐变消失QPropertyAnimation+进度条
Codeforces Round #802(Div. 2)A~D
MySQL授予用户指定内容的操作权限
读取和保存zarr文件
Quick to typescript Guide
C language must memorize code Encyclopedia
Advancedinstaller安装包自定义操作打开文件
Information security - Analysis of security orchestration automation and response (soar) technology
D - function (HDU - 6546) girls' competition
Information security - threat detection - detailed design of NAT log access threat detection platform
MySQL grants the user the operation permission of the specified content
TCP's three handshakes and four waves
AcWing:第58场周赛
Shell Scripting
Opencv learning log 28 -- detect the red cup cover
Programmers, what are your skills in code writing?
Configuration du cadre flask loguru log Library
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
分享一个在树莓派运行dash应用的实例。