Problem Description
题目大意:快乐数是由" 2 , 3 , 6 2,3,6 2,3,6"组成的数字,且不能包含其他的数字,求第 n n n个快乐数。
自信满满的写了个三进制…发发现样例都过不了,然后发现。。。跟三进制没啥关系。
枚举一下前几个快乐数,发现最后一位是 236 236 236循环,有第二位的数字开始算,第二位是 222333666 222333666 222333666,即为 3 1 3^1 31个 2 2 2、 3 1 3^1 31个 3 3 3、 3 1 3^1 31个 6 6 6循环,第三位的 222222222333333333666666666 222222222333333333666666666 222222222333333333666666666就是 3 2 3^2 32个 2 2 2、 3 2 3^2 32个 3 3 3、 3 2 3^2 32个 6 6 6循环…以此类推循环。
Accepted Code
#include
#define int long long
using namespace std;
const int N = 1e5 + 5;
int a[N] = {6, 2, 3, 6}, b[N];
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n; cin >> n;
int base = 3, tot = 0;
while (n > 0){
int now = (n - 1) % base + 1;
now = (now + (base / 3) - 1) / (base / 3);
b[++tot] = a[now];
n -= base;
base *= 3;
}
for (int i = tot; i >= 1; i--) cout
关注
打赏
- 回坑记之或许是退役赛季?
- [LCT刷题] P1501 [国家集训队]Tree II
- [LCT刷题] P2147 洞穴勘测
- 2022-2023 ICPC Brazil Subregional Programming Contest VP记录
- [线段树套单调栈] 2019-2020 ICPC Asia Hong Kong Regional Contest H.[Hold the Line]
- The 2021 ICPC Asia Nanjing Regional Contest E.Paimon Segment Tree 区间合并线段树/维护矩阵乘法
- CF580E - Kefa and Watch 线段树维护哈希
- HDU5869 Different GCD Subarray Query 离线查询/区间贡献
- 27.CF1004F Sonya and Bitwise OR 区间合并线段树
- 26.CF1000F One Occurrence