这个题主要在于时间复杂度的计算,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. gitlab账号注册及分组

    .开启注册邮箱验证 admin area -->setting-->Sign-up restrictions-->勾选Send confirmation email on sign- ...

  2. DataTable List 相互转换

    This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...

  3. java 基础 --概念--005

    1,面向对象特征 封装(encapsulation):隐藏对象的属相和访问细节,仅提供对外公共访问方式.继承(inheritance).多态(polymorphism) 2,类:一组相关的属性和行为的 ...

  4. 再看case语句

    再看case语句,case语句只处理单条记录,而不是set 列名的使用,可以当做数值来使用: case when 后面简直是完美的的,什么东西都是能放的,只要是一个逻辑上的true/false的逻辑就 ...

  5. C++模式学习------适配器模式

    适配器模式: 适配器模式属于结构型的设计模式,是将一个类的接口转换成使用方希望的另外一个接口,这样使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器模式有两种: 1.类的适配器:继承不 ...

  6. js判断浏览器语言实现网站国际化

    一般国际化的网站至少是有中.英文两种语言的,然后就是在不同的语言环境下使用不同的语言页面. 1.实现原理 一般实现这种功能的方法,无非就是两种, 第一种,判断浏览器语言类型: 第二种,判断ip所属国家 ...

  7. PGM学习之六 从有向无环图(DAG)到贝叶斯网络(Bayesian Networks)

    本文的目的是记录一些在学习贝叶斯网络(Bayesian Networks)过程中遇到的基本问题.主要包括有向无环图(DAG),I-Maps,分解(Factorization),有向分割(d-Separ ...

  8. P2234 [HNOI2002]营业额统计

    题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...

  9. bzoj4385 & POJ2015 Wilcze doły

    Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. Input 第一 ...

  10. 浅谈Tarjan算法及思想

    在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连 ...