Lydsy2017省队十连测
5215: [Lydsy2017省队十连测]商店购物
可能FFT学傻了,第一反应是前面300*300背包,后面FFT...
实际上前面背包,后面组合数即可.只是这是一道卡常题,需要注意常数..
1 //Achen 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdlib> 6 #include<vector> 7 #include<cstdio> 8 #include<queue> 9 #include<cmath> 10 #include<set> 11 #include<map> 12 #define For(i,a,b) for(int i=(a);i<=(b);i++) 13 #define Rep(i,a,b) for(int i=(a);i>=(b);i--) 14 const int N=307,up=10000000,p=1000000007; 15 typedef long long LL; 16 typedef double db; 17 using namespace std; 18 int n,m,k,w[N],prw; 19 LL fac[up+7],inv[up+7],f[N*N],sum[N],g[up+7],ans; 20 21 template<typename T> void read(T &x) { 22 char ch=getchar(); x=0; T f=1; 23 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); 24 if(ch=='-') f=-1,ch=getchar(); 25 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f; 26 } 27 28 LL C(int n,int m) { 29 if(n<m||n<0||m<0) return 0; 30 return fac[n]*inv[m]%p*inv[n-m]%p; 31 } 32 33 LL mo(LL x) { if(x<0) return x+p; if(x>=p) return x-p; return x; } 34 35 //#define DEBUG 36 int main() { 37 #ifdef DEBUG 38 freopen("1.in","r",stdin); 39 //freopen(".out","w",stdout); 40 #endif 41 read(n); read(m); read(k); 42 inv[0]=inv[1]=fac[0]=1; 43 For(i,2,k+n-m) inv[i]=mo(p-p/i*inv[p%i]%p); 44 For(i,1,k+n-m) fac[i]=fac[i-1]*i%p,inv[i]=inv[i-1]*inv[i]%p; 45 For(i,1,m) read(w[i]); 46 f[0]=1; 47 For(i,1,m) { 48 sum[0]=1; 49 prw+=w[i]; 50 For(j,1,prw) sum[j]=mo(sum[j-1]+f[j]); 51 Rep(j,prw,0) 52 f[j]=mo(sum[j]-(j-w[i]-1>=0?sum[j-w[i]-1]:0)); 53 } 54 g[0]=1; 55 if(!(n-m)) { 56 if(k<=prw) printf("%lld ",f[k]); 57 else puts("0"); 58 return 0; 59 } 60 For(i,0,min(prw,k)) 61 ans=mo(ans+f[i]*C(k-i+(n-m)-1,n-m-1)%p); 62 printf("%lld ",ans); 63 return 0; 64 }
5216: [Lydsy2017省队十连测]公路建设
感觉可以lct+回滚莫对乱搞
正解:发现n很小,让人浮想连篇
线段树,每个节点维护这段区间的边可选的情况下在最小生成森林上的边,n很小所以每个点上的边都不超过99条
线段树update的时候用归并排序,查的时候直接快拍一波.
求最小生产森林的时候并查集维护.
昨天写的时候对拍十分完美,看了有看也找不出毛病,一直WA.今天重构了下代码就过了...
1 //Achen 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdlib> 6 #include<vector> 7 #include<cstdio> 8 #include<queue> 9 #include<cmath> 10 #include<set> 11 #include<map> 12 const int N=200007; 13 #define For(i,a,b) for(int i=(a);i<=(b);i++) 14 #define Rep(i,a,b) for(int i=(a);i>=(b);i--) 15 typedef long long LL; 16 typedef double db; 17 using namespace std; 18 int n,m,q; 19 20 template<typename T> void read(T &x) { 21 char ch=getchar(); x=0; T f=1; 22 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); 23 if(ch=='-') f=-1,ch=getchar(); 24 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f; 25 } 26 27 struct edge { int u,v,w; }e[N]; 28 vector<int>vc[N*20]; 29 30 #define lc x<<1 31 #define rc ((x<<1)|1) 32 #define mid ((l+r)>>1) 33 34 int fa[N]; 35 int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } 36 37 int sta[N],top; 38 void update(int x) { 39 int i=0,j=0,up1=vc[lc].size(),up2=vc[rc].size(); top=0; 40 while(i<up1||j<up2) { 41 if(j>=up2||(i<up1&&j<up2&e[vc[lc][i]].w<=e[vc[rc][j]].w)) sta[++top]=vc[lc][i++]; 42 else sta[++top]=vc[rc][j++]; 43 } 44 For(i,1,top) { 45 int u=e[sta[i]].u,v=e[sta[i]].v; 46 fa[u]=u; fa[v]=v; 47 } 48 For(i,1,top) { 49 int u=e[sta[i]].u,v=e[sta[i]].v; 50 if(find(u)!=find(v)) { 51 fa[find(u)]=find(v); 52 vc[x].push_back(sta[i]); 53 } 54 } 55 } 56 57 void build(int x,int l,int r) { 58 if(l==r) { vc[x].push_back(l); return; } 59 build(lc,l,mid); build(rc,mid+1,r); 60 update(x); 61 } 62 63 void qry(int x,int l,int r,int ql,int qr) { 64 if(l>=ql&&r<=qr) { 65 int up=vc[x].size(); 66 For(i,0,up-1) sta[++top]=vc[x][i]; 67 return ; 68 } 69 if(ql<=mid) qry(lc,l,mid,ql,qr); 70 if(qr>mid) qry(rc,mid+1,r,ql,qr); 71 } 72 73 bool cmp(const int &A,const int &B) { 74 return e[A].w<e[B].w; 75 } 76 77 int solve(int l,int r) { 78 top=0; if(l>r) swap(l,r); 79 qry(1,1,m,l,r); 80 sort(sta+1,sta+top+1,cmp); 81 int rs=0,cnt=0; 82 For(i,1,n) fa[i]=i; 83 For(i,1,top) { 84 int u=e[sta[i]].u,v=e[sta[i]].v,w=e[sta[i]].w; 85 if(find(u)!=find(v)) { 86 fa[find(u)]=find(v); 87 rs+=w; cnt++; 88 if(cnt>=n-1) break; 89 } 90 } 91 return rs; 92 } 93 94 //#define DEBUG 95 int main() { 96 #ifdef DEBUG 97 freopen("1.in","r",stdin); 98 //freopen(".out","w",stdout); 99 #endif 100 read(n); read(m); read(q); 101 For(i,1,m) { 102 read(e[i].u); read(e[i].v); read(e[i].w); 103 } 104 build(1,1,m); 105 while(q--) { 106 int l,r; 107 read(l); read(r); 108 printf("%d ",solve(l,r)); 109 } 110 return 0; 111 }