计算可被 K 整除的范围内的数字,并在范围内计算数字之和



我有这样的任务:

如果一个数字可以被 K 整除并且其数字之和位于 [P, Q] 中,则让我们称其为"幸运"。计算 [A, B] 中"幸运"数字的数量。

1 ≤ A, B <10^11,
1 ≤ P, Q ≤ 99,
1 ≤ K ≤ 10^11。

每次测试时间 - 5 秒

这是我的代码

#include <iostream>
#include <fstream>
using namespace std;
int sum;
long long k;
int number[10];
int digits[10];
int p;
int q;
long long a;
long long b;
int delta;
bool flag = false;
long long memory[100000][100][10][2];
long long pow (int a, int b)
{
    long long res = 1;
    for(int i = 0; i < b; i++)
        res *= a;
    return res;
}
int sumOfDigits(long long number)
{
    int sum = 0;
    while(number != 0)
    {
        sum += number % 10;
        number /= 10;
    }
    return sum;
}
long long toLong(int digits[], int n)
{
    long long sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum *= 10;
        sum += digits[i];
    }
    return sum;
}
long long function(long long remainder// the remainder of division by k,
                      int currSum // current sum of digits, 
                      int n //number of digit to be added, 
                      bool allDigitsAllowed //says if all digits can be added to number)
{
    if(n == 10)
    {
        int counter = 0;
        for(int i = p; i <= q; i++)
        {
            long long res = i - currSum + remainder;
            if(res%k == 0 && (allDigitsAllowed || i-currSum <= number[9]))
            {
                counter++;
                if(!allDigitsAllowed && number[9] == i-currSum)
                    flag = true;
            }
        }
        return counter;
    }
    long long res = 0;
    int temp = 0;
    while(currSum + temp <= q)
    {   
        long long tempRemainder = (temp*pow(10,10-n)+remainder)%k;
        if(temp == number[n-1] && !allDigitsAllowed)
        {
            if(tempRemainder < 100000)
            {
                if(memory[tempRemainder][currSum+temp][n+1][false] == -1)
                    memory[tempRemainder][currSum+temp][n+1][false] = function(tempRemainder, currSum + temp, n+1,false);
                res += memory[tempRemainder][currSum+temp][n+1][false];
            }
            else
                res += function(tempRemainder, currSum + temp, n+1,false);
            //res += function(tempRemainder, currSum + temp, n+1,false);
            break;
        }
        else
        {
            if(tempRemainder < 100000)
            {
                if(memory[tempRemainder][currSum+temp][n+1][true] == -1)
                    memory[tempRemainder][currSum+temp][n+1][true] = function(tempRemainder, currSum + temp, n+1,true);
                res += memory[tempRemainder][currSum+temp][n+1][true];
            }
            else
                res += function(tempRemainder, currSum + temp, n+1,true);
        }
        temp++;
    }
    return res;
}
long long F (long long a)
{
    flag = false;
    memset(&number, 0, sizeof(number));
    sum = 0;
    int i = 9;
    while(a != 0)
    {
        number[i] = a%10;
        a /= 10;
        i--;
    }
    for(int j = 0; j < 10; j++)
        swap(number[j],number[9-j]);
    return function(0,0,1,false);
}
int main()
{
    ifstream in("lucky.in");
    ofstream out("lucky.out");
    in >> k >> p >> q >> a >> b;
    sum = p;
    memset(&memory, -1, sizeof(memory));
    long long result = - F(a) + flag;
    memset(&memory, -1, sizeof(memory));
    out<< result + F(b) << endl;
    return 0;
}

在这个解决方案中,我尝试用确定的数字总和制作一个数字,逐个添加数字。问题是我无法记住除以 k 的所有其余部分。

那么我该如何处理这个问题呢?

我会使用类似的东西:

unsigned int compute_number_sum(uint64_t n)
{
    unsigned int res = 0;
    while (n != 0) {
        const uint64_t nd = n / 10;
        res += static_cast<unsigned int>(n - 10 * nd); // equivalent to res += n % 10
        n = nd;
    }
    return res;
}
uint64_t count_lucky_number(uint64_t A, uint64_t B, uint64_t K,
                             unsigned int P, unsigned int Q)
{
    uint64_t res = 0;
    const uint64_t min_i = (A + K - 1) / K * K;
    for (uint64_t i = min_i; i <= B; i += K) {
        const unsigned int numberSum = compute_number_sum(i);
        res += P <= numberSum && numberSum <= Q;
    }
    return res;
}

相关内容

  • 没有找到相关文章

最新更新