题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1134
题解:简单的一道组合题,现求一下前缀和,然后只要找前缀和膜m的结果相同的值相互组合就行。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long ll; const int M = 1e5 + 10; ll a[M] , sum[M] , num[M]; int main() { int t , Case = 0; scanf("%d" , &t); while(t--) { int n , m; scanf("%d%d" , &n , &m); sum[0] = 0; for(int i = 0 ; i <= m ; i++) num[i] = 0; for(int i = 1 ; i <= n ; i++) scanf("%lld" , &a[i]) , a[i] %= m , sum[i] = a[i] + sum[i - 1]; for(int i = 1 ; i <= n ; i++) num[sum[i] % m]++; ll ans = 0; for(int i = 0 ; i < m ; i++) { if(i == 0) { ans += (num[i] * (num[i] + 1) / 2); } else ans += (num[i] * (num[i] - 1) / 2); } printf("Case %d: %lld " , ++Case , ans); } return 0; }