lightoj 1134 - Be Efficient
来源:互联网 发布:京东数据分析师待遇 编辑:程序博客网 时间:2024/05/16 23:33
You are given an array with N integers, and another integer M. You have to find the number of consecutive subsequences which are divisible by M.
For example, let N = 4, the array contains {2, 1, 4, 3} and M = 4.
The consecutive subsequences are {2}, {2 1}, {2 1 4}, {2 1 4 3}, {1}, {1 4}, {1 4 3}, {4}, {4 3} and {3}. Of these 10 'consecutive subsequences', only two of them adds up to a figure that is a multiple of 4 - {1 4 3} and {4}.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains two integers N (1 ≤ N ≤ 105) and M (1 ≤ M ≤ 105). The next line contains N space separated integers forming the array. Each of these integers will lie in the range [1, 105].
Output
For each case, print the case number and the total number of consecutive subsequences that are divisible by M.
Sample Input
Output for Sample Input
2
4 4
2 1 4 3
6 3
1 2 3 4 5 6
Case 1: 2
Case 2: 11
n代表输入4个数,问你有多少个子串和能被m整除。
先说一个定理:两个前缀和对m的余数相同,那么中间那段的和就是m的倍数。
dp[i]表示前缀和余数为i出现的次数。因为子串可以和前面的进行组合,所以出现2次的时候加1,出现3次的时候加2,以此类推。。。。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define inf 1e9#define endl '\n'#define LL long longint main(void){ int T,n,m,i,x; int dp[100010]; scanf("%d",&T); int cas = 1; while(T--) { scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); int mod = 0; LL ans = 0; dp[0] = 1; for(i=0;i<n;i++) { scanf("%d",&x); mod = (mod + x)%m; ans += dp[mod]; dp[mod]++; } printf("Case %d: %lld\n",cas++,ans); }}
- LightOJ - 1134 Be Efficient
- lightoj 1134 - Be Efficient
- lightoj-1134-Be Efficient
- LightOJ - 1134 Be Efficient(DP)
- lightoj 1134 - Be Efficient DP 思路
- Light oj 1134 - Be Efficient
- Light oj 1134 - Be Efficient(dp)
- Light OJ 1134 Be Efficient(水题)
- lightoj1134 Be Efficient
- LightOJ 1210 Efficient Traffic System
- LightOj 1054 Efficient Pseudo Code
- lightoj 1210 - Efficient Traffic System
- LightOJ - 1210 Efficient Traffic System(强连通)
- [强连通分量]LightOJ 1210 - Efficient Traffic System
- LightOJ 1210 - Efficient Traffic System【强连通图】
- JOJ 2442: Be efficient 线段树的做法
- lightoj 1054 - Efficient Pseudo Code 求所有n^m的所有因子和
- LightOJ 1054 - Efficient Pseudo Code (求n^m的因子和)
- 1196 Lowest Bit
- jquery隔行变色
- 黑马程序员高端iOS培训:苹果一条公告搅混了大家的脑洞
- jQuery、AJAX基础学习笔记
- mysql主从复制错误sql-running no
- lightoj 1134 - Be Efficient
- Numpy和Matplotlib的安装
- 浅谈Linux内存管理机制
- HDU 5381 The sum of gcd(数论+莫队算法)
- 数据结构第2章上机实验题2.1
- [Machine Learning]---ROC(Receiver Operating Characteristic) Curve
- CSS学习笔记
- matlab自带滤波器函数小结(图像处理)
- 深度好文丨读完此文,就知道Hadoop了!