2021牛客多校9 H Happy Number

131 篇文章 0 订阅

题目

链接:https://ac.nowcoder.com/acm/contest/11260/H
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

Digits 2, 3 and 6 are happy, while all others are unhappy. An integer is happy if it contains only happy digits in its decimal notation. For example, 2, 3, 263 are happy numbers, while 231 is not.

Now Cuber QQ wants to know the n-th happy positive integer.

输入描述:

The first and only line of input contains a positive integer n (1≤n≤1091\le n\le 10^91≤n≤109).

输出描述:

The first and only line of output contains the n-th happy positive integer.

示例1

输入

[复制](javascript:void(0)😉

680

输出

[复制](javascript:void(0)😉

326623

解释与代码

我们可以观察数字规律

2 3 6
22
23
26
32
33
36
62
63
66
222
223
226
232
233
236

我们可以发现,最后一位是236236循环,有第二位的数字开始算,第二位是3个2,3个3,3个6,有第三位的开始算,第三位就是9个2,9个3,9个6…以此类推

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define case        ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
#define _           0
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI    = acos(-1.0);
const double eps   = 1e-6;
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;
const int    maxn  = 1e9+10;
const ll     N     = 5;

ll arr[30];
int sw[30];
int p[30] = {6, 2, 3};



void solve(){
	arr [0] = 0, arr[1] = 3; 
	for (int i=2; i<=20; i++) arr[i] = arr[i-1]*3;
	for (int i=2; i<=20; i++) arr[i] += arr[i-1];
	int n, cnt, res, ans = 0;
	cin>>n;
	for (int i=1; i<=20; i++) {
		if (arr[i-1]<n && n<=arr[i]) {
			cnt = i;//cnt位 
			res = n-arr[i-1];//cnt位下第res个 
			break;
		}
	}
	
	for (int i=1; i<=cnt; i++) {
		sw[i] = p[(int)ceil((double)res/(double)(pow(3,i-1)))%3];
	}
	
	for (int i=cnt; i>=1; i--) cout<<sw[i];
	
	cout<<endl;
}

int main()
{
//	TIE;
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
	solve();
//    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
	return ~~(0^_^0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值