Tju_Oj_2790Fireworks Show

这个题主要在于时间复杂度的计算,N是10的6次方,C是10的2次方,OJ系统可接受的时间是10的7次方(室友说是无数先人测出来了┭┮﹏┭┮),所以如果普通遍历的话肯定会超时。
而代码中是跳着走了,相当于C*(N*(1/2 + 1/3 + 1/4 + 1/5 +...+1/N))   <=  C*N*logN  , 这样就不会超时了。

It's Independence Day, and Farmer John has taken the cows to the fireworks show. Bessie wonders how much of the show the cows will be able to see since they might not be able to stay for the entire display.

The show features C (1 ≤ C ≤ 100) fireworks cannons conveniently numbered 1..C. Cannon i shoots fireworks every Ti (1 ≤ Ti ≤ N) seconds (all times in this task are integer seconds). In a spectacular opening, all cannons first shoot at time 0. Fireworks are visible only during the second in which they are launched from the cannon. The cows will stay at the show from time 1 through time N (1 ≤ N≤ 2,000,000).

Help Bessie figure out how many different times the cows will be able to see fireworks during the time period that they are at the show.

Input

* Line 1: Two space-separated integers: C and N.

* Lines 2..C + 1: Line i+1 contains the single integer Ti.

Output

* Line 1: A single integer that is the number of distinct seconds in the time interval from 1 through N that the cows will be able to see fireworks.

 

Sample Inpput

2 20
4
6

Sample Output

7

Input Details

The show features 2 cannons: one shooting fireworks every 4 seconds, the other shooting every 6 seconds. The cows will stay at the show from time 1 to time 20. Below is a chart showing the fireworks launches and the time the cows are present.
      CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
2                                   2                                   2 ...
1           1     2     1           1           1     2     1           1 ...
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ ...
0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

Output Details

There will be fireworks at times 4, 6, 8, 12, 16, 18, and 20, for a total of 7 distinct times. (Note that time 12, where both cannons shoot fireworks simultaneously, is only counted once.) See the graph above.

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <stack>
 5 #include <cstring>
 6 using namespace std;
 7 #define N1 2000003
 8 #define N2 103
 9 
10 int Cannon[N2];
11 int See[N1];
12 int number,end;
13 int ans = 0;
14 
15 int main(){
16     memset(See,0,sizeof(See));
17     memset(Cannon,0,sizeof(Cannon));
18     cin >> number >> end;
19     for(int i = 0;i < number ;i++){
20         cin >> Cannon[i];
21     }
22     for(int i = 0;i < number ;i++){
23         for(int j = 1;j <= (end / Cannon[i]) ;j++){          //这个地方使得算法的复杂度:C*(N*(1/2 + 1/3 + 1/4 + 1/5 +...+1/N))   <=  C*N*logN 
24             See[Cannon[i] * j] = 1;
25         }
26     }
27     for(int i = 0;i < ( sizeof(See) / sizeof(See[0]) ); i++ ){
28         ans += See[i];
29     }
30     cout << ans << endl;
31 }

 

posted @ 2018-10-30 15:50  不愿透漏姓名的王建森  阅读(277)  评论(0编辑  收藏  举报