Submission #2703634


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair


ll f(ll b, ll n) {
    ll sigma = 0;
    while (n) {
        sigma += n % b;
        n /= b;
    }
    return sigma;
}

ll solve(ll n, ll s) {
    if (n < s) return -1LL;
    if (n == s) return n+1LL;
    // if (s == 1) return n;

    ll sq = sqrt(n);
    for (ll b=2; b<=sq; ++b) {
        if (f(b,n) == s) return b;
    }

    // q + r = s
    // qb + r = n
    // 2桁なので q<√n
    ll bmin = n+1LL;
    for (ll q=1; q<=sq; ++q) {
        ll r = s - q;
        if ((n - r) % q == 0) {
            ll b = (n-r) / q;
            if (f(b,n) == s) {
                bmin = min(bmin, b);
            }
        }
    }

    return (bmin == n+1LL) ? -1 : bmin;
}

int main() {
    ll n, s;
    cin >> n;
    cin >> s;
    cout << solve(n, s) << endl;
    return 0;
}

Submission Info

Submission Time
Task D - 桁和 / Digit Sum
User naoya_t
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2031 Byte
Status
Exec Time 15 ms
Memory 256 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask1_87654_30.txt, subtask1_87654_138.txt, subtask1_87654_45678.txt, subtask1_31415926535_1.txt, subtask1_1_31415926535.txt
All 500 / 500 subtask1_100000000000_1.txt, subtask1_100000000000_100000000000.txt, subtask1_100000000000_2.txt, subtask1_100000000000_3.txt, subtask1_100000000000_50000000000.txt, subtask1_100000000000_50000000001.txt, subtask1_100000000000_99999999999.txt, subtask1_16983563041_1.txt, subtask1_1_1.txt, subtask1_1_2.txt, subtask1_1_31415926535.txt, subtask1_239484768_194586924.txt, subtask1_2_1.txt, subtask1_2_2.txt, subtask1_31415926535_1.txt, subtask1_49234683534_2461734011.txt, subtask1_4_1.txt, subtask1_58640129658_232122496.txt, subtask1_68719476735_35.txt, subtask1_68719476735_36.txt, subtask1_68719476735_37.txt, subtask1_68719476736_1.txt, subtask1_68719476736_2.txt, subtask1_72850192441_16865701.txt, subtask1_79285169301_27.txt, subtask1_82914867733_1676425945.txt, subtask1_8594813796_75700.txt, subtask1_87654_12345.txt, subtask1_87654_138.txt, subtask1_87654_30.txt, subtask1_87654_4294967308.txt, subtask1_87654_45678.txt, subtask1_97822032312_49157112.txt, subtask1_98750604051_977728851.txt, subtask1_99999515529_1.txt, subtask1_99999515529_316226.txt, subtask1_99999515529_316227.txt, subtask1_99999515529_316228.txt, subtask1_99999515529_49999757765.txt, subtask1_99999515529_49999757766.txt, subtask1_99999515530_2.txt, subtask1_99999999977_1.txt, subtask1_99999999977_2.txt, subtask1_99999999977_49999999989.txt, subtask1_99999999977_49999999990.txt, subtask1_99999999999_1.txt, subtask1_99999999999_100000000000.txt
Case Name Status Exec Time Memory
subtask1_100000000000_1.txt 1 ms 256 KB
subtask1_100000000000_100000000000.txt 1 ms 256 KB
subtask1_100000000000_2.txt 15 ms 256 KB
subtask1_100000000000_3.txt 15 ms 256 KB
subtask1_100000000000_50000000000.txt 15 ms 256 KB
subtask1_100000000000_50000000001.txt 15 ms 256 KB
subtask1_100000000000_99999999999.txt 15 ms 256 KB
subtask1_16983563041_1.txt 1 ms 256 KB
subtask1_1_1.txt 1 ms 256 KB
subtask1_1_2.txt 1 ms 256 KB
subtask1_1_31415926535.txt 1 ms 256 KB
subtask1_239484768_194586924.txt 2 ms 256 KB
subtask1_2_1.txt 1 ms 256 KB
subtask1_2_2.txt 1 ms 256 KB
subtask1_31415926535_1.txt 9 ms 256 KB
subtask1_49234683534_2461734011.txt 11 ms 256 KB
subtask1_4_1.txt 1 ms 256 KB
subtask1_58640129658_232122496.txt 11 ms 256 KB
subtask1_68719476735_35.txt 5 ms 256 KB
subtask1_68719476735_36.txt 1 ms 256 KB
subtask1_68719476735_37.txt 12 ms 256 KB
subtask1_68719476736_1.txt 1 ms 256 KB
subtask1_68719476736_2.txt 1 ms 256 KB
subtask1_72850192441_16865701.txt 13 ms 256 KB
subtask1_79285169301_27.txt 1 ms 256 KB
subtask1_82914867733_1676425945.txt 13 ms 256 KB
subtask1_8594813796_75700.txt 3 ms 256 KB
subtask1_87654_12345.txt 1 ms 256 KB
subtask1_87654_138.txt 1 ms 256 KB
subtask1_87654_30.txt 1 ms 256 KB
subtask1_87654_4294967308.txt 1 ms 256 KB
subtask1_87654_45678.txt 1 ms 256 KB
subtask1_97822032312_49157112.txt 15 ms 256 KB
subtask1_98750604051_977728851.txt 15 ms 256 KB
subtask1_99999515529_1.txt 12 ms 256 KB
subtask1_99999515529_316226.txt 15 ms 256 KB
subtask1_99999515529_316227.txt 15 ms 256 KB
subtask1_99999515529_316228.txt 15 ms 256 KB
subtask1_99999515529_49999757765.txt 15 ms 256 KB
subtask1_99999515529_49999757766.txt 15 ms 256 KB
subtask1_99999515530_2.txt 11 ms 256 KB
subtask1_99999999977_1.txt 15 ms 256 KB
subtask1_99999999977_2.txt 15 ms 256 KB
subtask1_99999999977_49999999989.txt 15 ms 256 KB
subtask1_99999999977_49999999990.txt 15 ms 256 KB
subtask1_99999999999_1.txt 15 ms 256 KB
subtask1_99999999999_100000000000.txt 1 ms 256 KB