连续取模-function

2017-09-22 21:56:08

The shorter, the simpler. With this problem, you should be convinced of this truth. 
   
  You are given an array AA of NN postive integers, and MM queries in the form (l,r)(l,r). A function F(l,r) (1≤l≤r≤N)F(l,r) (1≤l≤r≤N) is defined as: 
F(l,r)={AlF(l,r?1) modArl=r;l<r.F(l,r)={All=r;F(l,r?1) modArl<r. 
You job is to calculate F(l,r)F(l,r), for each query (l,r)(l,r).

InputThere are multiple test cases. 
   
  The first line of input contains a integer TT, indicating number of test cases, and TT test cases follow. 
   
  For each test case, the first line contains an integer N(1≤N≤100000)N(1≤N≤100000). 
  The second line contains NN space-separated positive integers: A1,…,AN (0≤Ai≤109)A1,…,AN (0≤Ai≤109). 
  The third line contains an integer MM denoting the number of queries. 
  The following MM lines each contain two integers l,r (1≤l≤r≤N)l,r (1≤l≤r≤N), representing a query.

OutputFor each query(l,r)(l,r), output F(l,r)F(l,r) on one line.Sample Input

1
3
2 3 3
1
1 3

Sample Output

2

代码如下:
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

#define MAXN 100010

int a[MAXN],nex[MAXN];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j,n,m;
        scanf("%d",&n);
        for(i = 1; i<=n; ++i)
        {
            scanf("%d",&a[i]);
        }
        for(i = 1;i<=n;++i)
        {
            nex[i] = -1;
            for(j = i+1;j<=n;++j)
            {
                if(a[j]<=a[i])
                {
                    nex[i] = j;
                    break;
                }
            }
        }
        scanf("%d",&m);
        for(i = 0;i<m;++i)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            int num = a[l];
            for(j = nex[l];j<=r;j = nex[j])
            {
                if(j == -1)
                {
                    break;
                }
                num%=a[j];
            }
            printf("%d\n",num);
        }
    }
    return 0;
}
时间: 2024-07-29 18:39:57

连续取模-function的相关文章

连续取模

哈理工团体赛:problem E . Mod Kim刚刚学会C语言中的取模运算(mod).他想要研究一下一个数字A模上一系列数后的结果是多少.帮他写个程序验证一下. Input 第一行一个整数T代表数据组数. 接下来T组数据,第一行一个整数n,接下来n个数字ai 接下来一行一个整数m,接下来m个数字bi Output 对于每个bi,输出bi%a1%a2%...%an Sample Input Output 1 4 10 9 5 7 5 14 8 27 11 25 4 3 2 1 0 Hint 在

整除k的最大连续子区间(前缀和取模)(2017美团笔试)

题意:一个数n,给出n个数,再给一个数k.求能整除k的连续区间和所在区间的最大长度.bc85场1001的升级版. 题解:刚拿到题的时候没看清是连续区间,就瞎想dp.发现连续区间后,想尺取法,发现这道题是离散的,没法尺取,也没法二分. 正解应该是前缀和取模.若(sum[j]-sum[i])%k==0则区间[i,j]的和是能整除k的.注意,前缀和的第一项是0. 预处理完成后,考虑如何求最大区间长.从前往后扫一次,记录每个值最早出现的位置.从后往前扫一次,以便记录每个值最后出现的位置.当值为0时,则记

组合数取模(转载)

本文转自:http://blog.csdn.net/skywalkert/article/details/52553048 0. 写在前面 在程序设计中,可能会碰到多种类型的计数问题,其中不少涉及到组合数的计算,所以笔者写下这么一篇文章,期望能解决一些常规的组合数求模问题.以下部分内容改编自AekdyCoin的<组合数求模>,而且为了感谢他对(懵懂的)笔者的启发,这篇文章的标题与其文章相同.另外,感谢Picks将多项式运算的技巧在中国进行推广,感谢51nod提供了许多有趣的数论题目,感谢fot

HDU 5363 Key Set【快速幂取模】

Key Set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1886    Accepted Submission(s): 990 Problem Description soda has a set S with n integers {1,2,-,n}. A set is called key set if the sum

hdu 5109(构造数+对取模的理解程度)

Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 211 Problem Description Alexandra has a little brother. He is new to programming. One day

POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1845 Appoint description:  System Crawler  (2015-05-27) Description Consider two natural numbers A and B. Let S be the sum of all natural d

POJ 1845 Sumdiv (快速分解因式+快速幂取模)

题目地址:POJ 1845 转载自:http://blog.csdn.net/lyy289065406/article/details/6648539 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题思路: 要求有较强 数学思维 的题 应用定理主要有三个: 要求有较强 数学思维 的题 应用定理主要有三个: (1)   整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

快速幂取模(POJ 1995)

http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c=((a%c)*b)%c 每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化 由此可以用快速幂算法优化: http://www.cnblogs.com/qlky/p/5020402.html 再结合取模公式: (a + b) % p = (a % p