HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

时间:2022-04-13 13:30:37

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 ;
}
/*
// //
*/