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)$

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #define R register
  7. #define ll long long
  8. using namespace std;
  9. inline int read()
  10. {
  11. int f=,x=;char ch=getchar();
  12. while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
  13. while(ch<=''&&ch>=''){x=(x<<)+(x<<)+(ch^);ch=getchar();}
  14. return f*x;
  15. }
  16. const int mod=;
  17. const int maxn=;
  18. int n,m,k;
  19. ll f[][maxn],g[][maxn];
  20. int main()
  21. {
  22. //freopen("data1","r",stdin);
  23. //freopen("1.out","w",stdout);
  24. n=read(),m=read(),k=read();
  25. m-=n;k--;
  26. f[][]=g[][]=;
  27. for(R int j=;j<=k;++j)
  28. {
  29. f[][j]=;
  30. g[][j]=(g[][j-]+f[][j])%mod;
  31. }
  32. for(int j=k+;j<=m;j++)
  33. g[][j]=g[][j-]%mod;
  34. R int cnt=;
  35. for(R int i=;i<=n;++i)
  36. {
  37. cnt^=;
  38. for(R int j=;j<=m;++j)
  39. {
  40. R int l=max(,j-k),r=j;
  41. R ll tot=;
  42. if(l==)tot=g[cnt^][r];
  43. else tot=(g[cnt^][r]-g[cnt^][l-]+mod)%mod;
  44. f[cnt][j]=tot%mod;
  45. g[cnt][j]=f[cnt][j]%mod;
  46. if(j) (g[cnt][j]+=g[cnt][j-])%=mod;
  47. }
  48. }
  49. printf("%lld\n",f[cnt][m]%mod);
  50. }

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #define R register
  7. #define ll long long
  8. using namespace std;
  9. inline ll read()
  10. {
  11. ll f=,x=;char ch=getchar();
  12. while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
  13. while(ch<=''&&ch>=''){x=(x<<)+(x<<)+(ch^);ch=getchar();}
  14. return f*x;
  15. }
  16. const ll mod=;
  17. const ll maxn=;
  18. ll fac[maxn],inv[maxn],facinv[maxn];
  19. ll n,m,k;
  20. void init()
  21. {
  22. fac[]=;
  23. inv[]=inv[]=facinv[]=facinv[]=;
  24. for(ll i=;i<=m+n;i++)
  25. {
  26. fac[i]=fac[i-]*i*1ll%mod;
  27. inv[i]=(mod-mod/i)*inv[mod%i]%mod;
  28. facinv[i]=facinv[i-]*inv[i]%mod;
  29. }
  30. }
  31. ll C(ll n,ll m)
  32. {
  33. if(n<m) return ;
  34. return 1ll*fac[n]*facinv[n-m]%mod*facinv[m]%mod;
  35. }
  36. int main()
  37. {
  38. n=read(),m=read(),k=read();
  39. if(n>m||n*k<m){puts("");return ;}
  40. init();
  41. ll ans=C(m-,n-)%mod;
  42. for(ll i=;i<=n;i++)
  43. {
  44. if(m-n<i*k)break;
  45. ll f=(i&)?-:;
  46. ans=(ans+1ll*f*C(m-i*k-,n-)%mod*C(n,i)%mod+mod)%mod;
  47. }
  48. printf("%lld\n",ans%mod);
  49. }

T2

对于无环的情况,最优解就是,图中的最长链的长度,,,为什么?

注意审题:只是炸城市,道路不炸,故某一城市毁了,其他城市的联通性不变,所以最长链上最少要炸的次数就是链长,而其他的路径,当然可以在炸最长链上的每个节点的同时也一起炸,有环的话,tarjan所点成scc,然后拓扑排序,把入度为0的节点放入队列中,枚举其子节点,子节点的答案为,父节点答案加上子节点的scc大小,并且这个点可能使用多次,所以要每次取最大值

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<queue>
  8. using namespace std;
  9. inline int read()
  10. {
  11. int f=,x=;char ch=getchar();
  12. while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
  13. while(ch<=''&&ch>=''){x=(x<<)+(x<<)+(ch^);ch=getchar();}
  14. return f*x;
  15. }
  16. const int maxn=;
  17. int n,m;
  18. struct node{
  19. int v,nxt;
  20. }e[*maxn];int h[maxn],nu;
  21. void add(int x,int y)
  22. {
  23. e[++nu].v=y;
  24. e[nu].nxt=h[x];
  25. h[x]=nu;
  26. }
  27. struct nodc{
  28. int v,nxt;
  29. }ec[maxn*];int hc[maxn],nuc;
  30. void add_c(int x,int y)
  31. {
  32. ec[++nuc].v=y;
  33. ec[nuc].nxt=hc[x];
  34. hc[x]=nuc;
  35. }
  36. int dfn[maxn],low[maxn],num,top,cnt;
  37. int sta[maxn],ins[maxn],bl[maxn];
  38. vector<int>scc[maxn];
  39. void tarjan(int x)
  40. {
  41. dfn[x]=low[x]=++num;
  42. sta[++top]=x,ins[x]=;
  43. for(int i=h[x];i;i=e[i].nxt)
  44. {
  45. int y=e[i].v;
  46. if(!dfn[y])
  47. {
  48. tarjan(y);
  49. low[x]=min(low[x],low[y]);
  50.  
  51. }
  52. else if(ins[y])
  53. low[x]=min(low[x],dfn[y]);
  54. }
  55. if(dfn[x]==low[x]){
  56. int y;cnt++;
  57. do{
  58. y=sta[top--],ins[y]=;
  59. scc[cnt].push_back(y);bl[y]=cnt;
  60. }while(y!=x);
  61. }
  62. }
  63. int ind[maxn],ans[maxn];
  64. void topo()
  65. {
  66. queue<int>q;
  67. for(int i=;i<=cnt;i++)
  68. if(!ind[i])
  69. q.push(i),ans[i]=scc[i].size();
  70. while(q.size())
  71. {//cout<<" ^^^";
  72. int x=q.front();q.pop();
  73. for(int i=hc[x];i;i=ec[i].nxt)
  74. {
  75. int y=ec[i].v;ind[y]--;
  76. if(!ind[y])q.push(y);
  77. int w=scc[y].size();
  78. if(ans[y]<ans[x]+w)ans[y]=ans[x]+w;
  79. }
  80. }
  81. }
  82. int main()
  83. {
  84. //freopen("data","r",stdin);
  85. n=read(),m=read();
  86. for(int i=;i<=m;i++)
  87. {
  88. int x=read(),y=read();
  89. add(x,y);
  90. }
  91. // cout<<"^^^";
  92. for(int i=;i<=n;i++)
  93. if(!dfn[i])
  94. tarjan(i);
  95. for(int x=;x<=n;x++)
  96. {
  97. for(int i=h[x];i;i=e[i].nxt)
  98. {
  99. int y=e[i].v;
  100. if(bl[x]!=bl[y])
  101. add_c(bl[x],bl[y]),ind[bl[y]]++;
  102. }
  103. }
  104. topo();
  105. int an=;
  106. for(int i=;i<=n;i++)
  107. an=max(ans[i],an);
  108. printf("%d\n",an);
  109. }

