裴蜀定理 + lowbit

Problem - D - Codeforces

题意

定义一个包含 \(m\) 个元素的数组 \(b\) 是好的,当且仅当满足以下两个条件

  1. 对于 \(b[i]\), 存在一个长度为 \(b[i]\) 的连续的段(如 \(b[i]=4\), [1,2,3,4], [-1,0,1,2]等就是符合条件的)
  2. 对于这 \(m\) 个段,\(sum_i\) 记为这一段的数字和,并满足 \(\sum sum_i=0\)]

给定一个长度为 \(n\;(1<=n<=2*10^5)\) 的数组 \(a[i]\),求 \(a\) 的 \(2^n-1\) 个非空子序列组成的数组中,有多少个是好的

思路

  1. 对于 \(a[i]\) 而言,设选取的一段的第一个数是 \(x_i\), 则 \(sum_i=a_i*x_i+\frac {a_i*(a_i-1)}2\)

  2. 若选了 \(a\) 数组的 \(k\) 个数,记为 \(c_1,c_2...c_k\)

    \(\sum sum_i=0\Lrarr \sum c_i*x_i=\sum \frac {c_i*(c_i-1)}2\)

​ 根据裴蜀定理,设 \(g=\gcd(c_1,c_2,...,c_k)\), 则要满足 \(g\mid \sum \frac {c_i*(c_i-1)}2\)

  1. 如果 \(c_i\) 中存在奇数,则 \(g\) 也一定是奇数,所以右边的除以2不影响整除的性质,又因为 \(g\mid c_i\), 因此 \(g\mid \sum \frac {c_i*(c_i-1)}2\) 恒成立

    所以奇数部分对方案数的贡献就是 \(2^{odd}\) (也可以感性考虑,如果存在一个奇数,那么它和其余所有的偶数共有奇数个数,把它们按关于 0 对称排列即可;其余的奇数也关于 0 对称排列)

  2. 现在只关心偶数部分,因为 \(g\mid c_i\), 问题是右边的除以 2 之后还能否满足 \(g\mid \sum \frac {c_i*(c_i-1)}2\),(即关心的是 \(c_i\) 的含 2 量) 由于 \(c_i-1\) 为奇数,对除以 2 而言没有意义,不用考虑;

  3. 现在转化为 \(g\mid \sum \frac {c_i}2\) 是否成立,记 \(lowb[x]\) 为能整除 \(x\) 的最高的 2 的幂次,例如 \(lowb[8]=3,lowb[16]=4\)

    \(lowb[g]<=lowb[c_i]\)

    1. \(lowb[c_i]>lowb[g]\), 那么 \(\frac {c_i}2\) 仍能被 \(g\) 整除
    2. \(lowb[c_i]==lowb[g]\), 需要偶数个相等的 \(c_i\) 才能使 \(g\mid \sum \frac {c_i}2\) (需要用到 \(\binom n0+\binom n2+\binom n4+...=2^{n-1}\) 来优化)

    代码

#include <bits/stdc++.h>
using namespace std;
#define endl "\n" typedef long long ll;
typedef pair<int, int> PII; const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int n;
int a[N];
int lowb[40];
ll mi[N]; void add(ll &a, ll b)
{
a += b;
if (a >= mod)
a -= mod;
} void presolve()
{
mi[0] = 1;
for (int i = 1; i <= n; i++)
mi[i] = mi[i-1] * 2 % mod;
for (int i = 1; i <= n; i++)
{
int x = a[i], cnt = 0;
while(x % 2 == 0)
{
x /= 2;
cnt++;
}
lowb[cnt]++;
}
} ll solve()
{
ll ans = 0;
int even = 0;
for (int i = 1; i <= n; i++)
if (a[i] % 2 == 0) even++;
//至少一个奇数 + 任意个偶数
add(ans, mi[even] * (mi[n - even] - 1 + mod) % mod);
//只有偶数
for (int i = 1; i <= 30; i++)
{
if (!lowb[i])
continue;
even -= lowb[i];
//枚举拿的最小的lowbit,且要拿偶数个,C(n,0) + C(n,2) + C(n,4) + ... == 2^(n-1),且不能拿0个
add(ans, mi[even] * (mi[lowb[i] - 1] - 1 + mod) % mod);
}
return ans;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
presolve();
cout << solve() << endl;
return 0;
}

Codeforces Global Round 17 - D. Not Quite Lee的更多相关文章

  1. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  2. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  3. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  4. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  5. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  6. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  7. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  8. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

  9. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  10. Codeforces Beta Round #17 C. Balance DP

    C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...

随机推荐

  1. vue3 loading 等待效果

    一.自定义组件 loading.vue <template> <div class="loading" v-show="msg.show"&g ...

  2. golang流程控制if,switch分支

    if 分支 if 单分支 if 条件表达式 { 逻辑代码 } package main import "fmt" func main() { //var a int = 9 //i ...

  3. nfs-client-provisioner 利用NFS动态提供Kubernetes后端存储卷

    nfs-client-provisioner 利用NFS动态提供Kubernetes后端存储卷     一.选一个节点安装nfsserver 服务   yum install nfs-common n ...

  4. Linux安装Nginx安装并配置stream

    编译安装 1.下载可编译的nginx cd /opt wget http://nginx.org/download/nginx-1.20.1.tar.gz tar -zxvf nginx-1.20.1 ...

  5. MSSql 跨服务器查询解决方案

    先确定从服务器是否允许有外部链接的服务器: select * from sys.servers 没有的话,需要添加服务器链接: EXEC sp_addlinkedserver @server='10. ...

  6. IIS7无法访问.apk文件的解决方法

    随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站客户端,.apk文件就是安卓(Android)的应用程序后缀名,默认情况下,使用IIS作为Web服务器的无法访问下载此 ...

  7. RocketMq 下载安装

    下载地址 https://rocketmq.apache.org/zh/download linux安装步骤 启动nameserver bin目录下启动nameserver nohup sh mqna ...

  8. 重磅!Cloud Ace 首度于德国设立法人,作为拓展Google Cloud 欧洲整合市场的基础

    [Cloud Ace - 谷歌云 | 亚太地区最多据点的Google Cloud经销商 ]   Cloud Ace, Inc.(总部:东京都千代田区,代表:青木诚,以下简称 Cloud Ace)在20 ...

  9. vue 添加代理

    1.跟目录下新建vue.config.js文件,添加内容: module.exports = {   assetsDir: 'static',   parallel: false,   publicP ...

  10. Adobe Acrobat PDF Reader DC软件下载

    安装包下载 https://get.adobe.com/en/reader/enterprise/ ftp下载,按日期排序 ftp://ftp.adobe.com/pub/adobe/reader/w ...