AtCoder Beginner Contest 336 C - Even Digits题解及做题分享(1)
距离创建这个博客的时间也不久了,到今天我才终于在洛谷写下我的第一篇文章,分享我的寒假训练经历。
本题来自 AtCoder Beginner Contest 336 C - Even Digits。提交三次wa 1,tle 1, AC!!!
ok,话不多说,原题如下:
C - Even Digits
Time Limit: 2 sec / Memory Limit: 1024 MB
Score:
300 points
Problem Statement
A non-negative integer
n is called a good integer when it satisfies the following condition:
All digits in the decimal notation of
n are even numbers (
0,
2,
4,
6, and
8).
For example,
0,
68, and
2024 are good integers.
You are given an integer
N. Find the
N-th smallest good integer.
Constraints
1≤N≤10
12
N is an integer.
Sample Input 1
8
Sample Output 1
24
The good integers in ascending order are
0,2,4,6,8,20,22,24,26,28,….
The eighth smallest is
24, which should be printed.
Sample Input 2
133
Sample Output 2
2024
Sample Input 3
31415926535
Sample Output 3
2006628868244228
本人一开始思路为尝试打表,但一算时间复杂度发现是我想得太简单了,于是开始找规律,通过此题样例可以发现数据与五进制有什么不可告人的关系,但苦于能力有限并没有证明出来,但是通过样例不难直接发现规律。
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n;
scanf("%lld", &n);
n--;
int ans = 0, l = 1;
while (n > 0) {
int m = n % 5;
ans += l * 2 * m;
n /= 5;
l *= 10;
}
cout << ans << endl;
return 0;
}