CF 55D Beautiful numbers (digital DP)

Source: Internet
Author: User

Test instructions

If a positive integer is divisible by the numbers on all its bits, it is called beautiful number and how many beautiful number are there in the Q interval [l,r]? (1<=l<=r<=9*1018)

Ideas:

The numbers are large and cannot be violent. But if you want to know if a number is beautiful, at least wait for all of its bits to appear? How else do you know if you can actually be divisible?

Analysis, like 2232 and 3232 and so on, these two just appear 2 and 3, their LCM is 6, so there can be compressed statistics is the LCM, open one-dimensional to store. Next consider the prefix part of what can be compressed, because the 1~9 LCM maximum is 2520, that can be prefixed to the first 2520, and finally modulo that digital real LCM on the line (2520 must be any combination of all 1~9 LCM multiples, so the first take the remainder will not affect the results) , then open one more dimension. So the state is dp[[the result of digital lcm][more than 2520], all numbers can be categorized into these 3 dimensions. The digital LCM can be optimized, and the table finds only 47 possible LCM, so it does not have to open 2520 size.

1#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 0x7f3f3f3f A #defineLL Long Long - #defineULL unsigned long Long - using namespacestd; the Const DoublePI = ACOs (-1.0); - Const intn= -; - Const intMod=2520; - intp[ the]={0,1,2,3,4,5,6,7,8,9,Ten, A, -, the, -, -, +, -, -, +  -, *, $, +, the, $, About, -, the, -, the, -, -, the, -,126, $,168, -  the, About,252,280,315, the,420,504,630,840,1260,2520}; +  A int_LCM (intMintN) {return(m*n)/__GCD (M, n);} atLL f[n][ the][mod+2], Bit[n], has[mod+2]; -  -LL DFS (intIintLcmintLeftBOOLe) - { -     if(i==0)returnlcm&&left%p[lcm]==0 ; -     if(!e && ~f[i][lcm][left])returnF[i][lcm][left]; in  -LL ans=0; to     intu= e? Bit[i]:9; +      for(intD=0; d<=u; d++) -     { the         intt= LCM? HAS[_LCM (P[LCM], Max (d,1)]: Max (d,0); *Ans+=dfs (I-1, T, (left*Ten+d)%mod, e&&d==u); $     }Panax Notoginseng     returne==true? ans:f[i][lcm][left]=ans; - } the  + ll Cal (ll N) A { the     intlen=0; +      while(n)//Split number -     { $bit[++len]=n%Ten; $N/=Ten; -     } -     returnDFS (Len,0,0,true); the } - Wuyi intMain () the { -     //freopen ("Input.txt", "R", stdin); Wumemset (f,-1,sizeof(f)); -      for(intI=0; i< the; i++) has[p[i]]=i; About  $LL L, R, t;cin>>T; -      while(t-- ) -     { -Cin>>l>>R; ACout<<cal (R)-cal (l1) <<Endl; +     } the     return 0; -}
AC Code

CF 55D Beautiful numbers (digital DP)

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.