SPOJ:TBATTLE - Thor vs Frost Giants(数论)

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].

原题链接:http://www.spoj.com/problems/TBATTLE

题意:给出N个数字,求一个各数乘积可以被N整除的最小区间,输出该区间的范围。

将N分解质因数,再将N个数字分解质因数,从左到右枚举右端点,显然该过程左端点也在递增。

# include <stdio.h>
# include <string.h>
# define MAXN 100000
int prime[MAXN+3], p[20], n[20], f[MAXN+3][20];
bool b[MAXN+3];
int main()
{
    int len=0, icount, l, r, i, j, k, N, m, left, imin, temp;
    memset(b, false, sizeof(b));
    for(i=2; i<=MAXN; ++i)
        if(!b[i])
        {
            prime[++len] = i;
            for(j=i; j<=MAXN; j+=i)
                b[j] = true;
        }
    while(~scanf("%d",&N))
    {
        imin = 0x3f3f3f3f;
        icount = 0;
        m = N;
        memset(f, 0, sizeof(f));
        memset(p, 0, sizeof(p));
        memset(n, 0, sizeof(n));
        for(i=1; prime[i]*prime[i] <= m && i<=len; ++i)
        {
            if(!(m%prime[i]))
            {
                p[++icount] = prime[i];
                n[icount] = 0;
            }
            for(;!(m%prime[i]); ++n[icount],m/=prime[i]);
        }
        if(m > 1)
            p[++icount] = m, n[icount] = 1;
        for(i=1; i<=N; ++i)
        {
            scanf("%d",&temp);
            for(j=1; j<=icount; ++j)
                f[i][j] = f[i-1][j];
            for(j=1; j<=icount; ++j)
                for(;!(temp%p[j]); ++f[i][j],temp/=p[j]);
        }
        l = -1; r = N; j=0;
        for(i=1; i<=N; ++i)
        {
            for(;j<i; ++j)
            {
                bool ok = true;
                for(k=1; k<=icount; ++k)
                {
                    if(f[i][k]-f[j][k] < n[k])
                    {
                        ok = false;
                        break;
                    }
                }
                if(!ok)
                    break;
                if(i-j+1 < imin)
                {
                    l = j+1;
                    r = i;
                    imin = i-j+1;
                }
            }
        }
        if(l == -1)
            puts("-1");
        else
            printf("%d %d\n",l-1,r-1);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值