SAC # 1 - Meng number

Topic background

SOL this question is provided by the world's most konjac most spicy chicken Caesar ratio.

Death on the city website is the perfect classroom information official website. Address: http: //191.101.11.174/mgzd.

Title Description

Spicy chicken konjac SOL is a sucker, he actually felt the number is sprouting!

Fortunately, in his eyes, not all numbers are sprouting. Only satisfy "length of at least 2 of the present palindromic substring" number is Meng - that is, Meng 101, 101 itself as a palindrome; 110 Meng, because they contain palindromic substring 11; 102 but not Meng, 1201 nor sprouting.

SOL now want to know how many Meng number of all integers from l to r in there.

Since the answer may be large, so only you need to output the remainder of the answer to 1,000,000,007 (10 ^ 9 + 7).

Input Format

Input contains only one line, comprising two integers: l, r.


When I thought this would be spicy chicken card

 

I did not expect so water? ? ?
 
Record what you last, last1 to determine whether there is a palindromic length of at least 2 sub-string
And a flag to indicate whether the road already exists
Note the leading 0 compartments palindrome is wrong
You traverse what l is not eligible number, subtract
#include<bits/stdc++.h>
#define re return
#define ll long long
#define inc(i,l,r) for(ll i=l;i<=r;++i)
using namespace std;

ll n,m,len,num[1002],f[1005][2][10][10];
ll mod=1000000007;

template<typename T>inline void rd(T&x)
{
    len=0;
    char c;bool f=0;
    while((c=getchar())<'0'||c>'9');
    num[++len]=c^48;
    
    while((c=getchar())>='0'&&c<='9')
    num[++len]=c^48;
}

inline ll dfs(int pos,int limit,int flag,int last,int last1,int lead)
{
    if(pos==len+1)re flag;
    if(lead&&!limit&&(~last1)&&(~f[pos][flag][last][last1]))re f[pos][flag][last][last1];
    
    int up=limit?num[pos]:9;
    ll res=0;
    
    inc(i,0,up)
    {
        if(flag)res=(res+dfs(pos+1,limit&&i==up,flag,i,last,1))%mod;
        else res=(res+dfs(pos+1,limit&&i==up,lead&&(last==i|last1==i),i,lead?last:-1,lead|i))%mod;
    }
    
    if(!limit&&(~last1))f[pos][flag][last][last1]=res;
    re res;
}

inline bool judge()
{
    if(num[2]==num[1])re 1;
    inc(i,3,len)
    {
        if(num[i]==num[i-1])re 1;
        if(num[i]==num[i-2])re 1;
    }
    re 0;
}
int main()
{
    freopen("in.txt","r",stdin); 
    memset(f,-1,sizeof f);
    rd(n);
    ll ans1=dfs(1,1,0,-1,-1,0)-judge();
    rd(m);
    ll ans2=dfs(1,1,0,-1,-1,0);
    printf("%lld",(ans2-ans1+mod)%mod);
    re 0;
}   

 

 

Guess you like

Origin www.cnblogs.com/lsyyy/p/11391435.html