#737 div2 B 思维 C 组合数学+位运算

B. Moamen and k-subarrays

#include<bits/stdc++.h>
#define ll long long
#define _ 0
using namespace std;
const int maxn = 2e5 + 9;
const int inf = 0x3f3f3f3f;
struct node
{
	int x, y;
}a[maxn];
bool cmp(node a, node b)
{
	return a.x < b.x;
}
void work()
{
	int n, k;
	cin >> n >> k;
	for(int i = 1; i <= n; ++i)	
		scanf("%d", &a[i].x), a[i].y = i;	
	sort(a+1,a+1+n,cmp);
	int t = 0;
	for(int i = 2; i <= n; ++i)
		if(a[i].y != a[i-1].y + 1) ++t;
	
	if(k >= t + 1) cout << "YES\n";// t个断点,需要至少分成t+1段 
	else cout << "NO\n";
}

int main()
{
	int TT;cin>>TT;while(TT--)
	work();
	return ~~(0^_^0);
}

C. Moamen and XOR
题意:
给定 n n n k k k,求构造出满足不等式的数组的方案数
思路:
考虑去讨论不等式两边的每一位
位运算对于每一位只会有两种结果 0 0 0 1 1 1,因此可以从两边的值入手讨论
合法的情况无非就是: 1 > = 0 , 1 > = 1 , 0 > = 0 1>=0,1>=1,0>=0 1>=0,1>=1,0>=0
进而可以发现本题思想:如果高位胜出,低位任意取;高位平局,继续往下算
等式左边若为 1 1 1,则这一位全为 1 1 1,会发现等式右边的值与 n n n 的奇偶性有关,因此还要讨论奇偶性
n为奇数:
可以发现奇数的情况只能去构造两边所有位相等平局的情况,不会出现 1 > = 0 1>=0 1>=0

  1. 1 > = 1 1>=1 1>=1,只能全选 1 1 1,方案数为 1 1 1
  2. 0 > = 0 0>=0 0>=0,满足选偶数个 1 1 1 即可 ,方案数为 C n 0 + C n 2 + . . . + C n n − 1 = 2 n − 1 C_n^0+C_n^2+...+C_n^{n-1}=2^{n-1} Cn0+Cn2+...+Cnn1=2n1

方案数即为 2 n − 1 + 1 2^{n-1}+1 2n1+1
一共有 k k k
因此答案就是 ( 2 n − 1 + 1 ) k (2^{n-1} + 1)^k (2n1+1)k

n为偶数
可以发现偶数可以构造出 1 > = 0 1>=0 1>=0
先考虑构造平局,可以发现不会有 1 > = 1 1>=1 1>=1 的平局

  1. 0 > = 0 0>=0 0>=0,选偶数个 1 1 1,但不能选 n n n 个,方案数为 C n 0 + C n 2 + . . . + C n n − 2 = 2 n − 1 − C n n = 2 n − 1 − 1 C_n^0+C_n^2+...+C_n^{n-2}=2^{n-1}-C_n^n=2^{n-1}-1 Cn0+Cn2+...+Cnn2=2n1Cnn=2n11 k k k 位答案即为 ( 2 n − 1 − 1 ) k (2^{n-1}-1)^k (2n11)k
  2. 1 > = 0 1>=0 1>=0,枚举一个 i i i,当第 i + 1 ∼ k − 1 i+1∼k−1 i+1k1 位相同即全为 0 0 0,第 i i i 位与起来是 1 1 1,异或起来是 0 0 0,第 0 ∼ i − 1 0∼i−1 0i1 位随便时,对于第 i + 1 ∼ k − 1 i+1∼k−1 i+1k1 位,每一位有 C n 2 + C n 4 + ⋯ = 2 n − 1 − 1 C_n^2+C_n^4+⋯=2^{n−1}−1 Cn2+Cn4+=2n11 种可能,第 i i i 位只能全是 1 1 1,第 0 ∼ i − 1 0∼i−1 0i1 位每一位有 2 n 2^n 2n 种可能,一共就有 ( 2 n − 1 − 1 ) k − i − 1 ( 2 n ) i (2^{n−1}−1)^{k−i−1}(2^n)^i (2n11)ki1(2n)i 种可能。

code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;

ll q_pow(ll a, ll b){
	ll ans = 1;while(b){
		if(b&1) ans=ans*a%mod;b>>=1;a=a*a%mod;
	}return ans;
}
void work()
{
	cin >> n >> m;
	if(n & 1){
		ll d = q_pow(2, n - 1) + 1;
		cout << q_pow(d, m) << endl;
	}
	else{
		ll d = q_pow(2, n - 1) - 1;
		ll ans = q_pow(d, m);
		for(int i = 0; i < m; ++i)
			(ans += q_pow(d, m - 1 - i) * q_pow(2, n * i)) %= mod;
		cout << ans << endl;
	}
}

int main()
{
	ios::sync_with_stdio(0);
	int TT;cin>>TT;while(TT--)
	work();`在这里插入代码片`
	return 0;
}

[NCT058B]清新题
思路:
一道类似的题
有点难
题解
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
ll p[maxn];
ll fac[maxn], inv[maxn];

ll q_pow(ll a, ll b){
	ll ans = 1;while(b){
		if(b & 1) ans = ans * a % mod;b >>= 1; a = a * a % mod;
	}return ans;
}
void work()
{
	p[0] = 1;
	for(int i = 1; i <= maxn - 9; ++i) p[i] = p[i-1] * 2 % mod;
	cin >> n >> m;
	ll ans = q_pow(p[2 * n - 1], m);// 等式两边相等
	for(int i = 1; i <= m; ++i){
		ll x = q_pow(p[2 * n - 1], i - 1);// 高位等于 
		ll y = q_pow(p[2 * n], m - i);// 低位随便选
		(ans += x * (p[n] - 1) % mod * p[n - 1] % mod * y % mod) %= mod;
	}
	cout << ans;
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值