Codeforces Round #462 (Div. 2) D - A Determined Cleanup “消元”

思路:

按题意模拟一下,可以知道目的就是“消元”;消去系数大于等于k的项的时候,将余数放入答案序列;


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
#define kuaidian ios::sync_with_stdio(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 7, maxd = 1e6 + 7;
const int mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;


ll p, k;
vector ans;

int main() {
    //cout << (2018/214) << " ++ " << (2018%214) << endl;
    //cout << ((-5)/2) << " ++ " << ((-5)%2) << endl;
    scanf("%lld %lld", &p, &k);
    ll t = p;
    while(1) {
        if(t > 0 && t < k) {
            ans.push_back(t);
            break;
        }
        else if(t == 0) {
            break;
        }
        else if(t >= k) {
            ans.push_back(t%k);
            t /= k;
            t *= -1;
        }
        else {
            if(t % k != 0) {
                ans.push_back((t%k)+k);
                t /= k;
                t *= -1; ++t;
            }
            else {
                ans.push_back(0);
                t /= k;
                t *= -1;
            }
        }
    }
    printf("%d\n", ans.size());
    for(auto i : ans) {
        printf("%lld ", i);
    }

    return 0;
}














你可能感兴趣的:(codefoces)