模拟15 题解(waiting)
T1
60%算法
定义f[i][j]表示枚举到i位置,已经使用过了j个队,
$f[i][j]+=f[i-1][t] ( t \in [max(0,j-k),j])$滚动一下
这是个O(n^3)的,考虑如何优化,发现可以使用前缀和,避免枚举t,$O(n^2)$
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<algorithm>
- #define R register
- #define ll long long
- using namespace std;
- inline int read()
- {
- int f=,x=;char ch=getchar();
- while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
- while(ch<=''&&ch>=''){x=(x<<)+(x<<)+(ch^);ch=getchar();}
- return f*x;
- }
- const int mod=;
- const int maxn=;
- int n,m,k;
- ll f[][maxn],g[][maxn];
- int main()
- {
- //freopen("data1","r",stdin);
- //freopen("1.out","w",stdout);
- n=read(),m=read(),k=read();
- m-=n;k--;
- f[][]=g[][]=;
- for(R int j=;j<=k;++j)
- {
- f[][j]=;
- g[][j]=(g[][j-]+f[][j])%mod;
- }
- for(int j=k+;j<=m;j++)
- g[][j]=g[][j-]%mod;
- R int cnt=;
- for(R int i=;i<=n;++i)
- {
- cnt^=;
- for(R int j=;j<=m;++j)
- {
- R int l=max(,j-k),r=j;
- R ll tot=;
- if(l==)tot=g[cnt^][r];
- else tot=(g[cnt^][r]-g[cnt^][l-]+mod)%mod;
- f[cnt][j]=tot%mod;
- g[cnt][j]=f[cnt][j]%mod;
- if(j) (g[cnt][j]+=g[cnt][j-])%=mod;
- }
- }
- printf("%lld\n",f[cnt][m]%mod);
- }
100%算法
问题转化成:m个物品,放到n个抽屉里,每个至少放一个,最多放k个
若任何的限制: C(m+n-1,n-1)表示一共有m个物品,分成n组就要用n-1个挡板,把挡板也看成空位,总共m+n-1个空位,选出来n-1个
若考虑至少放一个:C(m-n+n-1,n-1)先用n个物品给每个抽屉放一个,剩了m-n个物品,再加上n-1个空,剩下的同上
再考虑k的限制:C(n,i)*C(m-n-i*k+n-1,n-1)表示至少有i个的数量已经超过k(>=k+1)所以先给n个抽屉放一个之后,再给n个放上k个,使之成为k+1个
就是m-n-i*k,再加上n-1个空
数组开2e7就行,显然n>m直接return0
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<algorithm>
- #define R register
- #define ll long long
- using namespace std;
- inline ll read()
- {
- ll f=,x=;char ch=getchar();
- while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
- while(ch<=''&&ch>=''){x=(x<<)+(x<<)+(ch^);ch=getchar();}
- return f*x;
- }
- const ll mod=;
- const ll maxn=;
- ll fac[maxn],inv[maxn],facinv[maxn];
- ll n,m,k;
- void init()
- {
- fac[]=;
- inv[]=inv[]=facinv[]=facinv[]=;
- for(ll i=;i<=m+n;i++)
- {
- fac[i]=fac[i-]*i*1ll%mod;
- inv[i]=(mod-mod/i)*inv[mod%i]%mod;
- facinv[i]=facinv[i-]*inv[i]%mod;
- }
- }
- ll C(ll n,ll m)
- {
- if(n<m) return ;
- return 1ll*fac[n]*facinv[n-m]%mod*facinv[m]%mod;
- }
- int main()
- {
- n=read(),m=read(),k=read();
- if(n>m||n*k<m){puts("");return ;}
- init();
- ll ans=C(m-,n-)%mod;
- for(ll i=;i<=n;i++)
- {
- if(m-n<i*k)break;
- ll f=(i&)?-:;
- ans=(ans+1ll*f*C(m-i*k-,n-)%mod*C(n,i)%mod+mod)%mod;
- }
- printf("%lld\n",ans%mod);
- }
T2
对于无环的情况,最优解就是,图中的最长链的长度,,,为什么?
注意审题:只是炸城市,道路不炸,故某一城市毁了,其他城市的联通性不变,所以最长链上最少要炸的次数就是链长,而其他的路径,当然可以在炸最长链上的每个节点的同时也一起炸,有环的话,tarjan所点成scc,然后拓扑排序,把入度为0的节点放入队列中,枚举其子节点,子节点的答案为,父节点答案加上子节点的scc大小,并且这个点可能使用多次,所以要每次取最大值
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<algorithm>
- #include<vector>
- #include<queue>
- using namespace std;
- inline int read()
- {
- int f=,x=;char ch=getchar();
- while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
- while(ch<=''&&ch>=''){x=(x<<)+(x<<)+(ch^);ch=getchar();}
- return f*x;
- }
- const int maxn=;
- int n,m;
- struct node{
- int v,nxt;
- }e[*maxn];int h[maxn],nu;
- void add(int x,int y)
- {
- e[++nu].v=y;
- e[nu].nxt=h[x];
- h[x]=nu;
- }
- struct nodc{
- int v,nxt;
- }ec[maxn*];int hc[maxn],nuc;
- void add_c(int x,int y)
- {
- ec[++nuc].v=y;
- ec[nuc].nxt=hc[x];
- hc[x]=nuc;
- }
- int dfn[maxn],low[maxn],num,top,cnt;
- int sta[maxn],ins[maxn],bl[maxn];
- vector<int>scc[maxn];
- void tarjan(int x)
- {
- dfn[x]=low[x]=++num;
- sta[++top]=x,ins[x]=;
- for(int i=h[x];i;i=e[i].nxt)
- {
- int y=e[i].v;
- if(!dfn[y])
- {
- tarjan(y);
- low[x]=min(low[x],low[y]);
- }
- else if(ins[y])
- low[x]=min(low[x],dfn[y]);
- }
- if(dfn[x]==low[x]){
- int y;cnt++;
- do{
- y=sta[top--],ins[y]=;
- scc[cnt].push_back(y);bl[y]=cnt;
- }while(y!=x);
- }
- }
- int ind[maxn],ans[maxn];
- void topo()
- {
- queue<int>q;
- for(int i=;i<=cnt;i++)
- if(!ind[i])
- q.push(i),ans[i]=scc[i].size();
- while(q.size())
- {//cout<<" ^^^";
- int x=q.front();q.pop();
- for(int i=hc[x];i;i=ec[i].nxt)
- {
- int y=ec[i].v;ind[y]--;
- if(!ind[y])q.push(y);
- int w=scc[y].size();
- if(ans[y]<ans[x]+w)ans[y]=ans[x]+w;
- }
- }
- }
- int main()
- {
- //freopen("data","r",stdin);
- n=read(),m=read();
- for(int i=;i<=m;i++)
- {
- int x=read(),y=read();
- add(x,y);
- }
- // cout<<"^^^";
- for(int i=;i<=n;i++)
- if(!dfn[i])
- tarjan(i);
- for(int x=;x<=n;x++)
- {
- for(int i=h[x];i;i=e[i].nxt)
- {
- int y=e[i].v;
- if(bl[x]!=bl[y])
- add_c(bl[x],bl[y]),ind[bl[y]]++;
- }
- }
- topo();
- int an=;
- for(int i=;i<=n;i++)
- an=max(ans[i],an);
- printf("%d\n",an);
- }
模拟15 题解(waiting)的更多相关文章
- [NOIP模拟15]题解
A.建设城市(city) 这容斥题多难啊你们是怎么考场切掉的啊 首先可以想一下,如果没有k的限制,这题怎么做? 相信你们肯定能看出来是挡板法裸题:m个物品分给n个人,每个人至少一个. 就是$C_{m- ...
- [CQOI2012]模拟工厂 题解(搜索+贪心)
[CQOI2012]模拟工厂 题解(搜索+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327574 链接题目地址:洛谷P3161 BZOJ P26 ...
- HZOJ 20190818 NOIP模拟24题解
T1 字符串: 裸的卡特兰数题,考拉学长讲过的原题,就是bzoj3907网格那题,而且这题更简单,连高精都不用 结论$C_{n+m}^{n}-C_{n+m}^{n+1}$ 考场上10min切掉 #in ...
- 模拟21 题解(waiting)
留坑待填 效率!!! 题还没改Oh,NO!!!
- 模拟20 题解(waiting)
留坑待填 T2 #include<cstdio> #include<vector> #include<cstring> #include<iostream&g ...
- 模拟19 题解(waiting)
T1,千万别转化成链了!! 直接数就可以,dfs搜索每种情况,对于搜到的点,如果子树大小过大,直接return,相等说明可以,小的话向上累加, 优化是先预处理子树大小,若子树小,不用搜了直接加上就行 ...
- NOIP第7场模拟赛题解
NOIP模拟赛第7场题解: 题解见:http://www.cqoi.net:2012/JudgeOnline/problemset.php?page=13 题号为2221-2224. 1.car 边界 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- HGOI NOIP模拟4 题解
NOIP国庆模拟赛Day5 题解 T1 马里奥 题目描述 马里奥将要参加 NOIP 了,他现在在一片大陆上,这个大陆上有着许多浮空岛,并且其中一座浮空岛上有一个传送门,马里奥想要到达传送门从而前往 N ...
随机推荐
- 关于 ros
1.https://mikrotik.com/download 下载 x86 架构的 cd image (当日这是试用版,特殊版下载后道理一样) 2.exsi 上传,并新建 linux 的 其他 ...
- PAT甲级——A1091 Acute Stroke【30】
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- exiftool(-k)与gui的联合使用
首先下载一个exiftool下载后改名字https://sno.phy.queensu.ca/~phil/exiftool/ 根据自己的操作系统选择,我需要这个 然后下载guihttp://u88.n ...
- Python学习day19-常用模块之re模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- python流程控制-条件语句If,while循环
一.If,条件语句-选择 格式:python简洁优美,注意缩进 1.第一种: if 条件: 四个空格(tab键) 满足条件时的执行步骤 if 5>4 : print(666) print(77 ...
- Ubuntu下U盘能看见盘符但打不开
查看U盘状态 sudo fdisk -l 格式化 sudo mkfs -t vfat -I /dev/sdb1 sudo mkfs -t ntfs -I /dev/sdb1 sudo mkfs -t ...
- HtmlHelper1
<div> @using(Html.BeginForm("Test","Default")) { 4 @Html.TextBox("nam ...
- 从NoSQL到NewSQL数据库
- “玲珑杯”ACM比赛 Round #11 B题
http://www.ifrog.cc/acm/problem/1097?contest=1013&no=1 //LIS的高端写法 #include <iostream> #inc ...
- 牛客NOIP暑期七天营-提高组6
目录 A-积木大赛 题目描述 link 题解 代码 B-破碎的序列 题目描述 link 题解 C-分班问题 题目描述 link 题解 比赛链接 官方题解 A-积木大赛 题目描述 link 题解 标签: ...