hdu 5288 OO’s Sequence(计数)
OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know
∑i=1n∑j=inf(i,j) mod (+).
There are multiple test cases. Please process till EOF.
In each test case:
First line: an integer n(n<=^) indicating the size of array
Second line:contain n numbers ai(<ai<=)
For each tests: ouput a line contain a number ans.
题意:
本来的题意问 枚举所有i,j ,1<=i<=j<=n, 然后计算f(i,j)和是多少。
f(l,r)的值 是 输入的数组下标 l到r中有多少 数是无法被这个区间 任意一个数整除的。
做法:
转换种思想就是 某个数num[i],在多少个区间内 可以不被区间其他任何数整除。 答案加上区间个数。
所以 可以左右两边枚举过来。
以左边枚举过来为例:
把最近出现的数 记录下来,记录到 has数组。 如num[i] 记录成has[num[i]]=i
然后把每个数的因子枚举,判断最近左边出现因子在哪。 然后那个位子+1 就是左端点了。
在同样处理出右端点, 左右端点知道就很容易算出num[i]在多少区间内符合要求 加到ans里。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<vector>
using namespace std;
#define N 100006
#define M 10006
#define MOD 1000000007
int n;
int vis[M];
int a[N];
vector<int> G[M];
int L[N];
int R[N];
void init(){
for(int i=;i<M;i++){
for(int j=;j<=i;j++){
if(i%j==){
G[i].push_back(j);
}
}
}
}
int main()
{
init();
while(scanf("%d",&n)==){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(vis,,sizeof(vis));
memset(L,-,sizeof(L));
memset(R,-,sizeof(R));
for(int i=;i<=n;i++){
for(int j=;j<G[a[i]].size();j++){
int tmp=G[a[i]][j];
if(vis[tmp]){
if(a[i]%tmp==){
if(L[i]!=-){
L[i]=max(L[i],vis[tmp]+);
}
else{
L[i]=vis[tmp]+;
}
} }
}
vis[a[i]]=i;
}
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--){
for(int j=;j<G[a[i]].size();j++){
int tmp=G[a[i]][j];
if(vis[tmp]){
if(a[i]%tmp==){
if(R[i]!=-){
R[i]=min(R[i],vis[tmp]-);
}
else{
R[i]=vis[tmp]-;
}
} }
}
vis[a[i]]=i;
}
int ans=;
for(int i=;i<=n;i++){
if(L[i]==-){
L[i]=;
}
if(R[i]==-){
R[i]=n;
}
}
for(int i=;i<=n;i++){
ans=(ans+(i-L[i]+)*(R[i]-i+))%MOD;
}
printf("%d\n",ans);
}
return ;
}
hdu 5288 OO’s Sequence(计数)的更多相关文章
- HDU 5288 OO’s Sequence [数学]
HDU 5288 OO’s Sequence http://acm.hdu.edu.cn/showproblem.php?pid=5288 OO has got a array A of size ...
- HDU 5288 OO‘s sequence (技巧)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5288 题面: OO's Sequence Time Limit: 4000/2000 MS (Jav ...
- HDU 5288 OO’s Sequence 水题
OO's Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5288 Description OO has got a array A ...
- HDU 5288——OO’s Sequence——————【技巧题】
OO’s Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- Hdu 5288 OO’s Sequence 2015多小联赛A题
OO's Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)
OO's Sequence Time Limit: 4000/2000 MS (Jav ...
- hdu 5288 OO’s Sequence(2015多校第一场第1题)枚举因子
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题意:在闭区间[l,r]内有一个数a[i],a[i]不能整除 除去自身以外的其他的数,f(l,r ...
- hdu 5288 OO’s Sequence 枚举+二分
Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...
- HDU 5288 OO’s Sequence
题意:给一个序列,函数f(l, r)表示在[l, r]区间内有多少数字不是其他数字的倍数,求所有区间的f(l, r)之和. 解法:第一次打多校……心里还有点小激动……然而一道签到题做了俩点……呜呜呜… ...
随机推荐
- iOS之即时通讯相关理解
Socket: 1>Socket又称"套接字" 2>网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 3>应用程序通常通 ...
- PHP设计模式笔记三:三种基本设计模式(工厂模式、单例模式、注册树模式) -- Rango韩老师 http://www.imooc.com/learn/236
一.工厂设计模式 index.php $db = IMooc\Factory::createDatabase(); 使用工厂类的静态方法直接创建一个dababase对象,当类名发生修改时,在工厂里修改 ...
- [RxJS] Updating Data with Scan
You often need to update the data flowing through the stream with custom logic based on what you nee ...
- IT痴汉的工作现状25-技术之养成
要想成为技术大牛,除了天赋以外,更与后天的刻苦努力分不开.伟仔我天生愚顿.工作多年后仍与大牛相差甚远,更加觉得技术的养成是一个异常困难的过程. 是我不用功吗?我不这样觉得.伟仔尽管是个懒人,但对于技术 ...
- .net学习体会
莫名其妙学了IT,在课堂上学了C,C++,自学了C#,也做了一些网站项目,学习过程,写了厚厚的几本笔记本,却没写博文的习惯,前几天,有同学问学习.net的建议.其实我懂的也不多,也给了一些个人见解,主 ...
- Sass函数--颜色函数--RGB颜色函数
RGB颜色函数-RGB()颜色函数 主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数,比如说 adjust-color 和 change-color 等.1. ...
- http 压缩
HTTP压缩是在Web服务器 和浏览器间传输压缩文本内容的方法.HTTP压缩采用通用的压缩算法如gzip等压缩HTML.JavaScript或 CSS文件.压缩的最大好处就是降低了网络传输的数据量,从 ...
- OpenCV——ANN神经网络
ANN-- Artificial Neural Networks 人工神经网络 //定义人工神经网络 CvANN_MLP bp; // Set up BPNetwork's parameters Cv ...
- 最小生成树prim算法———模板
codevs.cn 最优布线问题 #include<cstdio>#include<cstring> bool u[101]; int g[101][101],minn[101 ...
- Remove掉Request.QueryString
好久上博客来了,最近有点忙,有点懒. 今天在解决一个Request.QueryString 传值的问题上遇到了,当不是第一次加载时需要把Request.QueryString的值赋值为null,刚开始 ...