计算范围内的数字,可以用K除以范围内的数字之和

时间:2014-04-06 14:00:25

标签: c++ algorithm dynamic-programming

我有这样的任务:

如果它可以被K整除,那么我们称一个数字为“幸运”,其数字之和位于[P,Q]。计算[A,B]中“幸运”数字的数量。

1≤A,B <1。 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来记住除法的所有剩余部分。

那我怎么处理这个问题呢?

1 个答案:

答案 0 :(得分:2)

我会使用类似的东西:

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;
}