HDU5288 OO’s Sequence(序列的整除对数计数) 多校赛1最水题
来源:互联网 发布:node pdf导出 编辑:程序博客网 时间:2024/05/18 05:00
http://acm.hdu.edu.cn/showproblem.php?pid=5288
Problem Description
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 (109+7).
Input
There are multiple test cases. Please process till EOF.
In each test case:
First line: an integer n(n<=10^5) indicating the size of array
Second line:contain n numbers ai(0<ai<=10000)
In each test case:
First line: an integer n(n<=10^5) indicating the size of array
Second line:contain n numbers ai(0<ai<=10000)
Output
For each tests: ouput a line contain a number ans.
Sample Input
51 2 3 4 5
Sample Output
23
分析每个数字x对结果的共享:
x能与多少个数字不发生整除关系呢,a1,a2,a3,a4,a5,x,a7,a8,a9,a10,可以发现,如果x(a6)能整除a3,则x不能存在于a3之前的序列,所以它能贡献6-3=3点贡献值
x能贡献多少次呢?向有看,如果x可以整除a9,即它无法对a9以后的序列贡献,所以它能贡献9-6=3次
做法:
从左向右扫描,实事更新每个数字的最右位置。(数字只有从1-10000)。我用了一个L数组去存
然后找它的左边靠的最近的约数,它到约数的距离存进ansL
再就是从右向左扫描,更新最左边的位置R数字,与约数的距离存进ansR
最终答案就是所有的数字的ansL*ansR的和
#include<cstdio>#include<cstring>#include<vector>using namespace std;typedef long long LL;const int mod = 1000000007;vector<int>yue[10005];void init()//打一个约数表,每个数x,x的约数记录进yue[x]{ for(int i=1;i<=10000;i++) for(int j=i;j<=10000;j+=i) yue[j].push_back(i);}int a[100005],L[10005],R[10005],ansL[100005],ansR[100005];void solve(){ init(); int n; while(scanf("%d",&n)!=EOF){ memset(L,0,sizeof(L));//L数组初始化领所有数一开始最右边的位置为0 for(int i=0;i<=10000;i++) R[i]=n+1;//R数组初始化领所有数一开始最左边的位置为n+1 for(int i=1;i<=n;i++)scanf("%d",&a[i]); LL ans=0; for(int i=1;i<=n;i++){//从左到右 int x=a[i],ml=0; for(int j=0;j<yue[x].size();j++) if(ml<L[yue[x][j]])ml=L[yue[x][j]]; ansL[i]=i-ml;//记录它与左边约数的最近距离 L[x]=i;//更新每个数字最右位置 } for(int i=n;i>=1;i--){//从右到左 int x=a[i],mr=n+1; for(int j=0;j<yue[x].size();j++) if(mr>R[yue[x][j]])mr=R[yue[x][j]]; ansR[i]=mr-i;//记录它与右边约数的最近距离 R[x]=i;//更新每个数字最左位置 } for(int i=1;i<=n;i++) ans=(ans+(LL)ansL[i]*(LL)ansR[i])%mod;//每个数字的共享之和 printf("%lld\n",ans); }}int main(){ solve(); return 0;}
0 0
- HDU5288 OO’s Sequence(序列的整除对数计数) 多校赛1最水题
- HDU5288 OO’s Sequence
- HDU5288 OO’s Sequence
- hdu5288 OO’s Sequence
- hdu5288(2015多校1)OO’s Sequence
- HDU5288 OO’s Sequence(暴力枚举)
- HDU5288:OO’s Sequence(思维)
- 搜索 - hdu5288 OO’s Sequence
- HDU5288 OO’s Sequence 二分
- [杂题] HDU5288: OO’s Sequence
- 2015 Multi-University Training Contest 1 Hdu5288 OO’s Sequence
- 2015 Multi-University Training Contest 1OO’s Sequence hdu5288
- HDU5288.OO’s Sequence(多校第一题)
- 解题报告 之 HDU5288 OO' s Sequence
- 2015年多校联合训练第一场OO’s Sequence(hdu5288)
- hdu5288 OO’s Sequence 二分 多校联合第一场
- hdu5288 OO’s Sequence(质因子分解+二分)
- [HDU 5288] OO's Sequence (序列DP)
- adb logcat不能关闭的问题(进程再启动进程的问题)
- hdoj-1198-Farm Irrigation【并查集】
- Java Web学习【4】System
- hdu1874 畅通工程续(求最短路径)
- 开博感言
- HDU5288 OO’s Sequence(序列的整除对数计数) 多校赛1最水题
- 正则表达式的量词
- Objective-C学习笔记(十五)——实例化对象
- ocp 047-141 Evaluate the following SQL statement:SELECT product_name || 'it's not available for orde
- mysql任务调度
- 分红包算法
- 【MVC框架】——Global.asax文件中的RegisterGlobalFilters()和RegisterRoutes()方法
- 获取文件mime_type类型
- 关于调试PLX9054的一些总结和经验