spoj TBATTLE 质因数分解+二分

题目链接:点击传送

TBATTLE - Thor vs Frost Giants

#number-theory #sliding-window-1

Thor is caught up in a fierce battle with Loki‘s army. This army consists of frost giants that have magical powers with them. Their strength levels gets multiplied when they are together. Giants are not highly skilled in the arts of combat, but their sheer size and strength make them formidable opponents even for the Asgardian gods. Thor is no exception. They recover very fast from physical injury but their recovery slows down when they are exposed to extreme heat. 
Thor‘s hammer can generate heat only in multiples of heat quantum N. Frost giants get killed only when their combined strength level is exactly equal to the heat level of the hammer. Thor is interested in killing a continuous stretch of frost enemies with a throw of his hammer with a preference to kill closer enemies first.
Continuous stretch is defined as a set of consecutive elements.
Help Thor to determine the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index. If there is no such continuous stretch possible then print -1.

Input

The first line will contain N, the number of Frost Giants in Loki‘s army and the Heat quantum.
The second line will contain N integers (a_0, a_2....., a_n-1) - the strength of each frost giant. 
Minimum stretch of the army should be 1.

  • 1 ≤ N ≤ 100000
  • 1 ≤ a_i ≤ 100000

Output

Output the range of the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index.
If there is no such continuous stretch possible then print -1.

Example

Input:
3
1 2 9
Output: 2 2

Input: 5 2 3 4 8 9
Output:-1

Input:
10
2 4 3 5 17 4 7 5 2 15Output:7 8

Explanation

Input #1:
Thor can only kill the stretch [2,2] as this is the minimum length range with strength, multiple of 3.

Input #2:
There is no stretch of frost giants that have combined strength as a multiple of 5.

Input #3:
There are many stretches of frost giants that have strength as multiple of 10. But the minimal stretch with the least indices is from [7,8]. Minimum size stretches are [7, 8] and [8, 9]. Out of them we select [7,8].

Submit solution!

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
///   数组大小
vector<int>p;
int n;
int c[20];
void init(int n)
{
    memset(c,0,sizeof(c));
    int si=0;
    for(int i=2;i<=n;i++)
    {
        if(n%i==0)p.push_back(i),si++;
        while(n%i==0)
        {
            c[si-1]++;
            n/=i;
        }
    }
}
int sum[N][20];
int check(int l,int r)
{
    for(int j=0;j<p.size();j++)
    {
        if(sum[r][j]-sum[l-1][j]<c[j])
            return 0;
    }
    return 1;
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        p.clear();
        init(n);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            int num=x;
            for(int j=0;j<p.size();j++)
            {
                sum[i][j]=sum[i-1][j];
                while(num%p[j]==0)
                {
                    num/=p[j];
                    sum[i][j]++;
                }
            }
        }
        int s=-1,e=1e9;
        for(int i=1;i<=n;i++)
        {
            int st=i,en=n,ans=-1;
            while(st<=en)
            {
                int mid=(st+en)>>1;
                if(check(i,mid))
                {
                    ans=mid;
                    en=mid-1;
                }
                else
                    st=mid+1;
            }
            if(ans!=-1)
            {
                if(ans-i<e-s)
                    s=i,e=ans;
            }
        }
        if(s==-1)
            printf("-1\n");
        else
            printf("%d %d\n",s-1,e-1);
    }
    return 0;
}
时间: 2024-10-18 22:16:33

spoj TBATTLE 质因数分解+二分的相关文章

POJ 1845 Sumdiv#质因数分解+二分

题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想:选定一个要进行比较的目标,在区间[l,r]之间不断二分,直到取到与目标相等的值. #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll

【BZOJ2227】【ZJOI2011】看电影 [组合数学][质因数分解]

看电影 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特殊,具体方式如下: 1. 电影院的座位共有K个,并被标号为1…K,每个人买完票后会被随机指定一个座位,具体来说是从1…K中等可能的随机选取一个正整数,设其为L.

Codevs 1313 质因数分解

1313 质因数分解 题目描述 Description 已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 . 输入描述 Input Description 输入只有一行,包含一个正整数 n. 输出描述 Output Description 输出只有一行,包含一个正整数p,即较大的那个质数. 样例输入 Sample Input 21 样例输出 Sample Output 7 #include<iostream> #include<cstdio> #include<cm

HDU 3988 n!质因数分解

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2324    Accepted Submission(s): 569 Problem Description iSea is tired of writing the story of Harry Potter, so, l

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

求n!质因数分解之后素数a的个数

n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do { n/=m; w+=n; }while(n);

3164 质因数分解

3164 质因数分解 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description (多数据)给出t个数,求出它的质因子个数. 数据没坑,难度降低. 输入描述 Input Description 第一行 t 之后t行 数据 输出描述 Output Description t行 分解后结果(质因子个数) 样例输入 Sample Input 2 11 6 样例输出 Sample Output 1 2 数据范围及提示 Data

莫比乌斯函数-质因数分解

1240 莫比乌斯函数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.(据说,高斯(Gauss)比莫比乌斯早三十年就曾考虑过这个函数). 具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如:miu(4), miu(12), miu(18) = 0. 如果一个数不包含平方因子,并且有k个不同的质因子,那么miu(n)

质因数分解(0)&lt;P2012_1&gt;

质因数分解 (prime.cpp/c/pas) [问题描述] 已知正整数n是两个不同的质数的乘积,试求出较大的那个质数. [输入] 输入文件名为prime.in. 输入只有一行,包含一个正整数n. [输出] 输出文件名为prime.out. 输出只有一行,包含一个正整数p,即较大的那个质数. [数据范围] 对于60%的数据,6 ≤ n ≤ 1000. 对于100%的数据,6 ≤ n ≤ 2*109.