CSP-S全国模拟赛第二场 【nan】

A.count

本场比赛最难的题...

隔板法组合数容斥 xjb 搞搞就好了

//by Judge
#include<cstdio>
#include<iostream>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define ll long long
using namespace std;
const int mod=998244353;
const int M=1e7+3;
ll n; int m,k,mx,r,ans,fac[M],inv[M];
inline int mul(int x,int y){return 1ll*x*y%mod;}
inline void Pls(int& x,int y){if((x+=y)>=mod)x-=mod;}
inline int qpow(int x,int p=mod-2){ int s=1;
    for(;p;p>>=1,x=mul(x,x)) if(p&1) s=mul(s,x); return s;
}
inline int C(ll n,int m){
    if(n<mx) return mul(fac[n],mul(inv[m],inv[n-m]));
    Rg int s=inv[m]; fp(i,0,m-1) s=mul(s,(n-i)%mod); return s;
}
inline int f(int n){ if(n<0) return 0; int res=0,tp;
    fp(i,0,k) tp=mul(C(k,i),C(n-i*(m-1)+k-1,k-1)), // 选 i 个要放零头的,乘上其放置方案
        Pls(res,mul(i&1?mod-1:1,tp)); return res;
}
int main(){ scanf("%lld%d%d",&n,&m,&k);
    mx=k*m,r=n%m; if(!m) return !printf("0\n");
    fac[0]=1; fp(i,1,n) fac[i]=mul(fac[i-1],i);
    inv[n]=qpow(fac[n]); fd(i,n,1) inv[i-1]=mul(inv[i],i);
    fp(i,0,k) Pls(ans,mul(f(i*m+r-k),C((n-r)/m-i+k-1,k-1)));
    // 前面的 f ,求出零头放置方案(放到 k 个数上全都小于 m)
    // 后面的 C ,组合数解隔板法
    return !printf("%d\n",ans);
}

tree

很水的题 \(n^2\) 的复杂度,随便搜搜就好了

//by Judge
#include<cstdio>
#include<cstring>
#include<iostream>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(Rg int i=head[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
#define open(S) freopen(S".in","r",stdin),freopen(S".out","w",stdout)
#define ll long long
using namespace std;
const int mod=998244353;
const int M=1e5+3;
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int Max(int x,int y){return x>y?x:y;}
inline int Min(int x,int y){return x<y?x:y;}
inline int mul(int x,int y){return 1ll*x*y%mod;}
inline void Pls(int& x,int y){if((x+=y)>=mod)x-=mod;}
inline int read(){ int x=0,f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} int n,x,y,root,ans,a[M]; int pat,head[M];
struct Edge{ int to,nxt; }e[M<<1];
inline void add(int u,int v){
    e[++pat]={v,head[u]},head[u]=pat;
    e[++pat]={u,head[v]},head[v]=pat;
}
void dfs(int u,int fa,int mn,int mx){
    if(u>=root) Pls(ans,mul(mn,mx));
    go(u) if(v^fa) dfs(v,u,Min(mn,a[v]),Max(mx,a[v]));
}
int main(){ n=read(); fp(i,1,n) a[i]=read();
    fp(i,2,n) x=read(),y=read(),add(x,y);
    fp(i,1,n) dfs(root=i,0,a[i],a[i]); return !printf("%d\n",ans);
}

distance

n·q ,水题,注意别炸精度就好了

//by Judge
#include<cstdio>
#include<cstring>
#include<iostream>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(Rg int i=head[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
#define ll long long
using namespace std;
const int M=2003;
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline void cmax(ll& x,ll y){if(x<y)x=y;}
inline int Min(ll x,ll y){return x<y?x:y;}
inline int read(){ int x=0,f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} char sr[1<<21],z[20];int CCF=-1,Z;
inline void Ot(){fwrite(sr,1,CCF+1,stdout),CCF=-1;}
inline void print(ll x,char chr='\n'){
    if(CCF>1<<20)Ot();if(x<0)sr[++CCF]=45,x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++CCF]=z[Z],--Z);sr[++CCF]=chr;
} int n,q,root; ll ans,dis[M][M];
int pat,head[M];
struct Edge{ int to,val,nxt; }e[M<<1];
inline void add(int u,int v,int w){
    e[++pat]={v,w,head[u]},head[u]=pat;
    e[++pat]={u,w,head[v]},head[v]=pat;
}
void dfs(int u,int fa,ll d){ dis[root][u]=d;
    go(u) if(v^fa) dfs(v,u,d+e[i].val);
}
int main(){ n=read(),q=read(); Rg int x,y,z;
    fp(i,2,n) x=read(),y=read(),z=read(),add(x,y,z);
    fp(i,1,n) dfs(root=i,0,0);
    while(q--){ x=read(),y=read(),ans=0;
        fp(i,1,n) cmax(ans,Min(dis[x][i],dis[y][i])); print(ans);
    } return Ot(),0;
}

