当前位置:网站首页>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;
}
边栏推荐
- QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
- C language must memorize code Encyclopedia
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- [exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
- Basic Q & A of introductory C language
- Suffix expression (greed + thinking)
- [exercise-9] Zombie's Treasury test
- 图图的学习笔记-进程
- Ball Dropping
- (POJ - 2739) sum of constructive prime numbers (ruler or two points)
猜你喜欢
随机推荐
807. Maintain the urban skyline
1529. Minimum number of suffix flips
Openwrt source code generation image
F - birthday cake (Shandong race)
2078. Two houses with different colors and the farthest distance
605. Planting flowers
Vs2019 initial use
Codeforces Round #802(Div. 2)A~D
Is the sanic asynchronous framework really so strong? Find truth in practice
Problem - 1646C. Factorials and Powers of Two - Codeforces
New to redis
Maximum product (greedy)
QNetworkAccessManager实现ftp功能总结
1855. Maximum distance of subscript alignment
Determine the Photo Position
Sword finger offer II 019 Delete at most one character to get a palindrome
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
Information security - security professional name | CVE | rce | POC | Vul | 0day
Useeffect, triggered when function components are mounted and unloaded
(POJ - 3579) median (two points)