Skip to content

Instantly share code, notes, and snippets.

@yurahuna
Created November 3, 2016 14:38
Show Gist options
  • Save yurahuna/0b59a6d599dd0e11f6c841b9a8b96e55 to your computer and use it in GitHub Desktop.
Save yurahuna/0b59a6d599dd0e11f6c841b9a8b96e55 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(_v) for(auto _x:_v){cout<<_x<<" ";}cout<<endl
#define printVS(_vs) for(auto _x : _vs){cout << _x << endl;}
#define printVV(_vv) for(auto _v:_vv){for(auto _x:_v){cout<<_x<<" ";}cout<<endl;}
#define printP(_p) cout << _p.first << " " << _p.second << endl
#define printVP(_vp) for(auto _p : _vp) printP(_p);
typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;
// [1, x]に含まれる3でも5でも割り切れない整数の個数
int f(int x) {
int n3 = x / 3;
int n5 = x / 5;
int n15 = x / 15;
return x - n3 - n5 + n15;
}
// 数xまで書いたときの文字列の長さ
int calc(int x) {
int ret = 0;
ret += 4 * (x / 3 + x / 5);
int a = 0;
int b = 9;
rep(i, 100) {
bool finish = false;
if (x <= b) {
b = x;
finish = true;
}
// [a, b]に含まれる3でも5でも割り切れない整数の個数
int m = f(b) - f(a);
ret += (i + 1) * m;
if (finish) {
return ret;
}
a = b + 1;
b = 10 * a - 1;
}
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
int l = 0, r = 1e18;
while (r - l > 1) {
int m = (l + r) / 2;
(calc(m) < n ? l : r) = m;
}
string s;
rep2(x, l + 1, l + 100) {
if (x % 3 == 0) s += "Fizz";
if (x % 5 == 0) s += "Buzz";
if (x % 3 != 0 && x % 5 != 0) s += to_string(x);
}
cout << s.substr(n - calc(l) - 1, 20) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment