这个题主要在于时间复杂度的计算,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.

 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <cstring>
using namespace std;
#define N1 2000003
#define N2 103 int Cannon[N2];
int See[N1];
int number,end;
int ans = ; int main(){
memset(See,,sizeof(See));
memset(Cannon,,sizeof(Cannon));
cin >> number >> end;
for(int i = ;i < number ;i++){
cin >> Cannon[i];
}
for(int i = ;i < number ;i++){
for(int j = ;j <= (end / Cannon[i]) ;j++){ //这个地方使得算法的复杂度:C*(N*(1/2 + 1/3 + 1/4 + 1/5 +...+1/N))   <=  C*N*logN 
See[Cannon[i] * j] = ;
}
}
for(int i = ;i < ( sizeof(See) / sizeof(See[]) ); i++ ){
ans += See[i];
}
cout << ans << endl;
}

Tju_Oj_2790Fireworks Show的更多相关文章

随机推荐

  1. phantomjsDriver的初始化

    public static void main(String[] args) { File file=new File("src/main/resources/drivers"); ...

  2. 学习laravel源码之中间件原理

    刨析laravel源码之中间件原理 在看了laravel关于中间件的源码和参考了相关的书籍之后,写了一个比较简陋的管道和闭包实现,代码比较简单,但是却不好理解所以还是需要多写多思考才能想明白其中的意义 ...

  3. [转帖]什么是TRIM与GC?他们是怎样让SSD保持高速的

    什么是TRIM与GC?他们是怎样让SSD保持高速的 2017-7-6 15:43  |  作者:Strike   |  关键字:SSD,TRIM,GC,超能课堂 分享到       SSD的写入方式决 ...

  4. (转)关于ES6的 模块功能 Module 中export import的用法和注意之处

    关于ES6的 模块功能 Module 中export import的用法和注意之处 export default 的用法 export default命令用于指定模块的默认输出.显然,一个模块只能有一 ...

  5. Tomcat安全配置与性能优化

    Tomcat 是 Apache软件基金会下的一个免费.开源的WEB应用服务器,它可以运行在 Linux 和 Windows 等多个平台上,由于其性能稳定.扩展性好.免费等特点深受广大用户喜爱.目前,很 ...

  6. 每日一问(如何在List中加入、设置、获取和删除其中的元素?)

    作为集合接口的一部分,对List接口所做的操作,最常见的就是增删查改了.这里总结下JAVA 中List接口及实现该接口的类实现这些操作的方法. 一.增加新的元素的方法 在Collection接口中定义 ...

  7. PGM学习之四 Factor,Reasoning

    通过上一篇文章的介绍,我们已经基本了解了:Factor是组成PGM模型的基本要素:Factor之间的运算和推理是构建高维复杂PGM模型的基础.那么接下来,我们将重点理解,Factor之间的推理(Rea ...

  8. PHP中文字符gbk编码与UTF-8编码的转换

    通常PHP中上传文件,如果文件名称有中文字符,上传之后的名称是无法写入到本地的,因为上传来的编码格式一般是UTF-8的格式,这种格式是无法给文件命名并且存储到操作系统磁盘.在写入之前需要将其转换为gb ...

  9. 51nod 1589 移数博弈 | 基数排序(ノಠ益ಠ)ノ彡┻━┻

    51nod 1589 移数博弈 题面 给出一个序列a,长度 n <= 10^7, a[i] <= 10^7 求每个长度 >= 2 的区间的最大值*次大值 之和. 题解 主要思路是求每 ...

  10. 【转】ubuntu16.04安装ncurses-devel

    在ubuntu16.04中编译内核时,使用make menuconfig发生错误,说没有安装ncurses-devel. 使用apt install ncurses-devel命令安装该库,没有,然后 ...