• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #226 Div2 C

   
     
 

            
Sponsored by             
In English             По-русски        
HaibaraAi                 |                  Logout                        
 
 
 
 
 
  • Home
  • Contests
  • Gym
  • Problemset
  • Groups
  • Rating
  • Help
                                     
        
 
 
Codeforces Round #226 (Div. 2)
Finished
Practice
Add to favourites         
 
 
→ Practice            
 
        You are registered for practice. You can solve problems unofficially. Results can be found in the contest status and in the bottom of standings.    
    
 
 
→ Submit?            
 
                                   
Language:                 
Choose file:                 
                                Be careful: there is 50 points penalty for submission which fails the pretests or resubmission (except failure on the first test, denial of judgement or similar verdicts). "Passed pretests" submission verdict doesn't guarantee that the solution is absolutely correct and it will pass system tests.                            
                    
 
 
→ Last submissions            
 
SubmissionTimeVerdict
5814433 01/26/2014 10:30PM Accepted
5796818 01/24/2014 09:24PM Time limit exceeded on pretest 6
5796605 01/24/2014 09:22PM Runtime error on pretest 4
 
 
→ Problem tags            
 
 
 
 
 
    bitmasks     
 
 
 
 
    brute force     
 
 
 
 
    data structures     
 
 
 
 
    dp     
 
 
 
 
    implementation     
 
 
 
 
    math     
 
 
 
 
    number theory     
No tag edit access    
              
              
                
                
  •  
     
     
  • Problems
  • Submit
  • My submissions
  • Status
  • Hacks
  • Room
  • Standings
  • Custom test
C. Bear and Prime Numbers
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).

Help the bear cope with the problem.

Input

The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct.

The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.

Output

Print m integers — the answers to the queries on the order the queries appear in the input.

Sample test(s)
Input
6 5 5 7 10 14 15 3 2 11 3 12 4 4
Output
9 7 0
Input
7 2 3 5 7 11 4 8 2 8 10 2 123
Output
0 7
Note

Consider the first sample. Overall, the first sample has 3 queries.

  1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
  2. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
  3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.
            

Codeforces (c) Copyright 2010-2014 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: 01/26/2014 10:32PM (p1).
 
 
 
 1 #pragma comment(linker,"/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cmath>
 5 #include <queue>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 #define INF 0x7fffffff 
11 #define mod 1000000007
12 #define ll long long
13 #define maxn 10000025
14 #define pi acos(-1.0)
15 int n, m;
16 int a[1000025], pri[1000025], prv[maxn],b[maxn];
17 ll c[maxn];
18 void add(int x, ll d){
19     while (x < maxn){
20         c[x] += d;
21         x += x&-x;
22     }
23 }
24 ll sum(ll x){
25     ll res = 0;
26     while (x > 0){
27         res += c[x];
28         x -= x&-x;
29     }
30     return res;
31 }
32 int main(){
33     for (int i = 2; i*i < maxn;i++)
34     if (!prv[i]){
35         for (int j = i*i; j < maxn;j+=i)
36         if (!prv[j])prv[j] = 1;
37     }
38     int k = 0;
39     for (int i = 2; i < maxn;i++)
40     if (!prv[i])pri[k++] = i;
41     scanf("%d", &n);
42     for (int i = 0; i < n; i++)scanf("%d", &a[i]);
43     for (int i = 0; i < n; i++)b[a[i]]++;
44     for (int i=0;i<k;i++)
45         for (int j = pri[i]; j < maxn; j += pri[i])
46             if(b[j])add(pri[i], b[j]);
47     scanf("%d", &m);
48     while (m--){
49         int l, r;
50         scanf("%d%d", &l, &r);
51         if (l >= maxn){printf("0\n"); continue;}
52         if (r >= maxn)r = maxn - 1;
53         printf("%I64d\n", sum(r) - sum(l - 1));
54     }
55     return 0;
56 }
View Code
posted @ 2014-01-27 02:35  HaibaraAi  阅读(184)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3