版本比较
8.31-9.6代码分析
正文
增加 13 行删除 13 行
86 行未改动878889909191929394959 行未改动1051061071081091091101111121139 行未改动12312412512612712712812913013142 行未改动17417517617717817817918018118218318218318418518618787 行未改动2752762772782792792802812822832 行未改动28628728828929029029129229329429429529629729812 行未改动31131231331431531531631731831937 行未改动35735835936036136136236336436516 行未改动38238338438538638638738838939053 行未改动4444454464474484484494504514527 行未改动86 行未改动#ifndef ONLINE_JUDGEtemplate <typename tp>void _debug(const tp& t) {cerr<<t<<'\n';}template <typename tp,typename... args>void _debug(const tp& t, const args&... rest) {cerr<<t<<' ';_debug(rest...);}//多参数递归debugvoid _debug(const tp& t, const args&... rest) {cerr<<t<<' ';_debug(rest...);}#define debug(...) _debug(#__VA_ARGS__ " =", __VA_ARGS__)#else#define debug(...) 0#endif9 行未改动 for(int i=1;i<=n;i++) cout<<'1'<<" \n"[i==n]; return; } if(n==1||k==1) return cout<<"-1\n",void();//n==1时无配对,k==1时无解 B=1000000000;//将B定义为1e9大数 B=1000000000;//将B定义为1e9 int su=0;//维护当前总贡献 m=0; fill(ans+1,ans+n+1,1);//ans数组全部赋值为1 for(int i=1;i<=n&&k>=0;i++) {9 行未改动 const int cnt=min((-k)/sum(j-1,i-1),ans[j]-1);//最多可以减去cnt k+=cnt*sum(j-1,i-1);//把贡献加回k for(int t=j;t<=i;t++) ans[t]-=cnt;//整块全部减去cnt while(ans[j]==1) j++;//j向右跳过已经到1的位置 while(ans[j]==1) j++;//j向右跳已经到1的位置 } for(int i=1;i<=n;i++) cout<<ans[i]<<" \n"[i==n];}42 行未改动 f[0]=1,pos[0]=-1e18;//DP前初始化 int lst=0,res=1,now=0;//lst 指针,res 前缀和,now 计算连续营员数量 for(int i=1;i<=n;i++){ if(typ[i]){//当前i是教练 f[i]=res,res=(res+f[i])%mod;//必选i且前面所有合法方案都可接 f[i]=res,res=(res+f[i])%mod;//必选i且前面所有合法方案都可选 sum[i]=(sum[i-1]+f[i]*inv[now]%mod)%mod;//更新前缀和,乘inv减少倍数 } else{//当前i是营员 while(lst+1<=tot&&pos[coa[lst+1]]<pos[i]-dis) lst++;//指针找到超出范围的教练 sum[i]=sum[i-1];//营员sum直接变成上一个 while(lst+1<=tot&&pos[coa[lst+1]]<pos[i]-dis) lst++;//指针找到超范围的教练 sum[i]=sum[i-1];//营员sum直接变成上一个sum f[i]=((sum[i]-sum[coa[lst]])%mod+mod)%mod*pw[now]%mod;//前缀差,乘pw还原营员倍数 res=(res+f[i])%mod,now++;//累加入总方案 } }87 行未改动ll ans;bool check(int k) {//检查所有长度2^k的块 for (int i = 1; i <= (1 << (n - k)); i++) { if (a[(i - 1) * (1 << k) + 1] + (1 << (k - 1)) != a[(i - 1) * (1 << k) + (1 << (k - 1)) + 1]) {//判断块前后两半是否连续 return 0;//存在不合法 return 0;//不合法 return false; } } return 1;//全部块合法}2 行未改动 std::swap(a[i + m - 1], a[j + m - 1]);//交换两个位置的值 }}void dfs(int now, int num) {//now 当前处于第几层,num 已经使用次数 if (now && !check(now)) {//now不为0且当前check有问题 if (now && !check(now)) {//now不为0且当前check不合法 return;//无解 } if (now == n) {//到第n层了 ans += po[num];//贡献num的阶乘种排列顺序 ans += po[num];//贡献num!种排列顺序 return; } dfs(now + 1, num); int tmp[5], tot = 0;//tmp 出错块的编号,tot 出错块计数12 行未改动 for (int i = 1; i <= tot; i++) { for (int j = i + 1; j <= tot; j++) { swap((1 << now) * (tmp[i] - 1) + 1, (1 << now) * (tmp[j] - 1) + 1, 1 << now);//交换两块 dfs(now + 1, num + 1); swap((1 << now) * (tmp[i] - 1) + 1, (1 << now) * (tmp[j] - 1) + 1, 1 << now);//回溯复原数组 swap((1 << now) * (tmp[i] - 1) + 1, (1 << now) * (tmp[j] - 1) + 1, 1 << now);//回溯 } }}int main() {37 行未改动 ed[++tot].to = to; ed[tot].nxt = head[u]; head[u] = tot;}//加边int siz[maxn],dfn[maxn],pos[maxn],cnt;//siz 子树大小 dfn时间戳 pos时间戳对应节点 int siz[maxn],dfn[maxn],pos[maxn],cnt;//siz 子树大小 dfn 时间戳 pos 时间戳对应节点 void dfs(int x){ siz[x] = 1; for (int i = head[x];i;i = ed[i].nxt){ int to = ed[i].to;16 行未改动 { f[i][j] = max(f[i-1][j-1]+val[pos[i]],f[i-siz[pos[i]]][j]);//选pos[i],不选直接跳过pos[i]所在子树 } } return f[cnt][k]>0;//选k个点总权>0,则可行 return f[cnt][k]>0;//选k个点总权>0,可行}int main(){ k = read()+1,n = read(); for (int i = 1;i <= n;i++)53 行未改动 else{//断裂化学键 if(!fl[a][b])continue;//这条边不存在 ans++;//假设删掉边不连通,连通块数+1 fl[a][b]=fl[b][a]=0; for(int z=1;z<=n;z++){//是否还存在公共点 for(int z=1;z<=n;z++){//是否存在公共点 if(fl[a][z]&&fl[b][z]){//存在公共点,a b依然连通 ans--;//连通则减 break; }7 行未改动