首页 > ACM题库 > HDU-杭电 > HDU 4648-Magic Pen 6-数论-[解题报告]HOJ
2015
09-17

HDU 4648-Magic Pen 6-数论-[解题报告]HOJ

Magic Pen 6

问题描述 :

In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a magic pen, discover has a magic pen. Recently, Timer also got a magic pen from seniors.

At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean’s office. Most of these students are Timer’s friends, and Timer doesn’t want to see them fail the course. So, Timer decides to use his magic pen to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students’ scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students’ scores module M the same.

Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.

输入:

There are multiple test cases.
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,…an (-100000000 <= a1,a2,…an <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)

输出:

There are multiple test cases.
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,…an (-100000000 <= a1,a2,…an <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)

样例输入:

2 3
1 6
3 3
2 3 6
2 5
1 3

样例输出:

1
2
0
Hint
The magic pen can be used only once to scratch out consecutive students.

采用一种新的方式来处理数据连续的数目和能被m整除,时间复杂度为n

#include <cstdio>
#include <cstring>
int max(int x,int y)
{
    return x>y?x:y;
}
#define LL long long
LL num[100010];
int p[100010];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        num[0]=0;
        memset(p,-1,sizeof(int)*(m+1));
        for(int i=1; i<=n; ++i)
        {
            scanf("%I64d",&num[i]);
            num[i]+=num[i-1];
            LL c=num[i]/m;
            if(num[i]<0) num[i]=(1-c)*m+num[i];
            else num[i]=num[i]%m;
            if(p[m-num[i]]==-1||p[m-num[i]]>i) p[m-num[i]]=i;
        }
        int ans=0;
        for(int i=1; i<=n; ++i)
            if(num[i]%m==0) ans=max(i,ans);
            else ans=max(ans,i-p[m-num[i]]);
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/moyan_min/article/details/12044475