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\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...
随机推荐
- MySQL concat、concat_ws 和 group_concat 的用法
一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为+----+ ...
- mybatis中Parameter index out of range (2 > number of parameters, which is 1).
${name} 是不带单引号的,而#{name} 是带单引号的
- 解决ubuntu 18更换wifi vnc 连接不了的问题
解决ubuntu 18更换wifi vnc 连接不了的问题 问题: ubuntu18上更换了wifi热点,windows上的vnc连接不上. 解决方法: 1.ubuntu18系统上执行“共享”——“屏 ...
- NetworkComms V3 序列化器之Protobuf.net和 JSONSerializer
NetworkComms v3版本中,默认使用的是protobuf.net序列化器. 即当您没有指定序列化的时候,系统自动使用默认的protobuf.net序列化器. 当然我们也可以自己指定序列化器 ...
- 动态规划及LCS
LCS的python实现: #!/usr/bin/env python #-*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultenco ...
- Git比较分支差异的3个命令
查看本地分支 git branch 查看远端分支,无论是否checkout到本地 git branch -r 假如想比较dev和master 命令1:比较文件 git diff dev master ...
- spark性能调优05-troubleshooting处理
1.调节reduce端缓冲区大小避免OOM异常 1.1 为什么要调节reduce端缓冲区大小 对于map端不断产生的数据,reduce端会不断拉取一部分数据放入到缓冲区,进行聚合处理: 当map端数据 ...
- docker安装及使用
一.简介 说起docker,大多数人第一时间想起来的应该有虚拟化.云计算,这三者有什么区别呢? 对比: 我们所熟知的虚拟化有KVM.xen.Hyper-V等,这些虚拟化虚拟出来的服务器会产生一个新的操 ...
- python之将Unicode文本标准化
在需要比较字符串的程序中使用字符的多种表示会产生问题. 为了修正这个问题,你可以使用unicodedata模块先将文本标准化: s1 = 'Spicy Jalape\u00f1o' s2 = 'Spi ...
- MySQL字符集不一致导致查询SQL性能问题
今天做了一个MySQL数据库中的SQL优化. 结论是关联字段字符集不同,导致索引不可用. 查询的SQL如下: select `Alias`.`Grade`, `Alias`.`id`, `Alias` ...