[codeforces888E]Maximum Subsequence

本文探讨了一种算法,用于解决从一组整数中选择子集的问题,目标是使子集元素之和对特定模数取模后的结果最大化。通过折半搜索策略,将问题分解为两部分进行求解,最终输出最优解。

time limit per test : 1 second
memory limit per test : 256 megabytes

You are given an array a consisting of nnn integers, and additionally an integer mmm. You have to choose some sequence of indices b1, b2, ..., bk(1 ≤ b1 &lt; b2 &lt; ... &lt; bk ≤ n)b_1, b_2, ..., b_k (1 ≤ b_1 &lt; b_2 &lt; ... &lt; b_k ≤ n)b1,b2,...,bk(1b1<b2<...<bkn) in such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value of (∑1kabi)(\sum^k_1 {a_{b_i}})(1kabi) modmodmod mmm.

Input

The first line contains two integers nnn and m(1 ≤ n ≤ 35,1 ≤ m ≤ 109)m (1 ≤ n ≤ 35, 1 ≤ m ≤ 10^9)m(1n35,1m109).
The second line contains nnn integers a1,a2,...,an(1 ≤ ai ≤ 109)a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9)a1,a2,...,an(1ai109).

Output
Print the maximum possible value of (∑1kabi)(\sum^k_1 {a_{b_i}})(1kabi) modmodmod mmm.

Examples

Input

4 4
5 2 4 1

Output

3

Input

3 20
199 41 299

Output

19

Note

In the first example you can choose a sequence b = 1, 2b = {1, 2}b=1,2, so the sum is equal to 777 (and that’s 333 after taking it modulo 444).
In the second example you can choose a sequence b = 3b = {3}b=3.

题意:
nnn个数字里面选任意个,使得他们的和对mmm取模之后最大。输出这个最大值

题解:
折半搜索。先搜出前半段的所有和的结果,存在set里面,然后去后半段搜索的时候,假设当前状态的和取模后是xxx,那么我们就在set里面找小于等于m-x-1的最大数字。(记得用*--upper_bound(x)

tips:
upper_bound找的是大于x的第一个。
lower_bound找的是大于等于x的第一个。

#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int n,m,c[2];
int a[44][2];
int calc(int status,int r,int sit){
    int ret=0;
    for(int i=1;i<=r;i++){
        if(status&(1<<(i-1))){
            ret=(ret+a[i][sit])%m;
        }
    }
    return ret;
}
set<int>tr;
int w33ha(){
    tr.clear();
    int half=n/2;
    c[0]=half;
    c[1]=n-half;
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        if(i<=half)a[i][0]=x;
        else a[i-half][1]=x;
    }
    int ans=0;
    for(int S=0;S<(1<<c[0]);S++){
        int now=calc(S,c[0],0);
        tr.insert(now);
    }
    for(int S=0;S<(1<<c[1]);S++){
        int now=calc(S,c[1],1);
        int G=(*--tr.upper_bound(m-now-1));
        ans=max(ans,G+now);
    }
    printf("%d\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d%d",&n,&m)!=EOF)w33ha();
    return 0;
}
### 解决方案概述 对于给定的问题,在一维坐标系中有 N 个点,每个点具有特定的权重 w。如果两个点之间的距离大于它们各自权重之和,则这两个点之间可以建立一条边。目标是从这些点中找到最大数量的点使得任意两点间均存在边。 为了实现这一目的,可以通过构建不等式 Xi-Wi ≥ Xj+Wj 来确定有向图中的节点关系[^3]。具体来说: - 对于每一对不同的点 (Xi, Wi) 和 (Xj, Wj),当 Xi > Xj 并且 Xi - Wi >= Xj + Wj 成立时,表示从 j 到 i 存在一个方向。 - Xj| >= Wi + Wj 的判断过程。 基于上述分析,算法设计如下: 1. 创建一个新的数组 P[],其中存储的是经过变换后的数据对 (Xi + Wi, Xi - Wi)。 2. 将此新创建的数据集按照第二个分量升序排列;如果有相同的情况,则按第一个分量降序处理。 3. 初始化计数器 count=0 及当前最小右端点 cur_min_right=-∞。 4. 遍历排序后的列表: - 如果当前元素的第一个分量大于等于cur_min_right,则更新count并设置新的cur_min_right为该元素的第二分量。 5. 输出最终的结果即为所求的最大团大小。 以下是 Python 实现代码示例: ```python def max_clique(n, points): # 计算转换后的点集合 [(xi+wi, xi-wi)] transformed_points = sorted([(points[i][0]+points[i][1], points[i][0]-points[i][1]) for i in range(n)], key=lambda x:(x[1], -x[0])) result = 0 current_end = float('-inf') for start, end in transformed_points: if start >= current_end: result += 1 current_end = end return result ``` 通过这种方法能够有效地解决问题,并获得最优解。值得注意的是,这里采用了一种贪心策略来逐步增加符合条件的顶点数目,从而保证了结果是最优的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值