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 , 这样就不会超时了。
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的更多相关文章
随机推荐
- sql update set from 的用法 (转)
关键字: update set from 下面是这样一个例子: 两个表a.b,想使b中的memo字段值等于a表中对应id的name值 表a:id, name 1 ...
- 对一致性Hash算法及java实现(转)
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...
- [转帖学习] 使用阿里云证书 升级https
nodejs从http升级到https(阿里云证书的使用) https://home.cnblogs.com/u/lhyxq/ 改天买一个域名自己试试. 升级原因 1.各大搜索引擎中,https ...
- jdk10配置
解压 vi /etc/profile JAVA_HOME=/home/elasticsearch/jdk- CLASSPATH=$JAVA_HOME/lib/ PATH=$PATH:$JAVA_HOM ...
- 2018 焦作icpc现场赛总结
Day 0 没有直达焦作的飞机,所以选择了先到新郑机场,再转乘城际列车.城际列车猜是专门给学生开通的吧,每天只有来和回一共两趟(所以机票选择的余地也不多).买的时候只有无座票了,本来以为会一直站着,但 ...
- BZOJ3107 CQOI2013二进制A+B(动态规划)
显然答案只与a.b.c中各自1的个数及位数有关.a.b只考虑前i位怎么填时,c最多在第i+1位上为1,而第i+1位及之后的a.b怎么填都不会对前i位造成影响.于是设f[n][i][j][k][0/1] ...
- HNOI/AHOI2018题解
作为一名高二老年选手来补一下我省去年的省选题. D1T1:寻宝游戏 按顺序给出\(n\)个\(m\)位的二进制数\(a_i\),再在最前方添一个\(0\), 给出\(q\)次询问,每次询问给出一个同样 ...
- 【poj3623】 Best Cow Line, Gold
http://poj.org/problem?id=3623 (题目链接) 题意 给出一个字符串,每次可以取首或尾接到一个新的字符串后面,求构出的字典序最小的新字符串. Solution 首先可以发现 ...
- 方程式EQGRP_Lost_in_Translation工具之fb.py
使用方法: 环境搭建:win2003下测试: 下载python2.6并安装 下载pywin32并安装 将C:\Python26添加到环境变量PATH中 将整个windows目录复制到windows20 ...
- JS的语法
1.语句和表达式 var a = 3 * 6; var b = a; b; 这里,3 * 6是一个表达式(结果为18).第二行的a也是一个表达式,第三行的b也是.表达式a和b的结果值都是18. var ...