結果
| 問題 | No.782 マイナス進数 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-02-10 21:44:08 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 25 ms / 2,000 ms | 
| コード長 | 1,261 bytes | 
| コンパイル時間 | 1,472 ms | 
| コンパイル使用メモリ | 114,536 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-07 13:26:02 | 
| 合計ジャッジ時間 | 3,757 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 36 | 
ソースコード
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;
void solve(long long &n, int base, long long curr, long long minVal, long long maxVal, string& ans)
{
    if(minVal <= n && n <= maxVal){
        if(curr == 1)
            ans = "0";
        return;
    }
    long long curr2 = curr * base;
    long long minVal2 = minVal + min(0LL, curr * (-base - 1));
    long long maxVal2 = maxVal + max(0LL, curr * (-base - 1));
    solve(n, base, curr2, minVal2, maxVal2, ans);
    int x = 0;
    while(!(minVal <= n && n <= maxVal)){
        ++ x;
        n -= curr;
    }
    ans += (char)('0' + x);
}
int main()
{
    int t, b;
    cin >> t >> b;
    while(--t >= 0){
        long long n;
        cin >> n;
        string ans;
        solve(n, b, 1, 0, 0, ans);
        cout << ans << endl;
    }
    return 0;
}
            
            
            
        