zoukankan      html  css  js  c++  java
  • HDU5726(RMQ&&二分)

    GCD

    Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    Give you a sequence of N(N100,000) integers : a1,...,an(0<ai1000,000,000). There are Q(Q100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l,r)(1l<rN)such that gcd(al,al+1,...,ar) equal gcd(al,al+1,...,ar).

    Input

    The first line of input contains a number T, which stands for the number of test cases you need to solve. 

    The first line of each case contains a number N, denoting the number of integers. 

    The second line contains N integers, a1,...,an(0<ai1000,000,000)

    The third line contains a number Q, denoting the number of queries. 

    For the next Q lines, i-th line contains two number , stand for the li,ri, stand for the i-th queries. 

    Output

    For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1). 

    For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l,r) such that gcd(al,al+1,...,ar) equal gcd(al,al+1,...,ar)

    Sample Input

    1
    5
    1 2 4 6 7
    4
    1 5
    2 4
    3 4
    4 4

    Sample Output

    Case #1:
    1 8
    2 4
    2 4
    6 1

     1 //2016.8.9
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<map>
     5 #include<algorithm>
     6 #include<cmath>
     7 
     8 using namespace std;
     9 
    10 typedef long long ll;
    11 const int N = 100005;
    12 int dp[N][20];//d[i][j]表示从第i个数字开始向后2^j个数字这段区间内的gcd,具有递减性
    13 map<int, ll> mp;
    14 
    15 void init_rmq(int n)//初始化dp,求出每段区间的gcd
    16 {
    17     for(int j = 1; j < (int)log2(n)+1; j++)
    18       for(int i = 1; i <= n; i++)
    19       {
    20           if(i+(1<<j)-1 <= n)
    21             dp[i][j] = __gcd(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
    22       }
    23 }
    24 
    25 int rmq(int l, int r)//查询
    26 {
    27     int k = (int)log2(r-l+1);
    28     return __gcd(dp[l][k], dp[r-(1<<k)+1][k]);
    29 }
    30 
    31 int main()
    32 {
    33     int n, q, l, r, T, kase = 0;
    34     cin>>T;
    35     while(T--)
    36     {
    37         printf("Case #%d:
    ", ++kase);
    38         cin>>n;
    39         mp.clear();
    40         for(int i = 1; i <= n; i++)
    41           scanf("%d", &dp[i][0]);
    42         init_rmq(n);
    43 
    44 //利用二分求具有相同gcd区间的数目
    45 //-----------------------------------------------------------------------------------
    46         for(int i = 1; i <= n; i++)
    47         {
    48             int a = i, b = n, mid, tmp, vs;
    49             while(1)
    50             {
    51                 tmp = a; 
    52                 vs = rmq(i, a);
    53                 while(a <= b)
    54                 {
    55                     mid = (a+b)>>1;
    56                     if(rmq(i, mid)<vs) b = mid-1;
    57                     else a = mid+1;
    58                 }
    59                 mp[vs]+=1ll*(b-tmp+1);
    60                 b = n;
    61                 if(a>b)break;
    62             }
    63         }
    64 //------------------------------------------------------------------------------------
    65 
    66         cin>>q;
    67         while(q--)
    68         {
    69             scanf("%d%d", &l, &r);
    70             int ans = rmq(l, r);
    71             cout<<ans<<" "<<mp[ans]<<endl;
    72         }
    73     }
    74 
    75     return 0;
    76 }
  • 相关阅读:
    Replication:The replication agent has not logged a progress message in 10 minutes.
    分区管理
    获取URL最后一个 ‘/’ 之后的字符
    Replication 第四篇:事务复制中Subscriber的主键列是只读的
    窗口和窗口函数
    SQL Server 日期格式和日期操作
    约束4:唯一约束,Check约束和null
    约束3:default约束
    Merge语句中NULL的陷阱
    查询“全部”
  • 原文地址:https://www.cnblogs.com/Penn000/p/5754843.html
Copyright © 2011-2022 走看看