[Hihocoder 1033] staggered and digital DP/Memory search

Source: Internet
Author: User
#1033: crossover and time limit: Memory ms single point time limit: 256 Ms memory limit: MB
Description

Given a number X, it is set to A0 in sequence from the decimal position to the lower position ,? A1 ,?...,? An? -? 1. Define the interlaced and function:

F (x )? =? A0? -? A1? +? A2? -?...? +? (? -? 1) n? -? 1An? -? 1

For example:

F (3214567 )? =? 3? -? 2? +? 1? -? 4? +? 5? -? 6? +? 7? =? 4

Given


Input

Only one row of input data contains three integers: l ,? R ,? K (0? ≤? L? ≤? R? ≤? 1018 ,? | K |? ≤? 100 ).

Output

Output a row of an integer to indicate the result. Considering that the answer may be large, the output result is modeled as 109? +? 7.

Prompt

For example, if the number of conditions is 110 and 121, the result is 231 = 110 + 121.

More examples:

Input
4344 3214567 3
Output
611668829
Input
404491953 1587197241 1
Output
323937411
Input
60296763086567224 193422344885593844 10
Output
608746132
Input
100 121-1
Output
120



Sample Input
100 121 0
Sample output
231

Question = _ = the question comes from the first challenge of hihocoder.

At the beginning, I thought it was a number theory topic, and later I found it was a number of DP, which is easy to hold:

1. During the memory-based search and writing, the same number and number must be staggered, and the numbers and numbers of the same staggered number must be respectively DP

2. The calculation method of one digit and two digits is different. We need to discuss the calculation method of the two digits.

3. Because the result may be relatively large, we need to use the same remainder theorem for each step to prevent the long from popping up during the operation.


The concept of memory-based search,

The sum of the current staggered and the same number (number in the to-be-searched status and + the size of the number currently searched * The number of currently searched qualified numbers ).

# Include <cstdio> # include <cstring> long mod = 1000000007; long base [20]; long l, R, K, bit [20], BT, YY; struct node {long S, N; // s represents the number and N represents the number}; node DP [20] [400]; // status transition node DFS (long POs, long target, long limit) // digital DP, which is basically a template {node T; T. S = T. n = 0; If (Pos = 0) {// process to the last bit and directly judge that the returned if (target = 100) T. n = 1; return t;} If (Limit = 0) & (DP [POS] [target]. n! =-1) return DP [POS] [target]; long tail = limit? Bit [POS]: 9; long SGN = (yy-Pos) % 2 )? (-1) :( 1); // determine the long head symbol; If (Pos = YY) Head = 1; else head = 0; // determine the start and end of the search for (INT I = head; I <= tail; I ++) {node TMP = DFS (pos-1, target-I * SGN, (Limit = 1) & (I = bit [POS]); If (TMP. n)> 0) {T. N + = TMP. n; Long Q; q = (TMP. N % mod) * base [POS]) % mod) * I) % MOD; // result Cool T. S + = (TMP. s) % MOD; T. S % = MOD; T. S + = Q; T. S % = MOD; // each step must be the same.} If (Limit = 0) DP [POS] [target] = T; return t ;} long long CAL (long X, long y) {long ans = 0; If (x =-1) return 0; If (x = 0) return 0; bt = 0; while (x) {BT ++; bit [BT] = x % 10; X/= 10;} For (yy = 1; YY <= Bt; YY ++) {memset (DP,-1, sizeof DP); ans + = DFS (YY, Y + 100, YY = BT ). s; // process the number with a length of YY ans = (ANS + mod) % MOD;} return ans;} int main () {base [1] = 1; for (INT I = 2; I <= 19; I ++) base [I] = (base [I-1] * 10) % MOD; scanf ("% LLD", & L, & R, & K); {printf ("% LLD", (CAL (R, K) -Cal (L-1, k) + mod) % mod);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.