• 连续取模-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) (1lrN)F(l,r) (1≤l≤r≤N) is defined as: 
    F(l,r)={AlF(l,r1) 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(1N100000)N(1≤N≤100000). 
      The second line contains NN space-separated positive integers: A1,,AN (0Ai109)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 (1lrN)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
    ",num);
            }
        }
        return 0;
    }
  • 相关阅读:
    Windows逆向分析入门(六)——实战篇(用户信息)
    Windows逆向分析入门(五)——工具篇
    Windows逆向分析入门(四)——代码篇
    Windows逆向分析入门(三)——方法篇
    Windows逆向分析入门(二)——原理篇
    Windows逆向分析入门(一)——总篇
    WeTool的实现原理是什么呢?
    处理器如何实现原子操作
    如何保证对象在线程内唯一
    渣渣写算法之队列
  • 原文地址:https://www.cnblogs.com/pprp/p/7577351.html
Copyright © 2020-2023  润新知