codeforces1670F Jee, You See?(DP/位运算/前缀和/组合数)

题目链接:codeforces 1670F
题目思路:

首先,对于 sum,有
s u m = a 1 + a 2 + ⋯ + a n sum = a_1 + a_2 + \cdots + a_n sum=a1+a2++an
根据前缀的思想,所以得到 ans = sum[r] - sum[l-1]

于是问题转化为和为 sum 的情况下异或和等于 z 的方案数。

不难想到,当 z 的某位上为 1 1 1 时,说明这个位置上的 1 的个数为奇数,反之,则为偶数。

定义 dp[i][j] 为前 i 位且第 i 位上 1 1 1 的剩余个数为 j 的方案数。

定义状态转移方程为 dp[k][t] += dp[k+1][min(n, y - t)] * C[n][t],其中y 是当前位 1 1 1 的个数的最大值。于是可以理解为前 k 位且第 k 位上 1 1 1 的剩余个数为 t 的状态前 k+1 位且第 k+1 位上 1 1 1 的剩余个数为 min(n, y-t) 的状态转移而来。

为什么是 min(n, y-t) 呢?

因为当 1 1 1 的个数超过 n 个时,超过的部分对我们的答案不影响,所以取最小值处理。

那么如何得到这个 y 的值呢?

z 的当前位为 c,枚举 1 1 1 的个数 x,有
y = 2 ∗ x + ( ( s u m > > k ) & 1 ) y=2*x+((sum >> k)\&1) y=2x+((sum>>k)&1)
枚举 t 时,要注意遍历范围和增幅。

参考代码:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值