Codeforces数学1600----day1[同余定理,树状数组+两次二分,,组合计数]

1.C. Kuroni and Impossible Calculation
在这里插入图片描述
**知识点:同余定理 **
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define wei(a) fixed<<setprecision(a)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define f first
#define s second
using namespace std;
const int N = 2e5 + 10;
const double pi = acos(-1);
const double eps = 1e-9;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
LL a[N];
int main()
{
    LL n, m;
    cin >> n >> m;
    _for(i,0,n) cin >> a[i];
    if(n - 1 > 1000) 
    {
        cout << "0";
        return 0;
    }
    LL res = 1;
    _for(i,0,n)
     _for(j,i+1,n)
      res *= abs(a[i] - a[j]), res %= m;
    cout << res << endl;
    return 0;
}

2.D. MEX maximizing
题目大意:初始你有一个空的数组a[] = {};你每次都会向数组里面插入一个数字每次你都可以对数组里面的字进行任意次(±)x,问你在数组中没出现过的最小非负整数是多少?
思路:1.我们观察可以发现题目可以转化成是从0.1.2.3.4…n这样排列下去最长是少?
2.观察数据范围很明显一个数可以写成a = kx + b;n的大小是4e5,而插入的ai是1e9次方那么我们就可以把ai %= x, 把它变到这个范围里面。
3.我们刻意开一个数组记录一下0到n中有多少个数出现了,如插入的这个数以经有了那么我们就让它加上若干个x始得达到第一次没出现过的最小数为止,那么这个我们可以二分
4.插入之后我们就可以二分去查找最长的0,1,2,3,,,,n了,这里我们有树状数组维护一下就可以了(二分去查找前缀和==Mid的位置)[这里要特判一下0因为树状数组无法从0开始]

#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define wei(a) fixed<<setprecision(a)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define f first
#define s second
using namespace std;
const int N = 4e5 + 10;
const double pi = acos(-1);
const double eps = 1e-9;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
int q, x;
bool vis[N << 1];
int tree[N << 1];
inline void add(int x)
{
    while(x <= N)
    {
        tree[x] += 1;
        x += lowbit(x);
    }
}

inline LL sum(LL x)
{
    LL res = 0;
    while(x)
    {
        res += tree[x];
        x -= lowbit(x);
    }
    return res;
}

bool judge(LL Mid, LL num)
{
    if(1ll * num + x * Mid * 1ll > 4e5) return true;
    if(!vis[num + x * Mid]) return true;
    return false;
}

int check(LL num)
{
    int l = 0, r = 4e5;
    while(l < r)
    {
        if(judge(mid,num)) r = mid;
        else l = mid + 1;
    }
    return l;
}


int main()
{
    cin >> q >> x;
    while(q --)
    {
        LL num;
        cin >> num;
        num %= x;
        LL next = check(num);
        vis[num + x * next] = true;
        // cout << num << " -----  " << next << endl;
        if(num + x * next)  add(num + x * next);
        if(!vis[0]) 
        {
            cout << "0" << endl;
            continue;
        }
        int l = 0, r = N;
        while(l < r)
        {
            int Mid = l + (r - l + 1 >> 1);
            if(sum(Mid) == Mid) l = Mid;
            else r = Mid - 1;
        }
        cout << l + 1 << endl;
    }
    return 0;
}

C. New Year and Permutation

这个题很有意思呀。就是给你一个n,求n的[所有全排列]里区间左右端点的差等于区间内极差的区间的和。
1.其实手枚一下就可以发现,其实满足这个条件的区间内的数一定是连续的。
2.所以我们从区间长度入手,对于每个长度的区间看对应了多少个排列满足要求。
3.这就很简单了呀。我们枚举区间的极差i,考虑选差值为i的i+1个数有n-i种选法,区间内的顺序随意,整体看这i+1个数为一组,另外的n-i-1个数为n-i-1个组,这n-i个组的顺序同样随意。
所以就有:
`

在这里插入图片描述

#define maxn 250010
LL fact[maxn],ans=0;
int main(){
	int i,n,m;
	scanf("%d%d",&n,&m);
	fact[0]=1;
	for(i=1;i<=n;i++)fact[i]=fact[i-1]*i%m;
	for(i=0;i<=n-1;i++)ans=(ans+(n-i)*fact[i+1]%m*fact[n-i]%m)%m;
	printf("%lld\n",ans);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值