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

    object类的equals方法用来比较是否是同一个对象,比较内存地址. jdk中有些类重写了equals方法,只要类型,内容相同,就相等. 类如果涉及到比较应该重写equals方法,比较内存地址没有 ...

  2. HDU 6166 Senior Pan(二进制分组+最短路)

    题意 给出一个\(n\)个点\(m\)条边的有向图\((n,m<=100000)\),从中选择\(k\)个点\((k<=n)\),问这k个点两两之间的最短路最小值是多少? 思路 直接的想法 ...

  3. hdu3712 Detector Placement

    题意:给一束激光,一个三棱柱,三棱柱会折射光,问这束激光最终是否会和y = 0相交: 分析:模拟题,为了方便处理折射角,事先求出每条边的向内和向外的法向量: findpoint : 找第一交点 ste ...

  4. 【Cf #502 H】The Films(莫队)

    题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...

  5. 《Linux内核分析》期末总结及学习心得

    [洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 一.学习心得 本学 ...

  6. java随机数的有趣用法

    直接用代码说明,比较容易理解 package com.wz.other; import java.util.Random;import java.util.concurrent.ThreadLocal ...

  7. HTTP返回代码 201 304 404 500等代表的含义

    在网站日志中,我们经常会看到很多返回的http代码,如201.304.404.500等等.可是这些具体的返回的HTTP代码究竟什么含义呢,在此做一下知识普及吧,记不住不要紧,到时候看看就行了,但最主要 ...

  8. 使用Hystrix实现自动降级与依赖隔离-微服务

    转载: https://www.jianshu.com/p/138f92aa83dc Hystrix出现的原因: hystrix是netflix开源的一个容灾框架,解决当外部依赖故障时拖垮业务系统.甚 ...

  9. 左值与右值,左值引用与右值引用(C++11)

    右值引用是解决语义支持提出的 这篇文章要介绍的内容和标题一致,关于C++ 11中的这几个特性网上介绍的文章很多,看了一些之后想把几个比较关键的点总结记录一下,文章比较长.给出了很多代码示例,都是编译运 ...

  10. 阿里云上部署了zabbix,突然无法收到报警邮件的解决办法

    在阿里云上部署了zabbix,一直能正常接收到zbx发来的报警邮件(报警邮箱是163的),不知是什么原因,突然无法接收到报警邮件了. 但在服务器上手动执行echo "hello"| ...