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\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...
随机推荐
- 百度上有个最难数独, 用python跑它
直接上代码 #!/usr/bin/python3 #coding=GB2312 import tkinter as tk import threading import time import ran ...
- generate ascii table
$ cat ascii.sh dec_count=0 while [ $dec_count -lt 256 ] do echo -e "\x$(echo "ibase=10;oba ...
- 力扣算法——138CopyListWithRandomPointer【M】
A linked list is given such that each node contains an additional random pointer which could point t ...
- composer 配置镜像
阿里云镜像:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 腾讯云镜像:composer ...
- [轉]udp_sendmsg空指针漏洞分析 by wzt
udp_sendmsg空指针漏洞分析 by wzt 漏洞描述: 由于Linux ipv4协议栈中udp_sendmsg()函数设计上存在缺陷, 导致struct rtable *rt以空指针形式 ...
- 晒订单赢图灵图书,《第一行代码——Android》福利活动劲爆来袭!
版权声明:本文出自郭霖的博客,转载必须注明出处. https://blog.csdn.net/sinyu890807/article/details/28863515 (已结束) 我的著作<第一 ...
- springboot 配置文件的加载顺序
springboot 配置文件的加载顺序1.在命令行中传入的参数.2. SPRING APPLICATION JSON中的属性. SPRING_APPLICATION—JSON是以JSON格式配置在系 ...
- aiohttp上报405: Method Not Allowed
请求方式不对,修改为“POST”或者“GET” 可参考:https://blog.csdn.net/yiifaa/article/details/80928487
- openses机制
openses 大体结构解读 页面html与相关php文件的定位 点击界面,查看出现的url 例如: http://localhost/opensns/index.php?s=/people/inde ...
- python re.I compile search
import restring = "The quick brown fox jumps over the lazy dog."a_list = string.split()pat ...