题目地址: http://poj.org/problem?id=3844
思路: a[i]+a[i+1]+...+a[j]=s[j]-s[i]; 于是整除等价于 s[i]===s[j] (mod d); 然后统计出现了多少次 c[n][2]就可以了 。 值得注意的是,有可能50000* (50000-1)/2 要用long long
每次进入一个新case 后把p 清零,sum【i】表示前i个数的和, 0=<i<=n
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int sum[50005]; int p[1000000]; int main() { int T; cin>>T; int d; int n; while(T--) { cin>>d>>n; int temp; memset(p,0,sizeof(p)); for(int i=0;i<n;i++) { scanf("%d",&temp); sum[i+1]=(sum[i]+temp)%d; p[sum[i+1]]++; } p[0]++; long long ans=0; for(int i=0;i<d;i++) { long long temp=p[i]; if(temp>=2) ans+=temp*(temp-1)/2; } cout<<ans<<endl; } }