934D - A Determined Cleanup

思路:

找规律,和k进制的求法差不多,答案的奇数位是p%k,偶数位如果p%k!=0,那么答案是k-p%k,否则为0。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=2e3+5;
vector<int>ans;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll p,k;
    cin>>p>>k;
    int t=0;
    while(p){
        if(t%2==0)ans.pb(p%k),p/=k;
        else{
            if(p%k)ans.pb(k-p%k),p=p/k+1;
            else ans.pb(0),p=p/k;
        }
        t++;
    }
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)cout<<ans[i]<<' ';
    cout<<endl;
    return 0;
} 

 

相关文章:

  • 2022-12-23
  • 2021-07-08
  • 2022-02-08
  • 2022-03-01
  • 2021-11-27
  • 2022-02-12
  • 2022-02-05
猜你喜欢
  • 2021-07-24
  • 2022-12-23
  • 2021-06-06
  • 2021-10-28
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案