D - Tokitsukaze and Multiple
Tokitsukaze can merge two consecutive elements of aa as many times as she wants. After each operation, a new element that equals to the sum of the two old elements will replace them, and thus the length of aa will be reduced by 11.
Tokitsukaze wants to know the maximum possible number of elements that are multiples of pp she can get after doing some operations (or doing nothing) on the sequence aa.
The first line contains an integer TT (1≤T≤20)(1≤T≤20), denoting the number of test cases. Then follow all the test cases.
For each test case, the first line contains two integers nn and pp (1≤n,p≤105)(1≤n,p≤105), denoting the length of the sequence and the special number, respectively.
The second line contains nn integers, where the ii-th integer aiai (1≤ai≤105)(1≤ai≤105) is the ii-th element of aa.
It is guaranteed that the sum of nn in all test cases is no larger than 106106.
OutputFor each test case, output in one line the maximum possible number of elements that are multiples of pp after doing some operations.
Sample Input
2 5 3 2 1 3 2 1 3 1 123 456 789
Sample Output
3 3
Sponsor
题意:
一个序列,每次可以将序列相邻的两个数相加生成一个新的和替代这两个元素,求最终序列里为 p 的倍数的元素最多可能为多少个
思路:
前缀和
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime> #include<deque> #include<iomanip> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include <vector> #include <iterator> #include <utility> #include <sstream> #include <limits> #include <numeric> #include <functional> using namespace std; #define gc getchar() #define mem(a) memset(a,0,sizeof(a)) //#define sort(a,n,int) sort(a,a+n,less<int>()) #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef char ch; typedef double db; const double PI=acos(-1.0); const double eps=1e-6; const int inf=0x3f3f3f3f; const int maxn=1e5+10; const int maxm=100+10; const int N=2e5+10; const int mod=1e9+7; int n = 0; int p = 0; int counter[N] = {0}; int sum[N] = {0}; int ans[N] = {0}; int K[N] = {0}; void init_K() { for(int i = 0;i<p;i++) { K[i] = -1; } } int main() { int T; cin >> T; while(T--) { scanf("%d%d",&n ,&p); init_K(); for(int i = 1;i<=n;i++) { int temp = 0; cin >> temp; counter[i] = (counter[i-1] + temp)%p; } for(int i = 0;i<=n;i++) { sum[i] = K[counter[i]]; K[counter[i]] = i; } for(int i = 1;i<=n;i++) { if(sum[i] != -1) { ans[i] = max(ans[i-1] , ans[sum[i]] + 1); } else { ans[i] = ans[i-1]; } } printf("%d\n",ans[n]); } return 0; }
附第一遍错误思路OT代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime> #include<deque> #include<iomanip> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include <vector> #include <iterator> #include <utility> #include <sstream> #include <limits> #include <numeric> #include <functional> using namespace std; #define gc getchar() #define mem(a) memset(a,0,sizeof(a)) //#define sort(a,n,int) sort(a,a+n,less<int>()) #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef char ch; typedef double db; const double PI=acos(-1.0); const double eps=1e-6; const int inf=0x3f3f3f3f; const int maxn=1e5+10; const int maxm=100+10; const int N=2e5+10; const int mod=1e9+7; int counter[100005] = {0}; void init() { for(int i = 0;i < 100005;i++) counter[i] = 0; } int main() { int T = 0; int n = 0, p = 0; int temp = 0; int ans = 0; cin >> T; while(T--) { init(); ans = 0; cin >> n >> p; for(int i = 0;i<n;i++) { cin >> temp; counter[temp % p] += 1; } ans += counter[0]; for(int i = 1;i <= p/2;i++) { ans += min(counter[i],counter[p-i]); } if(p % 2 == 0) { ans += counter[p/2]/2; } cout << ans << endl; } return 0; }
原文地址:https://www.cnblogs.com/SutsuharaYuki/p/13416909.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android倒计时神器(CountDownTimer)
- Kotlin如何安全访问lateinit变量的实现
- Android实现加载对话框
- Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果
- Android监听键盘状态获取键盘高度的实现方法
- android实现小音频频繁播放
- 每日一个知识点:什么时候会触发Full GC
- 拥抱kotlin之如何习惯使用kotlin高阶函数
- Android中调用另一个Activity并返回结果(选择头像功能为例)
- Android 自定义缩短Toast显示时间的实例代码
- Android Presentation实现双屏异显
- Android中点击按钮启动另一个Activity及Activity之间传值问题
- Android中使用SeekBar拖动条实现改变图片透明度(代码实现)
- Android实现原生锁屏页面音乐控制
- android实现简单音乐播放器