原文地址:https://www.cnblogs.com/Judge/p/11713815.html

时间: 2024-08-30 15:43:37

CSP-S全国模拟赛第二场 【nan】的相关文章

WC2019 全国模拟赛第一场 T1 题解

由于只会T1,没法写游记,只好来写题解了... 题目链接 题目大意 给你一个数列,每次可以任取两个不相交的区间,取一次的贡献是这两个区间里所有数的最小值,求所有取法的贡献和,对 \(10^9+7\) 取模. 数列长度 \(2\times 10^5\) ,值域 \(1\) ~ \(10^9\) . \(O(n^4)\) 做法 预处理区间最小值,枚举选的两个区间. #include <iostream> #include <cstdio> #include <algorithm&

2017年校招全国统一模拟笔试(第二场)编程题集合-牛客网

 2017年校招全国统一模拟笔试(第二场)编程题集合-牛客网 链接:https://www.nowcoder.com/questionTerminal/276712b113c6456c8cf31c5073a4f9d7来源:牛客网 牛牛有两个字符串(可能包含空格),牛牛想找出其中最长的公共连续子串,希望你能帮助他,并输出其长度. 输入描述: 输入为两行字符串(可能包含空格),长度均小于等于50. 输出描述: 输出为一个整数,表示最长公共连续子串的长度. 输入例子: abcde abgde 输出例子

2019模拟赛09场解题报告

目录 2019模拟赛09场解题报告 目录la~~ 题一:瞬间移动 题二:食物订购 题三:马蹄印 题四:景观美化 2019模拟赛09场解题报告 标签(空格分隔): 解题报告 Forever_chen 2019.8.20 目录la~~ 题一:瞬间移动 [题面] 有一天,暮光闪闪突然对如何将一个整数序列a1,a2,...,an排序为一个不下降序列起了兴趣.身为一只年轻独角兽的她,只能进行一种叫做"单元转换"(unit shift)的操作.换句话说,她可以将序列的最后一个元素移动到它的起始位置

Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)

D 10248 修建高楼 D 传送门 题干 题目描述 C 市有一条东西走向的"市河".C 市的市长打算在"市河"的其中一条岸边自东往西的 n 个位置(可以将这 n 个位置看成在一条直线上,且位置不会重叠)依次建造高楼. C 市的设计部门设计了 T 个方案供市长挑选(方案编号为 1 到 T).每个方案都提供了建造的每幢高楼的高度,自东向西依次为 h1,h2,h3,-,hn-1,hn.每幢楼房的高度在 1 到 n 之间(包括 1 和 n),且各不相同. 市长在挑选设计方

20170925“切题如切菜杯”水题模拟赛 第二弹

T1:短 给出一张有n个点和m条双向边的图,要求求出1到n的次短路的长度.一条边可以多次通过. 输入格式: 第一行为两个整数n和m.接下来的m行每行三个整数ai,bi,vi,分别表示这条路连着的两个点和他的长度. 输出格式: 一个整数,表示次短路的长度. 样例输入 样例输出 4 41 2 1002 4 2002 3 2503 4 100 450 样例解释: 最短:1->2->4. 次短:1->2->3->4. 数据范围: 对于 100%的数据:1<=n.vi<=5

2017 多校赛 第二场

1003.Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Steph is extremely obsessed with “sequence problems” that are usually see

2015 多校赛 第二场 1006 (hdu 5305)

Problem Description There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (mostly using face-to-face communication). However, everyo

2015 多校赛 第二场 1002 (hdu 5301)

Description Your current task is to make a ground plan for a residential building located in HZXJHS. So you must determine a way to split the floor building with walls to make apartments in the shape of a rectangle. Each built wall must be paralled t

2015 多校赛 第二场 1004 hdu(5303)

Problem Description There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road.The ith tree is planted at position xi, clockwise from position 0. There are ai delicious appl