Hangzhou Electric (HDU) ACM 4548 US prime

Source: Internet
Author: User
Tags ming

US primeTime limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 4482 Accepted Submission (s): 1524


Problem Description Xiao Ming logarithmic research More love, a talk about the number, the brain emerges a good majority of the problem, today, Xiao Ming want to test your understanding of the prime number.
The problem is this: a decimal number, if it is prime, and its numbers and is also a prime number, it is called the "United States prime", such as 29, itself is a prime, and 2+9 = 11 is also prime, so it is the United States prime.
Given an interval, can you figure out how many primes there are in this interval?
Input the first line to enter a positive integer t, indicating that there is a total of T-group data (T <= 10000).
Next, a total of two integers l,r (1<= L <= R <= 1000000) are entered for each line, representing the left and right values of the interval.
Output for each set of data, the number of case outputs, and then the number of us primes in the interval (including the endpoint value l,r) is output.
One row for each group of data, and a specific output format see sample.
Sample Input
31 1002 23 19

Sample Output
Case #1:14Case #2:1Case #3:4

if the ordinary method to solve this problem, no doubt will be timed out, the subject is the prime number of screening, the prime number of the table, the complexity of time greatly reduced. It is also important to note that in the process of seeking the number of primes, we cannot change the initial value of an array of primitive prime numbers, which requires us to define an array to access the primes, in addition, when I do this topic, I also use the idea of dynamic programming, to access the numbers of the numbers of beautiful primes in the interval, I divide the function into The code is as follows:
#include <iostream> #include <cstdio> #include <cstring>using namespaceStd; BOOL Prime[1000001]; BOOL Beauty[1000001]; int Dp[1000001]; void Findprime(){Memset(Prime,true ,sizeof( Prime));Memset(Beauty,true ,sizeof( Beauty));Prime[0]=Prime[1]=false;Beauty[0]=Beauty[1]=false;  for (int I=2;I<= +;I + +)//Prime selection method The core code area {  for(int J=2*I;J<=1000000;J+=I) {Prime[J]=false;Beauty[J]=false; }}//prime number filter core code area}void Beautyprime () {  int Sum,K;  for (int I=2;I<=1000000;I++) {Sum=0;K=I; if (Prime[I]==true ) {  while( K!=0) {Sum+=K%Ten;K/=Ten; }  if( Prime[Sum]!=true) {Beauty[I]=false; }  }}} voidDp()///Dynamic planning idea, dp[i] that is, the number of primes 1 to I {Memset(Dp,0 ,sizeof( Dp));  for (int I=1;I<=1000000;I + +) {  if( Beauty[I]==true) {Dp[I]=Dp[I-1]+1; }  Else Dp[I]=Dp[I-1]; }}int  main() {  int A,B,T,_count;Findprime();Beautyprime();Dp();  while (scanf("%d",&T)!=Eof) {_count=0;  while (T--) {_count++;scanf("%d%d",&A,&B);Printf("Case #%d:%d\n",_count,Dp[B]-Dp[A-1]); }}  return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric (HDU) ACM 4548 US prime

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.