论文:

http://hi.baidu.com/3xianbin/item/917aca907a3fb6f4291647fc

http://wenku.baidu.com/view/d2414ffe04a1b0717fd5dda8.html

其中一道题目求给定区间的所有数的k进制的各位数字的和:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)
#define ll __int64
#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("din.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);


#define M 137
#define N 207

using namespace std;


const int inf = 0x7f7f7f7f;
const int mod = 1000000007;

ll x,y,n,k;
ll getsum(ll pre,ll n,ll k)
{
    ll C = pre, B = 1;

    for (int i = 0; i < n; ++i)
    {
        B *= k; C *= k;
    }
    B = B*n*(k - 1)/2;
    return B + C;
}
ll dfs(ll pre,ll n,ll k)
{
    ll res = 0;
    if (n < k)
    {
        for (int i = 0; i <= n; ++i)
        {
            res += pre + i;
        }
        return res;
    }

    int d = 0;
    ll t = 1;
    ll tn = n;
    while (tn >= k)
    {
        tn /= k;
        t *= k;
        d++;
    }
    for (int i = 0; i < tn; ++i)
    {
        res += getsum(pre + i,d,k);
    }
    res += dfs(pre + tn,n - tn*t,k);
    return res;
}
int main()
{
    while (cin>>x>>y>>k)
    {
        ll sum1 = dfs(0,y,k);
        ll sum2 = dfs(0,x - 1,k);
        cout<<sum1<<" "<<sum2<<" "<<sum1 - sum2<<endl;
    }
    return 0;
}
View Code

相关文章:

  • 2021-05-30
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2021-04-14
  • 2022-01-17
  • 2021-12-02
猜你喜欢
  • 2022-01-19
  • 2022-01-25
  • 2022-01-12
  • 2021-10-15
  • 2022-12-23
  • 2022-02-27
  • 2021-11-26
相关资源
相似解决方案