Bayan 2015 Contest Warm Up D. CGCDSSQ (math,pair,map,暴力)
哎,只能转题解了,,,
8165031 | 2014-10-10 15:53:42 | njczy2010 | D - CGCDSSQ | GNU C++ | Accepted | 156 ms | 27584 KB |
8163765 | 2014-10-10 13:03:56 | njczy2010 | D - CGCDSSQ | GNU C++ | Time limit exceeded on test 10 | 2000 ms | 32600 KB |
8163761 | 2014-10-10 13:02:38 | njczy2010 | D - CGCDSSQ | GNU C++ | Time limit exceeded on test 10 | 2000 ms | 31600 KB |
8163755 | 2014-10-10 13:01:12 | njczy2010 | D - CGCDSSQ | GNU C++ | Wrong answer on test 9 | 46 ms | 19700 KB |
8163749 | 2014-10-10 12:59:54 | njczy2010 | D - CGCDSSQ | GNU C++ | Wrong answer on test 9 | 31 ms | 2100 KB |
2 seconds
256 megabytes
standard input
standard output
Given a sequence of integers a1, ..., an and q queries x1, ..., xq on it. For each query xi 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 of v1, v2, ..., vn, that is equal to a largest positive integer that divides all vi.
The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line contains n 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 follows q lines, each contain an integer xi, (1 ≤ xi ≤ 109).
For each query print the result in a separate line.
3 2 6 3 5 1 2 3 4 6
1 2 2 0 1
7 10 20 3 15 1000 60 16 10 1 2 3 4 5 6 10 20 60 1000
14 0 2 2 2 0 2 2 1 1
题解以代码转自:http://blog.csdn.net/accelerator_/article/details/39892385
D:记录下gcd,每次多一个数字,就和前面的数字都取gcd,然后合并掉,下次利用这些已有的gcd去做,然后合并的过程要利用到gcd的递减性质,就可以直接从头往后找即可
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
//#include<pair> #define N 1000005
#define M 1000005
#define mod 1000000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,q;
int a[N];
map<int,ll>ans;
typedef pair<int,ll>PP;
PP save[N];
int tot; int gcd(int x,int y)
{
if(y==)
return x;
return gcd(y,x%y);
} void ini()
{
int i;
ans.clear();
tot=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
}
} void solve()
{
int i,j;
int sn;
for(i=;i<=n;i++){
for(j=;j<tot;j++){
save[j].first=gcd(save[j].first,a[i]);
}
save[tot]=make_pair(a[i],);
tot++;
sn=;
for(j=;j<tot;j++){
if(save[sn].first==save[j].first){
save[sn].second+=save[j].second;
}
else{
sn++;
save[sn]=save[j];
}
}
sn++;
tot=sn;
for(j=;j<tot;j++){
ans[ save[j].first ]+=save[j].second;
}
}
} void out()
{
int x;
scanf("%d",&q);
while(q--){
scanf("%d",&x);
printf("%I64d\n",ans[x]);
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
// scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d",&n)!=EOF)
{
//if(n==0 && k==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}
Bayan 2015 Contest Warm Up D. CGCDSSQ (math,pair,map,暴力)的更多相关文章
- Bayan 2015 Contest Warm Up D题(GCD)
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- map+pair Bayan 2015 Contest Warm Up D题
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- COCI2014/2015 Contest#1 D MAFIJA【基环树最大独立点集】
T1725 天黑请闭眼 Online Judge:COCI2014/2015 Contest#1 D MAFIJA(原题) Label:基环树,断环+树形Dp,贪心+拓扑 题目描述 最近天黑请闭眼在 ...
- 2018-2-6考试(COCI2014/2015 Contest#5)
T1:FUNGHI(1s,32M,50pts)得分:50 题意:给你8个数组成一个环,要你求出其中连续的4个数,让它们的和最大 题解:暴力求出每一连续4个数之和,比较一下就好 标签:模拟 C++ Co ...
- C++算法代码——求数列[coci2014/2015 contest #1]
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1815 题目描述 Mirko在数学课上以一种有趣的方式操作数列,首先,他写下一个数列A ...
- codeforces CF475 ABC 题解
Bayan 2015 Contest Warm Up http://codeforces.com/contest/475 A - Bayan Bus B - Strongly Connected Ci ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
- cf475A Bayan Bus
A. Bayan Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 2015 浙江省赛 H - May Day Holiday
H - May Day Holiday As a university advocating self-learning and work-rest balance, Marjar Universit ...
随机推荐
- 在Terminal中,如何打开Finder,并显示当前的目录
这是一个非常方便实用的小技巧,在Terminal中输入如下命令: $ open . 有图有真相: 参考: Open Finder in Current Folder from Terminal
- 二分查找算法java
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小于该中点元 ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【搜索 ex-BFS】bzoj2346: [Baltic 2011]Lamp
关于图中边权非零即一的宽度优先搜索 Description 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组 ...
- 关于Linux上的SSH服务无法启动,提示“/var/empty/sshd must be owned by root and not group or world-writable”错误
首先通过物理终端进入到linux上,手工检查ssh发现没运行# /etc/init.d/sshd statussshd is stopped 手动启动服务,发现报告权限错误.# /etc/init.d ...
- [php] 表单注意事项
什么是 htmlspecialchars()方法? htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 预定义的字符是: & (和号) 成为 & &q ...
- python--线程的其他方法
一 . current_thread的用法 import threading import time from threading import Thread, current_thread def ...
- Python面向对象(成员)(二)
1. 成员 在类中你能写的所有内容都是类的成员 2. 变量 1. 实例变量: 由对象去访问的变量. class Person: def __init__(self, name, id, gender, ...
- 数据结构之--图(Graphics)
1.1:图的定义和术语 图是一种比线性表和树更为复杂的数据结构.在线性表中,数据元素之间仅有线性关系,每个元素仅有一个直接前驱和一个直接后继:在树形结构中,数据元素之间有着明显的层次关系,并且每一 ...
- as eclipse 对比
eclipse目录结构 src:存放prj源码 gen:自动生成文件,R类存放资源文件的唯一id android+版本:项目jar包 assets:res存放资源文件,不会被gen生成资源id,同过a ...