这个题主要在于时间复杂度的计算,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. TestNG+Excel+(HTTP+JSON) 简单接口测试

    说明: 1.使用Exce作为数据存放地: 2.使用TestNG的Datarprovide 做数据供应: 3.不足的地方没有指定明确的result_code , error_code , ERROR_M ...

  2. php框架的制作原理

    php框架的制作原理 (2012-08-16 14:25:55) 转载▼ 标签: php框架制作 杂谈 分类: php index.php 主入口文件 <?php  define('ISEXIS ...

  3. DotNetty 跨平台的网络通信库

    长久以来,.Net开发人员都非常羡慕Java有Netty这样,高效,稳定又易用的网络通信基础框架.终于微软的Azure团队,使用C#实现的Netty的版本发布.不但使用了C#和.Net平台的技术特点, ...

  4. 网页正文提取,降噪的实现(readability/Document)

    安装: pip install readability-lxml 使用: # encoding:utf-8import html2textimport requestsimport refrom re ...

  5. MySQL的间隙锁

    什么是间隙锁当我们用范围条件而不是相等条件检索数据,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录的索引项加锁:对于键值在条件范围内但不存在的记录,叫做“间隙(GAP)”,InnoDB也 ...

  6. 绿色计算大赛决赛 第二阶段 消息传递(斯坦纳树 状压dp+spfa)

    传送门 Description 作为公司老板的你手下有N个员工,其中有M个特殊员工.现在,你有一个消息需要传递给你的特殊员工.因为你的公司业务非常紧张,所以你和员工之间以及员工之间传递消息会造成损失. ...

  7. jenkins自动部署windwos服务器

    jenkins 持续构建windows 项目 需求说明 公司新购windwos服务器,并配置了堡垒机,由于经常要提交代码进行更新,导致手动部署很是麻烦,故采用公司jenkins实行持续构建 jenki ...

  8. 利用VRID/VMAC实现更安全的netscaler HA故障切换

    利用VRID/VMAC实现更安全的netscaler HA故障切换 virtual MAC在故障切换(failover)中的作用.    在一个HA模式中,首要节点(primary node)拥有所有 ...

  9. idea的protobuf使用

    1.安装插件 2.添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  10. MT【137】多少个?

    数列\(\{a_n\}\)共11项,\(a_1=0,a_{11}=4\),且\(|a_{k+1}-a_{k}|=2,k=1,2,\cdots,10\) 求满足条件的不同的数列的个数______ 解答: ...