Advertisement
Ahmed_Negm

Untitled

Mar 3rd, 2023
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. ll fast_pow(ll base,ll power){
  36.     ll res = 1;
  37.     while(power){
  38.         if(power&1) res = (res*base);
  39.         base = (base*base);
  40.         power >>= 1;
  41.     }
  42.     return res;
  43. }
  44.  
  45.  
  46. void solve(){
  47. ll n,k; cin>>n>>k;
  48. if(k==1){
  49.     cout<<n<<nl;
  50.     return;
  51. }
  52. vector<ll> v;
  53. for(int i=0;i<n;i++){
  54.     ll tmp = fast_pow(k,i);
  55.     if( tmp>2*n) break;
  56.     v.push_back(tmp);
  57. }
  58. ll ans = 0;
  59. for(int i=0;i<sz(v);i++){
  60.     if(v[i]<=n) ans+=(v[i]/2)+1;
  61.     else{
  62.         ll tmp = n - (v[i]-n);
  63.         ans+=(tmp/2)+1;
  64.     }
  65. }
  66. cout<<ans<<nl;
  67.  
  68.  
  69.  
  70.  
  71. }
  72.  
  73. int main(){
  74.     Fast_IO();
  75. int t =1;
  76. //cin>>t;
  77. while(t--){
  78. solve();
  79. }
  80. return 0;
  81. }  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement