CodeForces-1249C2-Good Numbers (hard version) -巧妙进制/暴力
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number nn. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 33 (i.e. no duplicates of powers of 33 are allowed).
For example:
- 3030 is a good number: 30=33+3130=33+31,
- 11 is a good number: 1=301=30,
- 1212 is a good number: 12=32+3112=32+31,
- but 22 is not a good number: you can't represent it as a sum of distinct powers of 33 (2=30+302=30+30),
- 1919 is not a good number: you can't represent it as a sum of distinct powers of 33 (for example, the representations 19=32+32+30=32+31+31+31+3019=32+32+30=32+31+31+31+30 are invalid),
- 2020 is also not a good number: you can't represent it as a sum of distinct powers of 33 (for example, the representation 20=32+32+30+3020=32+32+30+30 is invalid).
Note, that there exist other representations of 1919 and 2020 as sums of powers of 33 but none of them consists of distinct powers of 33.
For the given positive integer n find such smallest m (n≤m) that m is a good number.
You have to answer qq independent queries.
Input
The first line of the input contains one integer q(1≤q≤500) — the number of queries. Then qq queries follow.
The only line of the query contains one integer n(1≤n≤1018).
Output
For each query, print such smallest integer m (where n≤m) that m is a good number.
Example
8
1
2
6
13
14
3620
10000
1000000000000000000
1
3
9
13
27
6561
19683
1350851717672992089 题意:
对于每组数据, 给出一个n,要求求出大于等于n的m,m需要满足的条件是:由3的次方相加组成,且每个3的次方最多只能出现一次。 注意:
对于次方问题,最好用快速幂进行运算,运用pow会造成奇怪错误。 法一:三进制
思路:
由于求的答案与3的次方有关,所以可以把每一个给出来的n转换成三进制存到数组里再去进行判断,
比如给出一个n=14,转换成三进制的话就是112,从高位往低位进行判断,
如果存放进制的数组里面只含有1和0的话,说明该数是3的次方,直接输出即可
否则可以进行判断,从低位往高位找,找到第一个2记录下标且break
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1e4+;
typedef unsigned long long ull; ull a[N]; ull ksm(ull x,ull n)
{
ull res=;
while(n>)
{
if(n&)
res=res*x;
x=x*x;
n>>=;
}
return res;
} int main()
{
int t;
ull n;
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof(a));
scanf("%llu",&n);
int p=;
ull ans=n;
while(n)
{
a[p++]=n%;
n=n/; }
// for(int i=0;i<p;i++)
// printf("%d",a[i]);
// printf("\n");//211
//从低位往高位找,找到第一个2记录下标且break
int k=-;
for(int i=p-;i>=;i--)
{
if(a[i]==)
{
k=i;
break;
}
}
if(k==-)
{
printf("%llu\n",ans);
continue;
}
// printf("%d***\n",k);
for(int i=k;i<p;i++)
{
if(a[i]==)
{
a[i]=;
a[i+]++;
// break;
// printf("%d***%d\n",i+1,a[i+1]);
}
} if(a[p]!=)
p++;
ull sum=;
for(int i=p-;i>=k;i--)
{
sum=sum+a[i]*ksm(,i);
}
printf("%llu\n",sum); }
return ;
}
法二:暴力
思路:
- 首先数据给出的范围是1018,所以可以通过打表计算出3的几次方会超过该范围,打表得出是38,所以数字开到40就ok
ll sum=;
int k;
for(int i=;i<N;i++)
{
sum=sum*;
if(sum>=1e18)
{
printf("%d\n",i);//i==38
break;
}
}
- 把3的次方存入a数组中。
- 再来,开始分析题意,题意要求求的是满足条件最小的m,所以如果将a数组从小往大遍历的话,找到的m不一定是最小的,所以需要将a数组从大往小遍历,再把小于等于n的第一个数减掉。
- 还需要知道的一点就是30+31+32+...+3n-1<3n,所以最后再从前往后遍历,找到没有被标记过的数,注意30需要特判。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1e4+;
typedef long long ll; ll a[];
bool book[N]; ll ksm(ll x,ll n)
{
ll res=;
while(n>)
{
if(n&)
res=res*x;
x=x*x;
n>>=;
}
return res;
} int main()
{
for(int i=; i<=; i++)
a[i]=ksm(,i);
int t;
ll n;
scanf("%d",&t);
while(t--)
{
memset(book,,sizeof(book));
scanf("%lld",&n);
ll sum=;
while()
{
int flag=;
for(int i=; i>=; i--)
{
if(a[i]<=n&&book[i]==)//第一个找到的就是最大的
{
sum+=a[i];
flag=;
book[i]=;
n-=a[i];
break;
}
}
if(flag==)
break;
}
if(n==)
{
printf("%lld\n",sum);
continue;
}
ll kk;
int k;
for(int i=;i<=;i++)
{
if(book[i]==)
{
kk=a[i];
k=i;
break;
}
}
sum=sum+kk;
for(int i=;i<k;i++)
sum-=a[i];
printf("%lld\n",sum);
}
return ;
}
CodeForces-1249C2-Good Numbers (hard version) -巧妙进制/暴力的更多相关文章
- PAT甲级——1100 Mars Numbers (字符串操作、进制转换)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...
- HDU 1887 Weird Numbers(负数的进制转化)
题目要求有两种情况,第一种from情况,正常输出即可,很简单.第二种是to情况,给一个数字,输出负进制R的原码,这个有点小麻烦...解决方法如下; 首先,把这个数n按正常方式展开,形式如下: .... ...
- CodeForces 1B-字符串,进制转换与数学
一个萌新的成长之路 Background 同学们都回家了,只有我和wjh还有邢神在机房敲代码,吃random口味的方便面-- Description Translated by @PC_DOS fro ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces Round #538 (Div. 2) C 数论 + 求b进制后缀零
https://codeforces.com/contest/1114/problem/C 题意 给你一个数n(<=1e8),要你求出n!在b进制下的后缀零个数(b<=1e12) 题解 a ...
- 蓝桥杯 问题 1110: 2^k进制数 (排列组合+高精度巧妙处理)
题目链接 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2 ...
- Codeforces 552C Vanya and Scales(进制转换+思维)
题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...
- 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales
题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...
- Codeforces 878 E. Numbers on the blackboard
Codeforces 878 E. Numbers on the blackboard 解题思路 有一种最优策略是每次选择最后面一个大于等于 \(0\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...
随机推荐
- 排序(分组后排序&整排)
一.整排 要求:根据score进行排名,分数相同,名次相同,且连续 表如下图: sql语句: 方法一:select a.score, (select count(distinct b.score) f ...
- 通过rocketmq思考一下mq的设计取舍
1. RocketMQ路由中心NameServer 2. RocketMQ消息发送 3. RocketMQ消息存储 4. RocketMQ消息消费 5. 消息过滤FiterServer 6. Rock ...
- python中使用动态库
首先,创建一个简单的动态库编程生成dll.so: gcc -fPIC -O2 -shared dll.c -o dll.soC文件:dll.c 如下 #include <stdio.h&g ...
- log4j 和 log4j2 在springboot中的性能对比
文章链接: https://pengcheng.site/2019/11/17/log4j-he-log4j2-zai-springboot-zhong-de-xing-neng-dui-bi/ 前言 ...
- buuctf zip伪加密
平时伪加密总是依赖osx,这道题无法直接解压,所以研究一下伪加密先放两张图(图是偷的)一般在压缩源文件数据区全局方式位标记处,真加密为 09 00,伪加密为00 00,而后面将压缩源文件目录区全局方式 ...
- 使用IO流将数据库中数据生成一个文件,结果使用Notepad++打开部分数据结尾出现NUL
场景描述: 项目中通过java代码中从数据库中查询一系列数据,对数据做相应处理,然后通过字符流将数据写如一个新生成的文件中,将该项目部署在linux服务器上,最后生成的文件拿到本地使用notepad+ ...
- Stm32 控制1.44寸液晶显示图片 基于stm32f051k8u6
在线图片转BMP格式:https://app.xunjiepdf.com/img2bmp 一.使用工具对图片生成字符数组 1. 使用1.44寸的液晶其像素是128*128的16位真彩,则需要把图片调整 ...
- Nodejs base64编码与解码
1、普通字符串 //编码 new Buffer(String).toString('base64'); //解码 new Buffer(base64Str, 'base64').toString(); ...
- 【CSS3】rgba与opacity
RGBA 语法 R:红色值.正整数 | 百分数 G:绿色值.正整数 | 百分数 B:蓝色值.正整数| 百分数 A:透明度.取值0~1之间 为什么要用RGBA而不用opacity 因为在项目中需要用到一 ...
- 【Luogu】【关卡2-9】带有技巧的搜索(2017年10月)
任务说明:这里的搜索不仅包含了dfs和bfs,还包括剪枝.记录等技巧以加快速度. [USACO06FEB]数字三角形Backward Digit Su… 滑雪 吃奶酪 靶形数独 P1118 [USAC ...