Codeforces 385 C Bear and prime numbers

Source: Internet
Author: User

Question link ~~>

Question recognition:This is an idea question. During the competition, I did the D question directly, but the dizzy result of processing the coordinates was not AC at the end.

Solution:

Because only the prime number, so ~ We only need to consider the prime number, which requires the screening of prime numbers. We can add the multiples of a prime number while screening the prime number, you only need to record a certain number of records when you input them.

Code:

# Include <iostream> # include <sstream> # include <map> # include <cmath> # include <fstream> # include <queue> # include <vector> # include <sstream> # include <cstring> # include <cstdio> # include <stack> # include <bitset> # include <ctime> # include <string> # include <iomanip> # include <algorithm> using namespace STD; # define int long intconst int INF = 0x3f3f3f; const double ESP = 0.0000000001; const double Pi = ACOs (-1.0); const I NT mod = 1000000007; const int my = 100 + 5; const int MX = 10000000 + 5; int Max, n, m; bool isprime [MX]; int sum [MX], num [MX]; void Init () // number of records simultaneously recorded by the screening method {memset (isprime, false, sizeof (isprime); memset (sum, 0, sizeof (SUM); For (INT I = 2; I <= max; ++ I) {sum [I] + = sum [I-1]; If (! Isprime [I]) {sum [I] + = num [I]; for (Int J = I + I; j <= max; j + = I) {sum [I] + = num [J]; isprime [J] = true ;}}} int main () {int X; while (~ Scanf ("% d", & N) {memset (Num, 0, sizeof (Num); max = 0; For (INT I = 0; I <N; ++ I) {scanf ("% d", & X); num [x] ++; // number of records max = max (max, x );} init (); scanf ("% d", & M); int le, RT; For (INT I = 0; I <m; ++ I) {scanf ("% d", & Le, & RT); If (RT> MAX) RT = max; If (Le> MAX) cout <"0" <Endl; else cout <sum [RT]-sum [le-1] <Endl ;}} return 0 ;}




Codeforces 385 C Bear and prime numbers

Related Article

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.