HDU XXXX: Number of primes [l,r] (DP)

Source: Internet
Author: User

Problem Gtime limit:2000/1000ms (java/other) Memory limit:131072/131072k (Java/other) total submission (s): Acce pted Submission (s): 28Font:Times New Roman |  Verdana | Georgia Font Size: ← → Problem descriptions number is the number which the sum of every digit is a prime number, such as number 98, 29. Output the number ofS number in [l,r]. Input First line containsT(t≤ten) Denoting the number of test cases.
TCases follows for each cases:
There numbers < Span id= "mathjax-span-16" class= "Mrow" >l ,r . (0 ≤l≤ r≤ 1016) Outputfor each case, output the number of S number . Sample Input
24 3049 173
Sample Output
1245

Test instructions: Find out [0,r]-[0,l-1] on the sum of the primes in [L,r].
Idea: Because the data range in 0~10^16 so large, so can not be violent, considering that each bit is up to 9, then a maximum of 15 9 words is 135 number so many, so I made a judgment 135 inside the number of which is the number of the table, and then the digital DP
1#include <cstdio>2#include <iostream>3#include <cstring>4 using namespacestd;5 #defineN 206 intBit[n];7 Long Longdp[n][ $][2];8 intprime[ $];9 Ten BOOLCheckintx) One { A      for(intI=2; i*i<=x;i++){ -         if(x%i==0)return false; -     } the     returnx!=1; - } -  - voidP () + { -      for(intI=2; i<= $; i++){ +         if(check (i)) { Aprime[i]=1; at         } -     } - } - //flag Indicates whether the previous number is the prefix of the upper bound (that is, the following number can be filled in arbitrarily).  - Long LongDfsintPosintStintHaveintflag) - { in     if(!pos)returnhave ; -     if(flag&&dp[pos][st][have]!=-1)returnDp[pos][st][have]; to     Long Longans=0; +     intU=flag?9: Bit[pos]; -      for(intD=0;d <=u;d++){ theAns+=dfs (pos-1, St+d,prime[st+d],flag| | d<u); *         //determines whether the previous position and plus the current position can be a prime number $     }Panax Notoginseng     if(flag) dp[pos][st][have]=ans; -     returnans; the } +  A Long LongSolveLong Longs) the { +memset (bit,0,sizeof(bit)); -     intL=0; $      while(s) { $bit[++l]=s%Ten; -S/=Ten; -     } the     returnDFS (L,0,0,0); - }Wuyi  the intMain () - { Wu     intT; -Cin>>T; Aboutmemset (Prime,0,sizeof(prime)); $ P (); -      while(t--){ -memset (dp,-1,sizeof(DP)); -         Long Longs1,s2; ACin>>s1>>S2; + //cout<<solve (S2) << "" <<solve (s1-1) <<endl; theCout<<solve (S2)-solve (s1-1) <<Endl; -     } $     return 0; the}

HDU XXXX: Number of primes [l,r] (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.