Function

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 976    Accepted Submission(s): 375

Problem Description
The shorter, the simpler. With this problem, you should be convinced of this truth.
  
  You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculate F(l,r), for each query (l,r).
 
Input
There are multiple test cases.
  
  The first line of input contains a integer T, indicating number of test cases, and T test cases follow. 
  
  For each test case, the first line contains an integer N(1≤N≤100000).
  The second line contains N space-separated positive integers: A1,…,AN (0≤Ai≤109).
  The third line contains an integer M denoting the number of queries. 
  The following M lines each contain two integers l,r (1≤l≤r≤N), representing a query.
 
Output
For each query(l,r), output F(l,r) on one line.
 
Sample Input
1
3
2 3 3
1
1 3
 
Sample Output
2
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5877 5872 5871 5870 5869 
 

Statistic | Submit | Discuss | Note

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5875

题目大意:

  N个数(N<=100000),M个询问,每次询问L,R,求F(L,R)。

  F(L,R)=F(L,R-1)%A[R] , L<R

     =A[L] , L=R

题目思路:

  【倍增】

  这题数据水到暴力居然过了。。比赛的时候一想会T就没写。

  首先这题实际是求A[L]%A[L+1]%A[L+2]...%A[R]的值。

  那么很容易想到A[L]右边比他大的数都是没有必要取模的,变为找L右边第一个小于A[L]的取模

  模完后的结果缩小,再次寻找当前位置右边第一个小于此时的值的继续取模。可以证明这样的取模次数为log2N(100000取模后最大为49999)

  暴力的做法就是预处理的时候直接从后往前用单调栈求出位置X右边第一个小于A[X]的位置Y,next[X]=Y。查询的时候用next跳着模。

  当然这是不够的,因为可以构造出递减序列使得实际操作次数仍为N。

  用倍增的思想,对于位置i右侧的最长下降子序列,next[i][j]表示位置i右边第2j个的位置,预处理出每个位置的next数组

  每次做的时候尽量远跳,直到找到next[j][0]恰好小于当前的值z,则z对A[next[j][0]]取模,移动左标记,继续查找,直到超出R或者没有更小的数。

倍增:

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 100004
#define M 18
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int a[N],s[N];
int nextt[N][M];
void init()
{
int i,j,k,top;
s[top=]=n;
for(i=;i<M;i++)nextt[n][i]=n+;
for(i=n-;i;i--)
{
while(top && a[i]<a[s[top]])top--;
if(!top)nextt[i][]=n+;
else nextt[i][]=s[top];
s[++top]=i;
}
for(j=;j<M;j++)
{
for(i=;i<n;i++)
{
k=nextt[i][j-];
nextt[i][j]=nextt[k][j-];
}
}
}
int work(int l,int r)
{
int i,j,z=a[l];
if(l==r)return z;
while(l<r)
{
if(!z)return z;
for(i=;i<M-;i++)
{
if(a[nextt[l][]]<=z || nextt[l][i]>r)break;
if(a[nextt[l][i+]]<=z || nextt[l][i+]>r)
{
l=nextt[l][i];
i=-;
continue;
}
}
if(i==M || nextt[l][i]>r || !a[nextt[l][i]])return z;
z%=a[nextt[l][]];l=nextt[l][];
}
return z;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",a+i);
init();
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
printf("%d\n",work(x,y));
}
}
return ;
}
/*
// //
*/

暴力:

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 100004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int a[N],pre[N],s[N];
void print()
{
int i;
for(i=;i<=n;i++)
printf("%d ",pre[i]);
puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z,top,l,r;
// init();
for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
top=;
s[top]=n;pre[n]=n+;
for(i=n-;i;i--)
{
while(top && a[i]<=a[s[top]])
top--;
if(!top)pre[i]=n+;
else pre[i]=s[top];
s[++top]=i;
}
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d",&l,&r);
x=a[l];
for(j=pre[l];j<=r;j=pre[j])
x%=a[j];
printf("%d\n",x);
}
//print();
}
return ;
}
/*
// //
*/

  

HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)的更多相关文章

  1. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  2. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  4. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  5. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  7. HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online

    N个数(N<=100000),M个询问,每次询问L,R,求F(L,R). F(L,R)=F(L,R-1)%A[R] , L<R 这道题数据比较鶸 可以直接用递减爆 正确做法应该是倍增 用倍 ...

  8. Hdu OJ 5884-Sort (2016 ACM/ICPC Asia Regional Qingdao Online)(二分+优化哈夫曼)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题目大意:有n个有序的序列,对于第i个序列有ai个元素. 现在有一个程序每次能够归并k个序列, ...

  9. 2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ

    Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

随机推荐

  1. jQuery作用

    jquery是前端里面比较总要的,是很强大的一个选择器. 表单: 1.$(":input") 查找所有的input元素 2.$("text")    匹配所有的 ...

  2. gulp的点点滴滴

    去年用gulp,但一直没有写篇博客,今天有时间无聊写一篇.... 什么是gulp?gulp的官网title上对这个工具有一个比较准确的定义,叫做:基于流的自动化构建工具.如果你查看它的网页源代码,还会 ...

  3. 调优系列-tomcat调优

    http://www.360doc.com/content/14/1208/13/16070877_431273418.shtml 使用JMeter对Tomcat进行压力测试与Tomcat性能调优 n ...

  4. hive hbase pig 区别

    参考文档http://www.linuxidc.com/Linux/2014-03/98978.htm

  5. TFS 服务器更换后工作区无法绑定

    需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...

  6. 用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本

    ng.module('index-filters', []) .filter('trustHtml', function ($sce) { return function (input) { retu ...

  7. 网络编程(学习整理)---1--(Tcp)实现简单的控制台聊天室

    1.简单的聊天室(控制台): 功能实现: 客户端和服务端的信息交流: 2.牵扯到的知识点: 这个我大概说一下,详细后面见代码! 1) 网络通讯的三要素 1. IP 2. 端口号. 3. 协议   2) ...

  8. underscorejs-toArray学习

    2.23 toArray 2.23.1 语法: _.toArray(list) 2.23.2 说明: 把list(任何可以迭代的对象)转换成一个数组,在转换arguments对象时非常有用. 2.23 ...

  9. javascript常用内置对象总结(重要)

    Javascript对象总结 JS中内置了17个对象,常用的是Array对象.Date对象.正则表达式对象.string对象.Global对象 Array对象中常用方法: Concat():表示把几个 ...

  10. http拦截器interceptors

    在服务里配置$httpProvider.interceptors的相关参数 包含 request请求拦截 response响应拦截 requestError请求错误抛出 responseError响应 ...