模拟15 题解(waiting)的更多相关文章

  1. [NOIP模拟15]题解

    A.建设城市(city) 这容斥题多难啊你们是怎么考场切掉的啊 首先可以想一下,如果没有k的限制,这题怎么做? 相信你们肯定能看出来是挡板法裸题:m个物品分给n个人,每个人至少一个. 就是$C_{m- ...

  2. [CQOI2012]模拟工厂 题解(搜索+贪心)

    [CQOI2012]模拟工厂 题解(搜索+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327574 链接题目地址:洛谷P3161 BZOJ P26 ...

  3. HZOJ 20190818 NOIP模拟24题解

    T1 字符串: 裸的卡特兰数题,考拉学长讲过的原题,就是bzoj3907网格那题,而且这题更简单,连高精都不用 结论$C_{n+m}^{n}-C_{n+m}^{n+1}$ 考场上10min切掉 #in ...

  4. 模拟21 题解(waiting)

    留坑待填 效率!!! 题还没改Oh,NO!!!

  5. 模拟20 题解(waiting)

    留坑待填 T2 #include<cstdio> #include<vector> #include<cstring> #include<iostream&g ...

  6. 模拟19 题解(waiting)

    T1,千万别转化成链了!! 直接数就可以,dfs搜索每种情况,对于搜到的点,如果子树大小过大,直接return,相等说明可以,小的话向上累加, 优化是先预处理子树大小,若子树小,不用搜了直接加上就行 ...

  7. NOIP第7场模拟赛题解

    NOIP模拟赛第7场题解: 题解见:http://www.cqoi.net:2012/JudgeOnline/problemset.php?page=13 题号为2221-2224. 1.car 边界 ...

  8. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  9. HGOI NOIP模拟4 题解

    NOIP国庆模拟赛Day5 题解 T1 马里奥 题目描述 马里奥将要参加 NOIP 了,他现在在一片大陆上,这个大陆上有着许多浮空岛,并且其中一座浮空岛上有一个传送门,马里奥想要到达传送门从而前往 N ...

随机推荐

  1. 关于 ros

    1.https://mikrotik.com/download   下载 x86 架构的 cd image  (当日这是试用版,特殊版下载后道理一样) 2.exsi 上传,并新建 linux 的 其他 ...

  2. PAT甲级——A1091 Acute Stroke【30】

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  3. exiftool(-k)与gui的联合使用

    首先下载一个exiftool下载后改名字https://sno.phy.queensu.ca/~phil/exiftool/ 根据自己的操作系统选择,我需要这个 然后下载guihttp://u88.n ...

  4. Python学习day19-常用模块之re模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. python流程控制-条件语句If,while循环

    一.If,条件语句-选择 格式:python简洁优美,注意缩进 1.第一种: if 条件: 四个空格(tab键)  满足条件时的执行步骤 if 5>4 : print(666) print(77 ...

  6. Ubuntu下U盘能看见盘符但打不开

    查看U盘状态 sudo fdisk -l 格式化 sudo mkfs -t vfat -I /dev/sdb1 sudo mkfs -t ntfs -I /dev/sdb1 sudo mkfs -t ...

  7. HtmlHelper1

    <div> @using(Html.BeginForm("Test","Default")) { 4 @Html.TextBox("nam ...

  8. 从NoSQL到NewSQL数据库

  9. “玲珑杯”ACM比赛 Round #11 B题

    http://www.ifrog.cc/acm/problem/1097?contest=1013&no=1 //LIS的高端写法 #include <iostream> #inc ...

  10. 牛客NOIP暑期七天营-提高组6

    目录 A-积木大赛 题目描述 link 题解 代码 B-破碎的序列 题目描述 link 题解 C-分班问题 题目描述 link 题解 比赛链接 官方题解 A-积木大赛 题目描述 link 题解 标签: ...