Factorials and Powers of Two

分析:我们可以看出这道题目的描述并不是很复杂,就是说对于一个给定的整数n,我们能否把他拆成k个powerful的数,也就是说这k个数要么是2的幂次,要么是某个数的阶乘,并且我们要让当前的k越小越好;然后如果不能被拆的话输出-1;
我们这样来看,先看会不会输出-1,我们如果把这个整数n用二进制的方法写出来,每个1都表明可以写成某个powerful的数,所以不可能输出-1;
那么我们就可以发现了k的个数就是这里二进制表示中1的个数,但是我们考虑到还有阶乘,我们令阶乘的和为s,个数为cnt,则k = cnt + F(n-s),这里的F函数就是根据二进制找1;
既然这样我们就可以枚举每个阶乘的可能性,我们发现14!已经是最大的可能了,因为15!就已经超过了1^12的数据范围,并且我们可以发现1!和2!是不需要考虑的,因为他们和幂次是一换一的关系没有必要,所以最多只需要枚举2^12次,找到最小值即可!
那么这里的关键是在于我怎么把这么多种可能枚举出来呢,很显然不适合用dfs,所以我们这里枚举i为0~1<<12,然后再去枚举j从0~11,看i&1<<j是否存在,存在的话就让s加上factorial[j+3],我们就是通过枚举12个位所有为0和为1的可能性,然后去看,就相当于是电路的12条并联的电路,只有对应通路的时候才会加上那条路的电阻!
代码:
- #include<bits/stdc++.h>
- #define INF 1100000000
- using namespace std;
- typedef long long LL;
- typedef pair<LL,LL> PII;
- LL fa[20],n;
- int k = INF;
- int find(LL x){
- int cnt = 0;
- while(x){
- cnt += x&1;
- x >>= 1;
- }
- return cnt;
- }
- int main()
- {
- int t;
- cin >> t;
- fa[1] = 1;
- fa[2] = 2;
- for(int i = 3;i<=14;i++) fa[i] = fa[i-1]*i;
- while(t--){
- k = 1100000000;
- cin >> n;
- for(int i = 0;i<(1<<12);i++){
- int cnt = 0;
- LL s = 0;
- for(int j = 0;j<=11;j++){
- if(i&(1<<j)){
- cnt++;
- s+=fa[j+3];
- }
- }
- if(s>n) continue;
- k = min(k,cnt + find(n - s));
- }
- cout << k << '\n';
- }
- return 0;
- }
Factorials and Powers of Two的更多相关文章
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- CodeForces 404C Ivan and Powers of Two
Ivan and Powers of Two Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- 每日一九度之 题目1038:Sum of Factorials
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- Educational Codeforces Round 15 Powers of Two
Powers of Two 题意: 让求ai+aj=2的x次幂的数有几对,且i < j. 题解: 首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后 ...
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- cf702B Powers of Two
B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- (Problem 34)Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...
- (Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...
随机推荐
- 36 异常机制 自定义异常 实际应用中的经验总结 尽量添加finally语句块去释放占用的资源
自定义异常 概念 使用Java内置的异常可以描述在编辑时出现的大部分异常情况.除此之外,用户还可以自定义异常.用户自定义异常类,只需继承Exception类即可. 在程序中使用自定义异常类,大体可分为 ...
- netty通信
学习netty之前,要先了解操作系统中的IO.零拷贝(已经附上链接了) 一.netty的简单介绍 Netty 是由 JBOSS 提供的一个 Java 开源框架,现为 Github 上的独立项目. Ne ...
- C++中vector::data()使用心得和对自定义类型指针运算符的默认重载
一.C++ vector::data()函数 返回值类型:vector的基类 返回值:Returns a pointer such that [data(), data() + size()] is ...
- java实现下载网络图片
package com.gylhaut.picture;import java.io.*;import java.net.MalformedURLException;import java.net.U ...
- 微服务8:通信之RPC实践篇(附源码)
★微服务系列 微服务1:微服务及其演进史 微服务2:微服务全景架构 微服务3:微服务拆分策略 微服务4:服务注册与发现 微服务5:服务注册与发现(实践篇) 微服务6:通信之网关 微服务7:通信之RPC ...
- 使用Xtrabackup 备份mysql数据库
##创建逻辑卷 [root@node1 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully create ...
- FrameScan CMS漏洞扫描
工具简介 GithubL:https://github.com/qianxiao996/FrameScan FrameScan是一款python3编写的简易的cms漏洞检测框架,支持多种检测方式,支持 ...
- CVE-2017-11882(Office远程代码执行)
测试环境:win7+kali+office 2007 xp+kali+office 2003 win7 ip:10.10.1.144 xp ip: 10.10.1.126 kali ip 10.10. ...
- file_put_contents利用技巧(php://filter协议)
Round 1 <?php $content = '<?php exit; ?>'; $content .= $_POST['txt']; file_put_contents($_P ...
- Docker容器入门介绍
1.前言 Docker是一种新兴的虚拟化技术,能够一定程度上的代替传统虚拟机.不过,Docker 跟传统的虚拟化方式相比具有众多的优势.Docker: 本意是码头工人,言外之意是集装箱: Java号称 ...