cf Beautiful numbers(디지털 dp)

2709 단어 DP
D. Beautiful numbers
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
Input
The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri (1 ≤ li ≤ ri ≤ 9 ·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
Sample test(s)
input
1
1 9

output
9

input
1
12 15

output
2

[l, r]에서 각 자릿수(0 제외)의 수를 정제할 수 있는 개수를 구하세요.
CODE:
#include  
#include  
#include  
#include  
#include  
#define ll long long  
#define Mod 2520  ////1-9        
  
using namespace std;  
  
ll l,r;  
ll dp[30][Mod][50];///dp[i][j][k] i:    j:          Mod    k:                
int num[30];  
int b[Mod+1];     ///         
  
ll gcd(ll a,ll b) {  
    return b==0?a:gcd(b,a%b);  
}  
  
ll Lcm(ll a,ll b) {  
    return a/gcd(a,b)*b;  
}  
  
ll dfs(int i,ll lcm,ll sum,bool e) {  
    if(i<=0)return sum%lcm==0;  
    if(!e&&dp[i][sum][b[lcm]]!=-1)return dp[i][sum][b[lcm]];  
    ll res=0;  
    int u=e?num[i]:9;  
    for(ll d=0; d<=u; d++) {  
        if(d==0)                 ///d==0 ,lcm    
            res+=dfs(i-1,lcm,sum*10%Mod,e&&d==u);  
        else  
            res+=dfs(i-1,lcm*d/gcd(lcm,d),(sum*10+d)%Mod,e&&d==u);  
    }  
    return e?res:dp[i][sum][b[lcm]]=res;  
}  
   
ll solve(ll n) {  
    int len=1;  
    while(n) {  
        num[len++]=n%10;  
        n/=10;  
    }  
    return dfs(len-1,1,0,1);  
}  
  
int main() {  
    memset(dp,-1,sizeof dp);  
    int cnt=0;  
[cpp] view plaincopy
    ///    lcm   
    for(int i=1; i<=Mod; i++)if(Mod%i==0)b[i]=cnt++;  
    int t;  
    scanf("%d",&t);  
    while(t--) {  
        scanf("%I64d%I64d",&l,&r);  
        ll ans=solve(r)-solve(l-1);  
        printf("%I64d
",ans); } return 0;

좋은 웹페이지 즐겨찾기