codeforces-1249

1.题目引入:

The only difference between easy and hard versions is the maximum value of nn.

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 nn.

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 nn find such smallest mm (n≤mn≤m) that mm is a good number.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤5001≤q≤500) — the number of queries. Then qqqueries follow.

The only line of the query contains one integer nn (1≤n≤1041≤n≤104).

Output

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

2.样例输出: 

Input

7
1
2
6
13
14
3620
10000

Output

1
3
9
13
27
6561
19683

题目大意: 给定一个数,将这个数用 3^n+……表示,如果出现重复则需要寻找符合条件且比它稍大的数,如果符合直接输出即可。

3.代码如下: 

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
typedef long long ll;
const int maxn=100010;
ll a[maxn],t;
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    cin>>t;
    while(t--)
    {
        ll n,flag=-1,ans=0,num=1;
        memset(a,0,sizeof(a));
        cin>>n;
        ll n1=n,k=0;
        while(n1)
        {
            a[k++]=n1%3;
            n1/=3;
        }
        for(int i=0; i<k; i++)
        {
            if(a[i]>=2)  //如果不满足 
            {
                flag=i;  //标记它的原始位置
		//下面这两步操作是为了寻找比它稍大的符合条件的数 
		//同时这也完美避过了更高位因++后导致 a[i] 大于等于 2的错误 
                a[i]=0;  
                a[i+1]++; 
            }
        }
        //为了最小的数 将最高标记位之后每位都归 0 即可 
        if(flag!=-1) 
        {
        	for(int i=0; i<flag; i++)
            a[i]=0;
		}   
        for(int i=0; i<=k; i++)
        {
            ans+=num*a[i];
            num*=3;
        }
        cout<<ans<<"\n";
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风遥~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值