map+pair Bayan 2015 Contest Warm Up D题
来源:互联网 发布:erp系统java项目 编辑:程序博客网 时间:2024/06/05 01:54
Given a sequence of integers a1, ..., an andq queriesx1, ..., xq on it. For each queryxi you have to count the number of pairs(l, r) such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, ..., ar) = xi.
is a greatest common divisor ofv1, v2, ..., vn, that is equal to a largest positive integer that divides allvi.
The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line containsn space separated integers a1, ..., an, (1 ≤ ai ≤ 109).
The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then followsq lines, each contain an integer xi, (1 ≤ xi ≤ 109).
For each query print the result in a separate line.
32 6 3512346
12201
710 20 3 15 1000 60 16101234561020601000
14022202211
题意给你一个序列和q个询问,输出有多少个区间[l,r]满足区间gcd为询问的值。
思路:处理出每个区间的gcd(要注意合并区间),如果有询问则直接加上去。
#include <bits/stdc++.h>using namespace std;std::map<int, int> id;vector<pair<int,int> >gcdList;const int MAXN=100000+5;int query[MAXN*3],A[MAXN];long long ans[MAXN*3];int _gcd(int a,int b){return b?_gcd(b,a%b):a;}int ID(int x){ if(!id[x])id[x]=id.size(); return id[x];}int main(int argc, char const *argv[]){ int n; ios_base::sync_with_stdio(false); gcdList.clear();id.clear(); cin>>n; for(int i=1;i<=n;i++)cin>>A[i]; int q;cin>>q; for(int i=1;i<=q;i++){ cin>>query[i]; query[i]=ID(query[i]); } for(int i=1;i<=n;i++){ for(int j=0;j<gcdList.size();j++) gcdList[j].first=_gcd(gcdList[j].first,A[i]); gcdList.push_back(make_pair(A[i],i)); int cnt=1; //sort(gcdList.begin(), gcdList.end()); for(int j=1;j<gcdList.size();j++){ if(gcdList[j].first!=gcdList[cnt-1].first) gcdList[cnt++]=gcdList[j]; } gcdList.resize(cnt); for(int j=0;j<gcdList.size();j++){ if(id[gcdList[j].first]){ int r=i+1; if(j+1<gcdList.size())r=gcdList[j+1].second; ans[id[gcdList[j].first]]+=r-gcdList[j].second; } } } for(int i=1;i<=q;i++)cout<<ans[query[i]]<<endl; return 0;}
- map+pair Bayan 2015 Contest Warm Up D题
- Bayan 2015 Contest Warm Up (D题)
- Bayan 2015 Contest Warm Up D题(GCD)
- Bayan 2015 Contest Warm Up
- Bayan 2015 Contest Warm Up
- codeforce Bayan 2015 Contest Warm Up D CGCDSSQ
- Bayan 2015 Contest Warm Up D. CGCDSSQ (数论)
- Bayan 2015 Contest Warm Up(C,D)
- Bayan 2015 Contest Warm Up C题(DP+模拟)
- CF Bayan 2015 Contest Warm Up A.(模拟+预处理)
- CF Bayan 2015 Contest Warm Up B.(dfs+暴力)
- CODEFORCES Bayan 2015 Contest Warm Up A题= =WA啦!~~!!!
- CF Bayan 2015 Contest Warm Up B题 Strongly Connected City
- Bayan 2015 Contest Warm Up E题(双连通分量缩点+DP计数)
- CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting
- D - Warm up 2
- 2015 HNU Warm Up 01
- 2015 HNU warm up 02
- report on "user interface design". on Oct 10th
- uva 12045 (矩阵快速幂)
- 血眼龙王传--秋叶青篇
- 表单兼容类型设计
- PKU 2226 - Muddy Fields (最小点覆盖)
- map+pair Bayan 2015 Contest Warm Up D题
- Spring MVC 文件下载最佳实践
- C# 6.0 (C# vNext) 新功能之:Auto-Properties with Initializers
- windows桌面显示“系统文件夹”和“系统图标”
- DirectX11 学习笔记8 - 最简单的光照
- 仔罪作足谞着最纂鬃庄谞兹住字转
- c++____abc : a b c , ab ac ba bc ca cb, abc acb bac bca cab cba
- UVA 557 Burger (数学)
- 修改u-boot编译选项以便支持使用arm-linux-gnueabihf-编译器编译(soft-float & VFP hard)