CF 55d-beautiful numbers (digital DP)

Source: Internet
Author: User

Test instructions

If a number can be divisible by the number of its own bits, then it is called Beautiful numbers.
The number of Beautiful numbers in the interval [a, b].

Analysis: First analysis, 2~9 the largest least common multiple is 2520 ({5,7,8,9}), first preprocess all possible least common multiple m[c]

DP[I][D][C] Indicates the number of length I, remainder d, least common multiple is m[c].

#include <cstdio>#include<cstring>#defineMoD 2520ll dp[ *][2520][ -];intbit[ -],m[ $],mnum;intgcdintAintb) {    inttmp;  while(a%b) {tmp=b; b=a%b; A=tmp; }    returnb;}intLcmintAintb) {    returna*b/gcd (A, b);}
Check the label of least common multipleintTfind (intx) { intLl=1, rr=Mnum; while(ll<=RR) { intMid= (LL+RR) >>1; if(m[mid]<x) ll=mid+1; Elserr=mid-1; } returnll;}voidinit () {memset (DP,-1,sizeof(DP)); Mnum=0; for(intI=1; i<=mod;++i)if(mod%i==0) m[++mnum]=i;} ll Dfs (intIintDintCinte) { if(i==0)returnD%M[C]?0:1; if(!e&&dp[i][d][c]!=-1)returnDp[i][d][c]; intL=e?bit[i]:9; ll Num=0; for(intj=0; j<=l;++j) {intTd= (d*Ten+J)%MoD; intTc=C; if(j) tc=Tfind (LCM (m[c],j)); Num+=dfs (I-1,td,tc,e&& (j==l)); } returne?num:dp[i][d][c]=num;} ll solve (ll x) {intlen=0; while(x) {bit[++len]=x%Ten; X/=Ten; } returnDFS (Len,0,1,1);}intMain () {intT; scanf ("%d",&t); Init (); ll x, y; while(t--) {scanf ("%i64d%i64d",&x,&y); printf ("%i64d\n", Solve (y)-solve (X-1)); }return 0;}

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.