SPOJ Thor vs Frost Giants 数论

来源:互联网 发布:生辰八字高分起名软件 编辑:程序博客网 时间:2024/05/29 16:53

TBATTLE - Thor vs Frost Giants

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 15
Output:
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].

题目链接

题意:给你一个含有n段强度的巨人,巨人的强度是多段强度的乘积,你可以产生n的倍数的能量,只有当你产生的能量和巨人某一段强度的乘积相同,你才能杀死巨人(其实就是集合里面连续的子串乘积要为n的倍数就好了…题目出的这么复杂,英语不好的我看了n久啊…),然后让你求子串里最短长度的能够杀死巨人的区间,如果存在多个长度最短的区间就输出起始位置最小的那个区间。

解题思路,分解n的质因子并统计每个的数量,然后统计那n段强度能整除n的质因子的个数,最后用一个l,r维护就好,r一直向右移直到符合条件,然后再l向右移直到不符合条件,判断l-1和r的区间长度是否为最小,一直维护到r==n就好。当然要判断一下n是否等于1…

#include<cstdio>#include<iostream>#include<algorithm>#define maxn 100005using namespace std;int n,pl1=0,pl,b[maxn],c[maxn],d[maxn]; //c,d数组统计1-100000的素数struct node{    int pr,num;}a[10]; //pr表示能被n整除的素数,num表示这个素数能被整除的个数int aa[maxn][10];int main(){    scanf("%d",&n);    for(int i=0;i<n;i++)    scanf("%d",&b[i]);    if(n==1){        printf("1\n");        return 0;    }    for(int i=2;i<maxn/2;i++){        for(int j=2;i*j<maxn;j++)   c[i*j]=1;    }    for(int i=2;i<maxn;i++){        if(c[i]==0) d[pl1++]=i;    }    int s=n;    while(s!=1){        for(int i=0;i<pl1;i++){            if(s%d[i]==0){                a[pl].pr=d[i];                while(s%d[i]==0){                    a[pl].num++;                    s/=d[i];                }                pl++;            }        }    }    for(int i=0;i<n;i++){        for(int j=0;j<pl;j++){            while(b[i]%a[j].pr==0){                aa[i][j]++;                b[i]/=a[j].pr;            }        }    }    int l=0,r=0,ans1,ans2,len=999999,flag=0,num=0;    while(r<n){        num=0;        for(int i=0;i<pl;i++){            a[i].num-=aa[r][i];        }        for(int i=0;i<pl;i++){            if(a[i].num<=0) num++;        }        if(num==pl){            flag=1;            while(1){                num=0;                for(int i=0;i<pl;i++){                    a[i].num+=aa[l][i];                }                for(int i=0;i<pl;i++){                    if(a[i].num<=0) num++;                }                if(num!=pl&&r-l+1<len){                    len=r-l+1;                    ans1=l;                    ans2=r;                }                l++;                if(num!=pl) break;            }        }        r++;    }    if(flag==0) printf("-1\n");    else printf("%d %d\n",ans1,ans2);    return 0;}

做出答案后看到网上很多人用二分做的…待我去看看~

0 0
原创粉丝点击