C. Factorials and Powers of Two

2023-10-25 12:50
文章标签 two powers factorials

本文主要是介绍C. Factorials and Powers of Two,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:C. Factorials and Powers of Two

        

题意:

        若一个数是2的次方,或是某个数的阶乘,则这个数为powerful数,给出一个数n,求n最少可表示为多少个powerful数的和

        数据范围:1\leq n \leq 10^{12}

思路:

        1、任意一个数肯定可以表示成若干个2的次方数的和,因为把这个数转化成二进制形式,为1的位置就是一个2的次方数。

        2、用阶乘来替换掉一些1,15的阶乘已经超过10^{12},所以共0~14个阶乘,二进制枚举选或不选,假如选了cnt个阶乘,和为sum,剩下的用2的次方数来表示,则这种选法的答案就是cnt 加上 (n-sum)的二进制表示中1的个数。

        3、__builtin_popcountll()函数可求一个数的二进制表示有多少个1

代码:

#include <iostream>
#include <algorithm>using namespace std;typedef long long LL;
LL fac[15];void init()
{LL num = 1;fac[0] = 1;for(int i = 1; i <= 14; i++)fac[i] = fac[i-1] * i;
}int main()
{init();int t;cin >> t;while(t--){LL n;cin >> n;int res = 0x3f3f3f3f;for(int i = 0; i <= (2<<14); i++){LL tmp = n;int cnt = 0;for(int j = 0; j <= 14; j++)if((i>>j) & 1){tmp -= fac[j];cnt++;}res = min(res, cnt + __builtin_popcountll(tmp));}cout << res << endl;}return 0;
}

这篇关于C. Factorials and Powers of Two的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/282701

相关文章

Java多线程编程模式实战指南:Two-phase Termination模式

文章来源: http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-two-phase-termination?utm_source=infoq&utm_campaign=user_page&utm_medium=link 文章代码地址: https://github.com/Visce

[LeetCode] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in

CodeForces 425C Sereja and Two Sequences

题意: 两组数字a和b  如果a[i]等于b[j]  则可将a[i]和b[j]前所有数字删掉  这种操作花费e体力  得到1元钱  或者一次删掉所有数字  这种操作花费等于曾经删除的所有数字个数  做完后得到所有钱  问 一共s体力 可以得到多少钱 思路: dp+二分 由数据可知最多拿到300元钱  因此可以定义  dp[i][j]表示有i元钱时  b串删除到了j处时  a串删到的位

poj 1849 Two(树形dp求直径)

树的直径:一棵树中两个点间的最长距离。 Description The city consists of intersections and streets that connect them.  Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that ha

Longest Substring with At Most Two Distinct Characters

Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example,Given s = “eceba”, T is "ece" which its length is 3. 思路:同向双指针,跟Longest Substrin

Two to One——C语言提高题【7 kyu】

一、原题 链接:Training on Two to One | Codewars Take 2 strings s1 and s2 including only letters from a to z. Return a new sorted string (alphabetical ascending), the longest possible, containing distinct

LeetCode --- Median of Two Sorted Arrays

第一次在csdn上写备忘录,以前一直是在笔记本上写,主要是笔记本上可以随意写,只要自己能看懂,在网页上多少都受些限制,另外一方面也是想锻炼下写作能力,为以后的论文做基础吧!最近偶尔上leetcode练些题目,所以也就以这个为主题写一篇试试看,因为能力不足,理解或言辞上会有错误,还望访者不吝赐教,我定当万分感激。 好了,废话也说完了,现在进入正题: 题目: There are two sor

2pc_two phase commit详情

文章目录 1. two phase commit protocol1.假设前提2. 算法概述3. 缺点4. 详情1. coordinator 端来看2. cohorts端 5. 正确性分析6. 简单总结 看2pc和3pc看的晕晕乎乎的,看了很多博客,感觉说的都不够细致,看起来也容易犯晕,找到了两篇英文文档(不算原文),看起来好像是清楚一些,有些时候这些协议类的东西研读,如果不是

Leetcode43: Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 用归并排序的思想。 /*** Definition for singly-linked list.* str

Leetcode207: Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 该方法的核心是将原问题转变成一个寻找第k小数的问