NOIP2017解题报告
来源:互联网 发布:淘宝客鹊桥助手 编辑:程序博客网 时间:2024/05/22 15:47
T1小凯的疑惑
根据裴蜀定理,一定存在ax+by==k的解(gcd(a,b)==1)
要求的是
那么一定有
考虑两边同时减
得到
那么可以将x减小,也可以将y减小
令x”,y”为x为最小非负整数解时,
令x’,y’为y为最小非负整数解时,
要求最大的解k,一定有
且x < x” ,y < y’
那么答案就是
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long ll;inline void exgcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b){ d=a; x=1; y=0; return ; } else { exgcd(b,a%b,d,y,x); y-=(a/b)*x; }}int main(){ freopen("2017math.in","r",stdin); freopen("2017math.out","w",stdout); ll a,b; scanf("%lld %lld",&a,&b); ll d,x,y; exgcd(a,b,d,x,y); ll xx=a/d,yy=b/d; x=(x%yy+yy)%yy; y=(y%xx+xx)%xx; printf("%lld\n",(long long)a*(x-1)+(long long)b*(y-1)-1);return 0;}
T2时间复杂度
手写一个栈模拟即可
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char A[1010];char B[1010];int vis[30];int s[1010];int pp[1010];int bl[1010];inline void init(){ memset(vis,0,sizeof(vis)); memset(s,0,sizeof(s)); memset(pp,0,sizeof(pp)); memset(bl,0,sizeof(bl));}int main(){ freopen("2017complexity.in","r",stdin); freopen("2017complexity.out","w",stdout); int T; scanf("%d",&T); int L; while(T--){ init(); scanf("%d",&L); //printf("%d\n",L); int timt=0; scanf("%s",A); int len=strlen(A); int pcnt=0; for(int i=0;i<len;){ if(i!=0&&A[i-1]!='^'&&A[i]>='0'&&A[i]<='9'){ int x=0; while(i<len&&A[i]>='0'&&A[i]<='9'){ x=x*10+A[i]-'0'; i++; } timt=x; } else if(i!=0&&A[i-1]=='^'&&A[i]>='0'&&A[i]<='9'){ int x=0; while(i<len&&A[i]>='0'&&A[i]<='9'){ x=x*10+A[i]-'0'; i++; } pcnt=x; } else i++; } scanf("\n"); int snum=0; int eok=0; int pclt=0; int blk=0; int pcltma=0; for(int i=1;i<=L;i++){ gets(B); int le=strlen(B); if(B[0]=='E'){ if(!snum){ eok=1; continue; } else { vis[s[snum]]=0; if(pp[snum]){ pclt--; pp[snum]=0; } if(bl[snum]){ blk--; bl[snum]=0; } snum--; } } else if(B[0]=='F'){ int hav_x=0,hav_a=0,hav_b=0; int a=0,b=0; int aen=0,ben=0; for(int j=1;j<le;j++){ if(B[j]>='a'&&B[j]<='z'){ if(!hav_x){ hav_x=1; if(vis[B[j]-'a']) eok=1; vis[B[j]-'a']=1; s[++snum]=B[j]-'a'; } else if(!hav_a){ hav_a=1; aen=1; } else if(!hav_b){ hav_b=1; ben=1; } } else if(B[j]>='0'&&B[j]<='9'){ if(!hav_a){ hav_a=1; while(j<le&&B[j]>='0'&&B[j]<='9'){ a=a*10+B[j]-'0'; j++; } j--; } else if(!hav_b){ hav_b=1; while(j<le&&B[j]>='0'&&B[j]<='9'){ b=b*10+B[j]-'0'; j++; } j--; } } } if(!aen&&ben&&!blk){ pclt++; pp[snum]=1; } if(!aen&&!ben&&a>b) blk++,bl[snum]=1; if(aen&&!ben) blk++,bl[snum]=1; } pcltma=max(pcltma,pclt); } if(snum) eok=1; if(eok) puts("ERR"); else if(pcnt){ if(pcltma==pcnt) puts("Yes"); else puts("No"); } else { if(!pclt) puts("Yes"); else puts("No"); } }return 0;}
T3逛公园
令f[i][l]为到第i个点,比从第1个点到第i个点最短路长l的方案数
容易写出dp方程
但这个dp是有后效性的
将f[i][l]拆成k+1个点,再建边跑topo-sort或记忆化搜索
用tarjan判断是否有0环
时间复杂度O(n*k)
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;const int maxn=100000+10;vector<int>A[maxn];vector<int>C[maxn];vector<int>B[maxn];vector<int>D[maxn];vector<int>Z[maxn];int p,n;int dfn[maxn],low[maxn],ins[maxn],s[maxn];int f[maxn][60];int inz[maxn];int top;int dis[maxn],vis[maxn],rdis[maxn];int add[maxn][60];queue<int>q;int ind;int k;inline void tarjan(int x){ dfn[x]=low[x]=++ind; s[++top]=x; ins[x]=1; for(int i=0;i<Z[x].size();i++){ int u=Z[x][i]; if(!dfn[u]){ tarjan(u); low[x]=min(low[x],low[u]); } else if(ins[u]) low[x]=min(low[x],low[u]); } if(dfn[x]==low[x]){ int t=top,cnt=0; while(s[t]!=x) cnt++,t--; cnt++; while(s[top]!=x){ if(cnt>1) inz[s[top]]=1; ins[s[top]]=0; top--; } if(cnt>1) inz[s[top]]=1; ins[s[top]]=0; top--; }}inline void cle(){ top=ind=0; memset(f,0,sizeof(f)); memset(inz,0,sizeof(inz)); memset(add,0,sizeof(add)); memset(dfn,0,sizeof(dfn)); for(int i=1;i<=n;i++){ A[i].clear(); B[i].clear(); C[i].clear(); D[i].clear(); Z[i].clear(); }}inline int fuc(int x,int d){ if(add[x][d]) return f[x][d]; add[x][d]=1; if(x==1&&d==0) return f[x][d]=1; for(int i=0;i<B[x].size();i++){ int u=B[x][i]; int t=dis[x]+d-D[x][i]-dis[u]; if(t>k||t<0) continue; f[x][d]+=fuc(u,t); if(f[x][d]>=p) f[x][d]-=p; } return f[x][d];}int main(){ freopen("2017park.in","r",stdin); freopen("2017park.out","w",stdout); int T; scanf("%d",&T); while(T--){ int m; scanf("%d %d %d %d",&n,&m,&k,&p); cle(); int x,y,z; for(int i=1;i<=m;i++){ scanf("%d %d %d",&x,&y,&z); //printf("%d %d %d\n",x,y,z); A[x].push_back(y); C[x].push_back(z); B[y].push_back(x); D[y].push_back(z); if(!z) Z[x].push_back(y); } for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); memset(dis,127/2,sizeof(dis)); dis[1]=0; vis[1]=1; q.push(1); while(!q.empty()){ x=q.front(); q.pop(); for(int i=0;i<A[x].size();i++){ int u=A[x][i]; if(dis[u]>dis[x]+C[x][i]){ dis[u]=dis[x]+C[x][i]; if(!vis[u]){ vis[u]=1; q.push(u); } } } vis[x]=0; } memset(rdis,127/2,sizeof(rdis)); rdis[n]=0; vis[n]=1; q.push(n); while(!q.empty()){ x=q.front(); q.pop(); for(int i=0;i<B[x].size();i++){ int u=B[x][i]; if(rdis[u]>rdis[x]+D[x][i]){ rdis[u]=rdis[x]+D[x][i]; if(!vis[u]){ vis[u]=1; q.push(u); } } } vis[x]=0; } int ok=0; if(n==1){ puts("0"); continue; } for(int i=1;i<=n;i++) if(dis[i]+rdis[i]<=dis[n]+k&&inz[i]){ ok=1; break; } if(ok){ puts("-1"); continue; } int ans=0; for(int d=0;d<=k;d++){ ans+=fuc(n,d); if(ans>=p) ans-=p; } printf("%d\n",ans); }return 0;}
T4奶酪
直接暴力建边跑bfs,并查集什么的
时间复杂度O(n)
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>using namespace std;const int maxn=1000+10;double x[maxn],y[maxn],z[maxn];int vis[maxn];int to[maxn][maxn];int top[maxn];queue<int>q;inline double dist(int i,int j){ return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]));}int main(){ freopen("2017cheese.in","r",stdin); freopen("2017cheese.out","w",stdout); int T; scanf("%d",&T); while(T--){ memset(to,0,sizeof(to)); memset(vis,0,sizeof(vis)); int n; double h,r; scanf("%d %lf %lf",&n,&h,&r); for(int i=1;i<=n;i++) scanf("%lf %lf %lf",&x[i],&y[i],&z[i]); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ if(dist(i,j)<=2*r) to[i][j]=to[j][i]=1; } for(int i=1;i<=n;i++) if(z[i]-r<=0){ q.push(i); vis[i]=1; } while(!q.empty()){ int x=q.front(); q.pop(); for(int i=1;i<=n;i++) if(to[x][i]&&!vis[i]){ vis[i]=1; q.push(i); } } int ok=0; for(int i=1;i<=n;i++) if(z[i]+r>=h&&vis[i]) ok=1; if(ok) puts("Yes"); else puts("No"); }return 0;}
T5宝藏
状压dp
令f[i]为状态为i时,最小代价
令dep[i][j]为状态为i时,第j个点的深度
i&(1<<(u-1))
枚举子集转移O(3^n)
枚举根时间复杂度O(n),枚举用哪个点转移O(n)
时间复杂度O(3^n*n^2)
#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn=(1<<12)+10;int dep[maxn][13];int f[maxn];vector<int>A[13];vector<int>C[13];int n;int ans=0x7fffffff;inline void work(int x){ memset(f,127/2,sizeof(f)); f[1<<(x-1)]=0; memset(dep[1<<(x-1)],0,sizeof(dep[1<<(x-1)])); dep[1<<(x-1)][x]=1; int top=(1<<n); for(int i=0;i<top;i++){ if(f[i]==f[top]) continue; for(int j=1;j<=n;j++){ if(i&(1<<(j-1))) continue; for(int k=0;k<A[j].size();k++){ int u=A[j][k]; if(i&(1<<(u-1))){ if(f[i|(1<<(j-1))]>f[i]+dep[i][u]*C[j][k]){ f[i|(1<<(j-1))]=f[i]+dep[i][u]*C[j][k]; for(int l=1;l<=n;l++) dep[i|(1<<(j-1))][l]=dep[i][l]; dep[i|(1<<(j-1))][j]=dep[i][u]+1; } } } } } ans=min(ans,f[top-1]);}int main(){ freopen("2017treasure.in","r",stdin); freopen("2017treasure.out","w",stdout); int m; scanf("%d %d",&n,&m); int x,y,z; for(int i=1;i<=m;i++){ scanf("%d %d %d",&x,&y,&z); A[x].push_back(y); C[x].push_back(z); A[y].push_back(x); C[y].push_back(z); } for(int i=1;i<=n;i++) work(i); printf("%d\n",ans);return 0;}
T6列队
考虑每行其实是一个等差数列
最后一列也是个等差数列
每次操作相当于把一个等差数列分裂成最多两个公差相等的等差数列
然后再把取出的元素放到最后一列的末端
对于”向前看齐”操作也是一样的,不过是把取出的元素放到了该行的等差数列的末端
那么最多有O(3*q+n+1)个等差数列
空间复杂度O(n)
可以用平衡树维护插入删除
时间复杂度O(qlogn)
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=300000+10;typedef long long ll;int tr[maxn*8][2];int size[maxn*8];ll fr[maxn*8];int len[maxn*8];int fa[maxn*8];int tot=0;int root[maxn];inline void pushup(int x){ int l=tr[x][0],r=tr[x][1]; size[x]=size[l]+size[r]+len[x];}inline void rotate(int x,int &k){ int y=fa[x],z=fa[y]; int l=tr[y][1]==x; int r=l^1; if(y==k) k=x; else tr[z][tr[z][1]==y]=x; fa[y]=x; fa[x]=z; fa[tr[x][r]]=y; tr[y][l]=tr[x][r]; tr[x][r]=y; pushup(y); pushup(x);}inline void splay(int x,int &k){ int y,z; while(x!=k){ y=fa[x],z=fa[y]; if(y!=k){ if((tr[z][1]==y)^(tr[y][1]==x)) rotate(x,k); else rotate(y,k); } rotate(x,k); }}inline int find(int x,int k,int &num){ int l=tr[x][0],r=tr[x][1]; if(size[l]+1<=k&&size[l]+len[x]>=k){ num=k-size[l]; return x; } else if(size[l]>=k) return find(l,k,num); else return find(r,k-size[l]-len[x],num);}int main(){ freopen("2017phalanx.in","r",stdin); freopen("2017phalanx.out","w",stdout); int n,m,q; scanf("%d %d %d",&n,&m,&q); for(int i=1;i<=n;i++){ root[i]=++tot; size[tot]=m+1; fr[tot]=1ll*(i-1)*m+1; len[tot]=m-1; int u=tot; tr[u][0]=++tot; fa[tot]=root[i]; len[tot]=size[tot]=1; tr[u][1]=++tot; fa[tot]=root[i]; len[tot]=size[tot]=1; } root[n+1]=++tot; size[tot]=n+2; fr[tot]=m; len[tot]=n; int u=tot; tr[u][0]=++tot; fa[tot]=root[n+1]; len[tot]=size[tot]=1; tr[u][1]=++tot; fa[tot]=root[n+1]; len[tot]=size[tot]=1; int x,y; while(q--){ scanf("%d %d",&x,&y); long long id; if(y!=m){ int u; int t=find(root[x],y+1,u); printf("%lld\n",id=(long long)u-1+fr[t]); int nu; int p=find(root[x],y-u+1,nu); splay(p,root[x]); p=find(root[x],len[t]-u+y+2,nu); splay(p,tr[root[x]][1]); tr[p][0]=0; pushup(p); pushup(root[x]); int hvl=0; if(u!=1){ hvl=1; tr[p][0]=++tot; fa[tot]=p; fr[tot]=fr[t]; len[tot]=size[tot]=u-1; pushup(p); } if(u!=len[t]){ if(hvl){ tr[tr[p][0]][1]=++tot; fa[tot]=tr[p][0]; fr[tot]=u+fr[t]; len[tot]=size[tot]=len[t]-u; pushup(tr[p][0]); pushup(p); } else { tr[p][0]=++tot; fa[tot]=p; fr[tot]=u+fr[t]; len[tot]=size[tot]=len[t]-u; pushup(p); } } pushup(root[x]); t=find(root[n+1],x+1,u); p=find(root[x],m-1,nu); splay(p,root[x]); tr[tr[root[x]][1]][0]=++tot; fa[tot]=tr[root[x]][1]; fr[tot]=fr[t]+(long long)(u-1)*m; len[tot]=size[tot]=1; p=find(root[n+1],x-u+1,nu); splay(p,root[n+1]); p=find(root[n+1],len[t]-u+x+2,nu); splay(p,tr[root[n+1]][1]); tr[p][0]=0; pushup(p); pushup(root[n+1]); hvl=0; if(u!=1){ hvl=1; tr[p][0]=++tot; fa[tot]=p; fr[tot]=fr[t]; len[tot]=size[tot]=u-1; pushup(p); } if(u!=len[t]){ if(hvl){ tr[tr[p][0]][1]=++tot; fa[tot]=tr[p][0]; fr[tot]=(long long)u*m+fr[t]; len[tot]=size[tot]=len[t]-u; pushup(tr[p][0]); pushup(p); } else { tr[p][0]=++tot; fa[tot]=p; fr[tot]=(long long)u*m+fr[t]; len[tot]=size[tot]=len[t]-u; pushup(p); } } pushup(root[n+1]); } else { int t,p,u,nu; t=find(root[n+1],x+1,u); printf("%lld\n",id=(long long)(u-1)*m+fr[t]); p=find(root[n+1],x-u+1,nu); splay(p,root[n+1]); p=find(root[n+1],len[t]-u+x+2,nu); splay(p,tr[root[n+1]][1]); tr[p][0]=0; pushup(p); int hvl=0; if(u!=1){ hvl=1; tr[p][0]=++tot; fa[tot]=p; fr[tot]=fr[t]; len[tot]=size[tot]=u-1; pushup(p); } if(u!=len[t]){ if(hvl){ tr[tr[p][0]][1]=++tot; fa[tot]=tr[p][0]; fr[tot]=(long long)u*m+fr[t]; len[tot]=size[tot]=len[t]-u; pushup(tr[p][0]); pushup(p); } else { tr[p][0]=++tot; fa[tot]=p; fr[tot]=(long long)u*m+fr[t]; len[tot]=size[tot]=len[t]-u; pushup(p); } } pushup(root[n+1]); } int tu; int l=find(root[n+1],n,tu); splay(l,root[n+1]); tr[tr[l][1]][0]=++tot; fa[tot]=tr[l][1]; fr[tot]=id,len[tot]=size[tot]=1; pushup(tr[l][1]); pushup(l); splay(tot,root[n+1]); }return 0;}
- noip2017解题报告题解
- NOIP2017解题报告
- NOIP2017解题报告
- NOIP2017解题报告
- NOIP2017提高组解题报告
- [NOIP2017]提高组解题报告
- NOIP2017提高组解题报告
- NOIP2017普及组解题报告
- NOIP2017普及组复赛解题报告
- 正睿OI noip2017冲刺 第一次考试T2 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- NOIP2017 宝藏 题解报告【状压dp】
- leetcode 451. Sort Characters By Frequency
- 14个你可能不知道的JavaScript调试技巧
- JavaMail发送邮件
- SharePoint 2010管理中心服务器提示“需要升级”
- Java中Vector和ArrayList的区别
- NOIP2017解题报告
- CentOS7中搭建SVN服务器
- Java集合框架
- 利用linux的df和du命令查看文件和目录的内存占用
- Hbase_应用
- 1、饼图展示
- 工作日志:添加第三方源码到项目中
- 伪类在ios移动端浏览器内无效的解决方法
- 污水雨管理Bentley.SewerGEMS.CONNECT.Edition.Update1.v10.01.00.70.build.30102017