topic link : [Kuangbin take you to fly] topic XV DP J-Gill series story-Hate 7 no wife
Test instructions
Time limit:500ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Description
Single!
Still single!
Brother Jill is still single!
DS-Class Code The brother is still single!
So, he hates Valentine's Day, whether it's 214 or 77, he hates it!
Jill observed 214 and 77 of these two numbers, found:
2+1+4=7
7+7=7*2
77=7*11
In the end, he found out that it all boils down to 7! So he now even hates everything and 7 about the number!
What kind of numbers are related to 7?
If an integer conforms to one of the 3 conditions below, then we say that this integer is related to 7
1. One of the integers is 7;
2. Each of the integers adds up to an integer multiple of 7;
3. This integer is an integer multiple of 7;
Now the question is: Gill want to know the sum of squares of 7 unrelated numbers within a certain interval.
Input
The first line of input data is the case number T (1 <= T <= 50), then the next T-line represents the T case; Each case contains two positive integer l, R (1 <= l <= R <= 10^18) in a row.
Output
Please calculate the sum of squares of [l,r] and 7 unrelated numbers and output the result to 10^9 + 7 after modulo.
Ideas
The topic DP mentality is very good to think about, but the trouble is that the value it requires is the sum of squares of the tree that matches the criteria.
We use PRA to record the number of the front digit and%7,PRB record the value of the previous digit%7. In addition to the sifting of 7 on the Traverse, it is easy to find the number unrelated to the 7, but how does the sum of squares be calculated?
We use three variables
CNT represents the number of 7 unrelated numbers in the current state, which is easily obtained during the search process.
Sum indicates that the current state of the 7-independent number of
Then newsum = i*10^len*cnt + SUM (i is the current selected number, with CNT plus the number of CNT and that is, sum, that is, a new number and)
Sqsum represents the sum of squares of 7 unrelated numbers in the current state
(I*10^len + num) ^2 = (I*10^len) ^2 + 2*i*10^len*num + num^2;
And the square of the number of CNT is
(I*10^len) ^2*cnt + SUM (num^2) + 2*i*10^len*sum (num)
i.e. (I*10^len) ^2*cnt + sqsum + 2*i*10^len*sum.
PS: The reason that this problem is the devil, is to consider the small details too annoying, the problem needs a lot of surplus, less one will have to wrong (good elegance to write code it)
Also, because of the reason for the remainder, ansr-ansl may result in negative, so add mod to take more.
Code
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>using namespace Std;#define LL Long LongConstintMOD =1E9 +7; struct node{LL cnt; LL sum; LL sqsum; Node () {} node (ll A, ll B, ll C): CNT (a), sum (b), Sqsum (c) {}}dp[ -][7][7];intdis[ -]; LL c[ -]; Node DFS (intLenintPraintPRB, BOOL flag) {if(Len <0) {returnNode (pra!=0&& prb!=0,0,0); }if(!flag && dp[len][pra][prb].cnt! =-1)returnDP[LEN][PRA][PRB];intEnd = Flag?dis[len]:9; Node ans = node (0,0,0); for(intI=0; i<=end; i++) {if(I! =7) {Node T = DFS (len-1, (Pra+i)%7, (prb*10+i)%7, flag && i==end); ans.cnt = (ans.cnt + t.cnt)% MOD; Ans.sum + = ((C[len)*i)%mod*t. CNT)%mod+ t.sum)% MOD; Ans.sum%=MOD; Ans.sqsum + = (t.sqsum + (2*c[Len]*i)%mod*t. Sum)%mod)%mod; Ans.sqsum%=MOD; Ans.sqsum + = ((i*c[Len]*i%mod)*c[Len]%mod* t.cnt)%mod; Ans.sqsum%=MOD; } }if(!flag) dp[len][pra][prb] = ans;returnAns;} void Init () {c[0] =1; for(intI=1; i< -; i++) C[i] = (c[i-1]*10)% MOD;} ll solve (ll N) {intLen =0; while(n) {dis[len++] = n%10; N/=Ten; } Node ans; Ans = DFS (len-1,0,0,1);returnAns.sqsum;}intMain () {intT scanf"%d", &t); Init (); Memset (DP,-1, sizeof (DP)); while(t--) {LL L, R; scanf"%lld%lld", &l, &r);printf("%lld\ n", (Solve (R)-Solve (L-1+ MoD)% mod); }return 0;}
HDU 4507 series of stories-Hate 7 not wife (digital dp& good magic a good question)