Horse Races


2180: Horse Races

Time Limit: 2 Sec   Memory Limit: 256 MB
Submit: 2   Solved: 1
[ Submit][ Status][ Web Board]

Description

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya likes horse racing very much. Horses numbered from l to r take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the distance between which does not exceed k. Petya learned from some of his mates from Lviv that lucky digits are digits 4 and 7. The distance between the digits is the absolute difference between their positions in the number of a horse. For example, if k=2, then numbers 4123954974044070400000070004007 are nearly lucky and numbers 441239549974007000040070004007 are not.

Petya prepared t intervals [li,ri] and invented number k, common for all of them. Your task is to find how many nearly happy numbers there are in each of these segments. Since the answers can be quite large, output them modulo 1000000007 (109+7).

Input

The first line contains two integers t and k (1≤t,k≤1000) − the number of segments and the distance between the numbers correspondingly. Next t lines contain pairs of integers li and ri (1≤lr≤101000). All numbers are given without the leading zeroes. Numbers in each line are separated by exactly one space character.

Output

Output t lines. In each line print one integer − the answer for the corresponding segment modulo 1000000007 (109+7).

Examples
Input
1 2
1 100
Output
4
Input
1 2
70 77
Output
2
Input
2 1
1 20
80 100
Output
0
0
Note

In the first sample, the four nearly lucky numbers are 44, 47, 74, 77.

In the second sample, only 74 and 77 are in the given segment.

【analysis】

The meaning of the question is very simple. Find out the number of digits whose interval between 4 and 4, 7 and 7, 4 and 7 is less than k in a given interval.

Digital dp... is to judge a little bit longer, and finally judge whether l is the answer, and add it. Generally speaking, it won’t be t because as long as the interval that meets the conditions is found, it is directly marked as true. No need to count 4 and 7

【Code】

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
const int maxn = 1000 + 5;
int dp[maxn][maxn][2];
int bits[maxn];

int t,k;

ll dfs(int len,int lpos,int fg,bool border)
{
    if(!len) return fg == 1;
    if(!border && dp[len][lpos][fg] != -1) return dp[len][lpos][fg];
    int up = border ? bits[len] : 9;
    ll res = 0 ;
    for (int i = 0; i <= up; i ++) {
        if(i == 4 || i == 7)
        {
            res += dfs(len - 1, len, (lpos && lpos - len <= k) | fg, border && i == up);
        }
        else res += dfs(len - 1, lpos, fg, border && i == up);
    }
    res %= mod;
    if(!border) dp[len][lpos][fg] = res;
    return res;
}
ll f(string s)
{
    int len = 0;
    for (int i = s.size() - 1; i >= 0; i --) {
        bits[++ len] = s[i] - '0';
    }
    return dfs(len,0,0,1);
}
bool check(string &s)//
{
    int p = 0;
    for (int i = 1; i <= s.size(); i ++) {
        if (s[i - 1] == '4' || s[i - 1] == '7') {
            if(!p || i - p > k) p = i;
            else if(i - p <= k) return true;
        }
    }
    return false;
}

int main()
{
    cin >> t >> k;
    string l,r;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < t; i ++) {
        cin >> l >> r;
        ll ans = f(r) - f(l) + (check(l) ? 1 : 0);
        cout << (ans % mod + mod) % mod << endl;//
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/jnxxhzz/article/details/80957297