poj 3844 Divisible Subsequences

一道简单的组合数学题,我们把从1~m的和的余数相同的放在一个集合里面,那么他们任意一个相减就会是得到余数为0;那么也是d的倍数;这里要注意的是当余数为0的集合那么可以从中任意选2个与1个,这个题会超出int型:

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

#include<string>

#define LL long long

using namespace std;

LL mod[1000024];

LL Solve( int n )

{

    LL ans = mod[0];

    for( int i = 0; i < n ; i ++ )

    {

        if( mod[i]>1 )

        {

        ans += (mod[i]*(mod[i]-1))/2;

        }

    }

    return ans;    

}

int main(  )

{

    int num,sum,n,MOD;

    int T;

    while( scanf( "%d",&T )==1 )

    {

        while( T-- )

        {

            int sum = 0;

            memset( mod , 0 , sizeof( mod ) );

            scanf( "%d %d",&MOD,&n );

            for( int i = 0 ; i < n ; i ++ )

            {

                 scanf( "%d",&num );

                 sum = (sum + num)%MOD;

                 mod[sum]++;    

            }

            printf( "%I64d\n",Solve( MOD ) );

        }

    }

    return 0;

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(sequence)