easy_hard求3的不同幂和的最好数*

##easy_hard求3的不同幂和的最好数
Good Numbers
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. 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 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
30 is a good number: 30=33+31,
1 is a good number: 1=3^0,
12 is a good number: 12=32+31,
but 2 is not a good number: you can’t represent it as a sum of distinct powers of 3 (2=3^ 0+3^ 0),
19 is not a good number: you can’t represent it as a sum of distinct powers of 3 (for example, the representations 19=3^ 2+3^ 2+3^ 0=3 ^2+3 ^1+3 ^1+3 ^1+3 ^0 are invalid),
20 is also not a good number: you can’t represent it as a sum of distinct powers of 3 (for example, the representation 20=3 ^2+3 ^2+3 ^0+3 ^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n≤m) that m is a good number.
You have to answer q independent queries.

Input
The first line of the input contains one integer q (1≤q≤500) — the number of queries. Then q queries follow.
The only line of the query contains one integer n (1≤n≤10^4).

Output
For each query, print such smallest integer m (where n≤m) that m is a good number.

Example

Input

7
1
2
6
13
14
3620
10000
Output

1
3
9
13
27
6561
19683

**转载:**https://blog.csdn.net/xybdx/article/details/102846099

题目大意:找到>=n的最小的数,这个数是Good number。
也就是将这个数分解成3的幂的和(但是幂不能相等)。。。
30=33+31(1和3不等);YES
2=30+30(0==0);NO

easy版本
思路:增加一位最高位设为0,用来避免当前位数的3进制数无法满足要求,
然后从最高位向下寻找,如果为0或者1,跳过;
遇到2,说明不满足要求,
我们要增大数字,令该位置及更低位的数字都为0,并向上寻找,如果为2或者1,就令其为0;
否则遇到0(遇到的第一个0改为1就结束),令其为1;这样得到的就是满足条件的最小值。

hard版本
思路:从3的0次方,1次方,一直加.一直加到第一个大于n的sum.
然后对于sum,去减前面所加过的数,看看是否存在比这个sum要小但满足>=n的情况.
正向思维一般容易超时,逆向思维反而不会,所以要会换个角度思考问题

例:14
30+31+32+33=40
1.40-27=13<14 说明3^3必须存在
2.40-9=31>14 说明删去3^2依然满足
3.31-3=28>14 说明再删去3^1依然满足
4.28-1=27>14 说明再删去3^0还满足
最后只留下3^3=27
为什么可以这样呢,那是因为
对于数列1,3,9,27,81…一定满足:an>S(n-1)
可用贪心的思维考虑:
先得到一个>=n的最小好数字,然后再通过减去3的幂(大)使最好数变小

//easy版本
#include<stdio.h>
#include<math.h>
int a[10];
int len;
void Transformation_System_3(int x)
{
	while(x)
	{
		a[len++]=x%3;
		x/=3;
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		len=0;
		int n,i,j,flag=0;
		scanf("%d",&n);
		Transformation_System_3(n);
		a[len]=0;
		for(i=len;i>=0;i--)
		{
			if(a[i]==2)
			{
				a[i]=0;
				for(j=i+1;j<=len;j++)
				{
					if(a[j]==1)
						a[j]=0;
					else
					{
						a[j]=1;
						break;
					}
				}
				for(j=i-1;j>=0;j--)
					a[j]=0;
			}
		}
		int ans=0;
		for(i=len;i>=0;i--)
				ans+=a[i]*pow(3,i);
		printf("%d\n",ans);
	}
	return 0;
}
//hard版本
#include<stdio.h>
typedef long long ll;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll n;
		scanf("%lld",&n);
		ll sum=0,x=1;b
		while(sum<n)
		{
			sum+=x;
			x*=3;
		}
		while(x)
		{
			if(sum-x>=n)
				sum-=x;
			x/=3;
		}
		printf("%lld\n",sum);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值