[Loj #10168. "One book 5.3 exercise 3" hate 7 not to become a wife] solution

Posted by Garion on Thu, 13 Jan 2022 07:48:40 +0100

Title Link

subject

single!
Still single!
GIGO is still single!
DS code Nong Jige is still single!
Therefore, he hates Valentine's day most in his life. Whether it's \ (214 \) or \ (77 \), he hates it!
GIGO observed the two numbers \ (214 \) and \ (77 \), and found that:
\(2+1+4=7\)
\(7+7=7×2\)
\(77=7 × 11\)
Finally, he found that all this was due to \ (77 \) in the final analysis! So now he even hates all the numbers related to \ (7 \)!
What kind of number is related to \ (7 \)? If an integer meets one of the following three conditions, we say that the integer is related to \ (7 \):
A bit in an integer is \ (7 \);
The sum of each bit of an integer is an integer multiple of \ (7 \);
This integer is an integer multiple of \ (7 \).
Now the problem comes: GIGO wants to know the sum of squares of numbers unrelated to \ (7 \) in a certain interval.

thinking

Put aside the sum of squares, this is an ordinary digital dp.

But this question has a square sum!

First, let's assume that the current number is \ (\ outline {AB} \), \ (a \) is the first digit in our digit dp enumeration, and \ (b \) is the following number. Let's assume that there are \ (k \) bits, and then let's find the sum of squares of \ (\ outline {AB} \) under the possible values of all \ (b \) when \ (a \) is determined, and record it as \ (s {\ outline {AB} \)

First, we use the digit dp to find a \ (F_b \) to represent the number of schemes of \ (B \), so the above problem can be transformed into:

\[\Large S_{\overline{ab}}=\sum^{F_b}(\overline{ab})^2 \]

Then you can happily start simplification.

First, turn \ (\ outline {AB} \) into a mathematical formula:

\[\Large S_{\overline{ab}}=\sum^{F_b}(a\times 10^k+b)^2 \]

The square sum formula follows:

\[\Large S_{\overline{ab}}=\sum^{F_b}(a^2\times 10^{2\times k}+2\times a\times 10^k\times b+b^2) \]

Insert \ (\ sum^{F_b} \):

\[\Large S_{\overline{ab}}=F_b\times a^2\times 10^{2\times k}+2\times a\times 10^k\times \sum^{F_b}b+\sum^{F_b}b^2 \]

Make it look better:

\[\Large S_{\overline{ab}}=F_ba^210^{2k}+2\times10^ka\sum^{F_b}b+\sum^{F_b}b^2 \]

Obviously \ (\ sum^{F_b}b^2=S_b \), because this is the definition of \ (S \):

\[\Large S_{\overline{ab}}=F_ba^210^{2k}+2\times10^ka\sum^{F_b}b+S_b \]

Once you see this formula, there is still \ (\ sum^{F_b}b \) unknown. What should you do? Let's set \ (T_b=\sum^{F_b}b \).

So the formula becomes this:

\[\Large S_{\overline{ab}}=F_ba^210^{2k}+2\times10^kaT_b+S_b \]

Then let's push \ (t {\ outline {AB} \).

\[\Large T_{\overline{ab}}=\sum^{F_b}\overline{ab} \]

Remove \ (\ outline {ab} \):

\[\Large T_{\overline{ab}}=\sum^{F_b}(a\times 10^k+b) \]

Multiply \ (\ sum^{F_b} \) by:

\[\Large T_{\overline{ab}}=F_ba10^k+\sum^{F_b}b \]

Then we found that \ (\ sum^{F_b}b \) is not \ (T_b \)? Then put it in:

\[\Large T_{\overline{ab}}=F_ba10^k+T_b \]

Then the problem was solved.

summary

This question is really a good one.

Good digital dp + push.

The process of pushing this question is a little annoying, but I also understand that the process of digital dp is essentially recursive. Consider yourself at each step, and the answer is right.

Code

// Problem: 1586: [example 2] Digital Games
// Contest: SSOIER
// URL: http://ybt.ssoier.cn:8088/problem_show.php?pid=1586
// Memory Limit: 524 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
#define mo 1000000007
//#define N
int n, m, i, j, k; 
int x, y, a[30], ten[10010]; 
int f[30][10][10][2]; 
int s[30][10][10][2]; 
int t[30][10][10][2]; 

int dfs(int n, int sum, int x, int p)
{
	if(n==0) return f[n][sum][x][p]=((sum%7>0 && x%7>0) ? 1 : 0); 
	int i, k=0, F, S, T; 
	if(f[n][sum][x][p]!=-1) return f[n][sum][x][p]; 
	for(i=0; i<=9; ++i)
	{
		if(!p && i>a[n]) break; 
		if(i==7) continue; 
		k+=dfs(n-1, (sum+i)%7, (x*10+i)%7, p|(i<a[n])); k%=mo;  
		F=f[n-1][(sum+i)%7][(x*10+i)%7][p|(i<a[n])]; 
		S=s[n-1][(sum+i)%7][(x*10+i)%7][p|(i<a[n])]; 
		T=t[n-1][(sum+i)%7][(x*10+i)%7][p|(i<a[n])]; 
		t[n][sum][x][p]+=(ten[n-1]*i%mo*F%mo+T)%mo; 
		// printf("t[%lld][%lld][%lld][%lld]+=ten[%lld]*%lld*%lld+%lld\n", n, sum, x, p, n-1, i, F, T); 
		s[n][sum][x][p]+=(ten[n-1]*ten[n-1]%mo*i%mo*i%mo*F%mo+ten[n-1]*i%mo*T%mo*2%mo+S)%mo; 
		// printf("%lld %lld %lld %lld\n", n, S, T, s[n][sum][x][p]); 
		t[n][sum][x][p]%=mo; s[n][sum][x][p]%=mo; 
	}
	return f[n][sum][x][p]=k; 
}

int calc(int x)
{
	n=0; 
	memset(f, -1, sizeof(f)); 
	memset(s, 0, sizeof(s)); 
	memset(t, 0, sizeof(t)); 
	while(x) a[++n]=x%10, x/=10; 
	dfs(n, 0, 0, 0); 
	// printf("> %lld\n", s[n][0][0][0]); 
	return s[n][0][0][0]; 
}

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	for(i=ten[0]=1; i<=10000; ++i) ten[i]=ten[i-1]*10%mo; 
	m=read(); 
	while(m--) x=read(), y=read(), printf("%lld\n", ((calc(y)-calc(x-1))%mo+mo)%mo); 
	return 0; 
}

Topics: Math