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, ...
随机推荐
- php 数组汇总
PHP 5 Array 函数 函数 描述 array() 创建数组. array_change_key_case() 返回其键均为大写或小写的数组. array_chunk() 把一个数组分割为新的数 ...
- egg-multipart + el-upload 实现带参图片上传至阿里云OSS
egg-multipart有两种模式:file和stream el-upload参数传递有两种方式:利用自带参数data和手动添加参数 egg-multipart介绍 一.file 模式下的带参传递 ...
- Java学习笔记:02面向对象-重写_this_super_抽象类
02面向对象-重写/this/super/抽象类 ****1.this和super 作用: this: 区分本类的成员变量和局部变量同名情况 super:区分父类的成员变量和局部变量同名情况 用法: ...
- spring——依赖注入的三种方式
1 构造器注入(与构造器有直接关系) 默认无参构造 3种构造方式:通过<contructor-arg>调用类中的构造器 下标 <bean id="userService&q ...
- Chartjs 初体验
I 官网 https://www.chartjs.org/ https://chartjs.bootcss.com/ 中文网址 简单易上手,支持的Chart 类型:折线图,饼图,柱状,雷达图,网状图 ...
- rancher接管已部署的集群
1.选择一台服务器部署rancher服务 docker pull rancher/rancher:v2.5.6 #拉取rancher镜像 docker run --privileged -d -v / ...
- python 包之 xlwt 操作 excel 教程
一.安装 pip install xlwt 二.创建表格并写入 创建表格,设置sheet名称 写入指定行列的数据,将表格进行保存 import xlwt # 创建一个workbook并设置编码 wor ...
- Badger简单使用
Badger简介 badger 是 dgraph 开源的 LSMTree 的 KV 引擎,它相比 leveldb 有 KV 分离.事务.并发合并等增强,是 go 生态中比较生产级的存储引擎了. 文档: ...
- CF1500D Tiles for Bathroom (递推+大讨论)
题目大意:给你一个n*n的矩阵,现在问对于每个k\le n,求出所有k*k的子矩阵中,元素种类数不超过q的矩阵个数,n\le 1500, q\le 10 先考虑最暴力的做法: 对于每个格子,求出以它为 ...
- Redis的Jedis操作(五)
需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层. 1.连接单机版 第一步:创建一个Jedis对象.需要指定服务端的ip及端口. 第二步 ...