2017-10-5 清北刷題沖刺班a.m
阿新 • • 發佈:2017-10-05
mage can style ++ event ima txt ostream void
行列式
#include<iostream> #include<cstdio> #define maxn 500010 using namespace std; int n,m,mod,l,r,x,y,b[maxn],a[maxn],cnt; void dfs(int now[],int sz){ if(sz<=2){ for(int i=1;i<=sz;i++)b[++cnt]=now[i]; return; } int sz1=0,sz2=0; int d[sz],c[sz];30分 暴力for(int i=1;i<=sz;i++){ if(i%2!=0){//奇數位 c[++sz1]=now[i]; } if(i%2==0){//偶數位 d[++sz2]=now[i]; } } dfs(c,sz1); dfs(d,sz2); } int main(){ //freopen("Cola.txt","r",stdin); freopen("seq.in","r",stdin);freopen("seq.out","w",stdout); scanf("%d%d%d",&n,&m,&mod); for(int i=1;i<=n;i++)a[i]=i; dfs(a,n); long long ans; for(int i=1;i<=m;i++){ scanf("%d%d%d%d",&l,&r,&x,&y); ans=0; if(x>y)swap(x,y); for(int j=l;j<=r;j++){if(b[j]<=y&&b[j]>=x){ ans=(ans+b[j])%mod; } }printf("%I64d\n",ans); } fclose(stdin);fclose(stdout); return 0; }
序列
數數
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #define maxn 100010 using namespace std; int num,head[maxn]; int sz[maxn],son[maxn],fa[maxn],top[maxn],dep[maxn]; long long ans; struct node{ int to,pre; }e[maxn*2]; int n,dis[2010][2010]; bool vis[maxn]; void Insert(int from,int to){ e[++num].to=to; e[num].pre=head[from]; head[from]=num; } void Bfs(int s){ queue<int>q; memset(vis,0,sizeof(vis)); vis[s]=1;q.push(s);dis[s][s]=0; while(!q.empty()){ int now=q.front();q.pop(); for(int i=head[now];i;i=e[i].pre){ int to=e[i].to; if(vis[to])continue; else { dis[s][to]=dis[s][now]+1; vis[to]=1; q.push(to); } } } } int count(int x){ int res=0; while(x){ res+=x&1; x>>=1; } return res; } void dfs1(int now,int father){ dep[now]=dep[father]+1; sz[now]=1;fa[now]=father; for(int i=head[now];i;i=e[i].pre){ int to=e[i].to; if(to==father)continue; dfs1(to,now); sz[now]+=sz[to]; if(!son[now]||sz[son[now]]<sz[to])son[now]=to; } } void dfs2(int now,int father){ top[now]=father; if(son[now])dfs2(son[now],father); for(int i=head[now];i;i=e[i].pre){ int to=e[i].to; if(to==fa[now]||to==son[now])continue; dfs2(to,to); } } int LCA(int a,int b){ while(top[a]!=top[b]){ if(dep[top[a]]<dep[top[b]])swap(a,b); a=fa[top[a]]; } if(dep[a]>dep[b])swap(a,b); return a; } int main(){ //freopen("Cola.txt","r",stdin); freopen("bitcount.in","r",stdin);freopen("bitcount.out","w",stdout); scanf("%d",&n); int x,y; for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); Insert(x,y);Insert(y,x); } for(int i=1;i<=n;i++)Bfs(i); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=count(dis[i][j]); dfs1(1,0); dfs2(1,1); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ int lca=LCA(i,j); ans+=dis[i][lca]+dis[j][lca]; } } cout<<ans; fclose(stdin);fclose(stdout); return 0; }60分 暴力
2017-10-5 清北刷題沖刺班a.m