Tokitsukaze has a sequence of length nn, denoted by aa.
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.
InputThere are several test cases.

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;
}

  

D - Tokitsukaze and Multiple的更多相关文章

  1. hdu 6794 Tokitsukaze and Multiple 前缀和思想+思维

    题意: t组输入,给你一个长度为n的数组,你每次可以从数组中找到a[i]和a[i+1],然后用a[i]+a[i+1]这个新元素来覆盖掉a[i]和a[i+1]的位置(1<=i<n),从而数组 ...

  2. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  5. SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“

    最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...

  6. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  7. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher

    加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...

  8. scala - multiple overloaded alternatives of method bar define default arguments

    同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...

  9. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...

  10. 多文档上传(upload multiple documents)功能不能使用怎么办?

    问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...

随机推荐

  1. JS/Jquery检查网络路径文件是否存在

    var url='网络文件路径'; var isExists; $.ajax(url, { type: 'HEAD', dataType: 'text', async: false, success: ...

  2. JAVA 循环删除list中元素的方法总结

    摘要:介绍List集合实现元素边遍历边删除的方法,例如removeIf和迭代器iterator.remove()等. 综述   List集合是我们开发中经常使用到的一种集合形式,有时候会遇到在遍历Li ...

  3. redis碰到的问题

    SpringBoot整合redis 连接报错: Unable to connect to Redis; nested exception is org.springframework.data.red ...

  4. 【译】Agent 模式现已全面推出并支持 MCP

    Copilot Agent 模式是 AI 辅助开发的又一次革新,如今已在 Visual Studio 六月更新版中全面推出. Agent 模式将 GitHub Copilot 转变为一个能够自主完成多 ...

  5. Elastic学习之旅 (10) Logstash数据采集

    大家好,我是Edison. 上一篇:结构化搜索 Logstash是啥? Logstash是一款优秀的开源ETL工具/数据搜集处理引擎,可以对采集到的数据做一系列的加工和处理,目前已支持200+插件具有 ...

  6. java基础--List

    List基本属性和方法移步官方文档: List (Java Platform SE 8 ) 1.处理最简单的List<String>: (1)并集.交集.差集 并集: 如果只用List.a ...

  7. Rust修仙之道 第九章 静流境 · 生命周期与智能灵枢之术

    第九章:静流境 · 生命周期与智能灵枢之术 "灵不可散无归,术不可失其主.唯知存亡之律,方可掌控万象之根." 顾行云开始接触复杂灵术:多个术式调用.灵力相互交织,导致引用失效.灵气 ...

  8. Luogu P11158 【MX-X6-T4】夢重力 题解

    P11158 [MX-X6-T4]夢重力 分类讨论好题. 不难发现交换行等价于交换列,考虑转化贡献体,枚举长度为 \(\frac{n}{2}\) 区间,统计这个区间被多少种交换方式包含. 考虑一个长度 ...

  9. 前端开发系列079-Node篇之npm+

    本文介绍NPM系列核心工具(npm.nrm.npx和nvm)的基本使用和常用的命令. 核心工具 npm(node package manager) Node的包管理工具,我们可以利用该工具来搜索.下载 ...

  10. leetcode 1405

    简介 贪心算法 思路 填2个数量最多的字母,如果下一次该字母数量还是最多的,填1个数量次多的字母 code class Solution1405 { public: string longestDiv ...