2014 Multi-University Training Contest 9
官方解题报告:http://blog.sina.com.cn/s/blog_6bddecdc0102uzwm.html
Boring Sum http://acm.hdu.edu.cn/showproblem.php?pid=4961
这个二分也过了,on更快些。
1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef __int64 LL; 7 const int M=100010; 8 int a[M]; 9 LL b[M],c[M]; 10 int val[M]; 11 vector<int> yueshu[M]; 12 int main(){ 13 for(int i=1;i<=100000;i++){ 14 yueshu[i].clear(); 15 for(int j=1;j*j<=i;j++){ 16 if(!(i%j)){ 17 yueshu[i].push_back(j); 18 if(i/j!=j) 19 yueshu[i].push_back(i/j); 20 } 21 } 22 } 23 int n; 24 while(~scanf("%d",&n),n){ 25 for(int i=0;i<n;i++){ 26 scanf("%d",&a[i]); 27 } 28 mt(val,0); 29 for(int i=n-1;i>=0;i--){ 30 int now=a[i]; 31 if(!val[now]){ 32 b[i]=now; 33 } 34 else{ 35 b[i]=val[now]; 36 } 37 int len=yueshu[now].size(); 38 for(int j=0;j<len;j++){ 39 int yue=yueshu[now][j]; 40 val[yue]=now; 41 } 42 } 43 mt(val,0); 44 for(int i=0;i<n;i++){ 45 int now=a[i]; 46 if(!val[now]){ 47 c[i]=now; 48 } 49 else{ 50 c[i]=val[now]; 51 } 52 int len=yueshu[now].size(); 53 for(int j=0;j<len;j++){ 54 int yue=yueshu[now][j]; 55 val[yue]=now; 56 } 57 } 58 LL ans=0; 59 for(int i=0;i<n;i++){ 60 ans+=b[i]*c[i]; 61 } 62 printf("%I64d\n",ans); 63 } 64 return 0; 65 }
Fast Matrix Calculation http://acm.hdu.edu.cn/showproblem.php?pid=4965
ababab矩阵等于a*(ba)^2*b,ba是kk的矩阵复杂度就k^3 log(n^2)
1 #include<cstdio> 2 #include<cstring> 3 typedef __int64 LL; 4 const int mod=6; 5 const int M=8; 6 class Matrix{ 7 typedef int typev; 8 public: 9 typev val[M][M]; 10 void zero(){ 11 memset(val,0,sizeof(val)); 12 } 13 void unit(){ 14 zero(); 15 for(int i=0;i<M;i++) val[i][i]=1; 16 } 17 }D; 18 int LEN; 19 Matrix operator * (const Matrix &a,const Matrix &b){ 20 Matrix tmp; 21 tmp.zero(); 22 for(int k=0;k<LEN;k++) 23 for(int i=0;i<LEN;i++){ 24 if(a.val[i][k]) 25 for(int j=0;j<LEN;j++){ 26 tmp.val[i][j]+=a.val[i][k]*b.val[k][j]; 27 tmp.val[i][j]%=mod; 28 } 29 } 30 return tmp; 31 } 32 Matrix operator ^ (Matrix &a,int p){ 33 Matrix tmp; 34 tmp.unit(); 35 while(p){ 36 if(p&1) tmp=tmp*a; 37 a=a*a; 38 p>>=1; 39 } 40 return tmp; 41 } 42 struct mat { 43 int n,m; 44 int data[1024][1024]; 45 }A,B,C,E; 46 class MatrixOp { 47 public: 48 int mul(mat& c,const mat& a,const mat& b) { //c=a*b 49 int i,j,k; 50 if (a.m!=b.n) return 0; 51 c.n=a.n,c.m=b.m; 52 for (i=0; i<c.n; i++) 53 for (j=0; j<c.m; j++) 54 for (c.data[i][j]=k=0; k<a.m; k++) 55 c.data[i][j]+=a.data[i][k]*b.data[k][j]; 56 return 1; 57 } 58 } gx; 59 int main(){ 60 int N,K; 61 while(~scanf("%d%d",&N,&K),N|K){ 62 A.n=N; 63 A.m=K; 64 B.n=K; 65 B.m=N; 66 for(int i=0;i<N;i++){ 67 for(int j=0;j<K;j++){ 68 scanf("%d",&A.data[i][j]); 69 } 70 } 71 for(int i=0;i<K;i++){ 72 for(int j=0;j<N;j++){ 73 scanf("%d",&B.data[i][j]); 74 } 75 } 76 gx.mul(C,B,A); 77 LEN=C.n; 78 for(int i=0;i<C.n;i++){ 79 for(int j=0;j<C.m;j++){ 80 D.val[i][j]=C.data[i][j]; 81 } 82 } 83 D=D^(N*N-1); 84 for(int i=0;i<C.n;i++){ 85 for(int j=0;j<C.m;j++){ 86 C.data[i][j]=D.val[i][j]; 87 } 88 } 89 gx.mul(E,A,C); 90 gx.mul(C,E,B); 91 LL ans=0; 92 for(int i=0;i<C.n;i++){ 93 for(int j=0;j<C.m;j++){ 94 ans+=C.data[i][j]%6; 95 } 96 } 97 printf("%I64d\n",ans); 98 } 99 return 0; 100 }
Improving the GPA http://acm.hdu.edu.cn/showproblem.php?pid=4968
4个for循环,突出一个暴力0ms,硬是要写dfs,当练习了,15ms。
1 #include<cstdio> 2 double big,sma; 3 int num[8],judge; 4 int tobig[]={100,84,79,74,69}; 5 int tosma[]={85,80,75,70,60}; 6 double val[]={4.0,3.5,3.0,2.5,2.0}; 7 void dfs(int id,int sum){ 8 if(id==4){ 9 num[4]=sum; 10 int bigsum=0,smasum=0; 11 for(int i=0;i<5;i++){ 12 bigsum+=tobig[i]*num[i]; 13 smasum+=tosma[i]*num[i]; 14 } 15 if(smasum<=judge&&judge<=bigsum){ 16 double now=0; 17 for(int i=0;i<5;i++){ 18 now+=val[i]*num[i]; 19 } 20 big=big>now?big:now; 21 sma=sma<now?sma:now; 22 } 23 return ; 24 } 25 for(int i=0;i<=sum;i++){ 26 num[id]=i; 27 dfs(id+1,sum-i); 28 } 29 } 30 int main(){ 31 int t,n,avg; 32 while(~scanf("%d",&t)){ 33 while(t--){ 34 scanf("%d%d",&avg,&n); 35 sma=1e9; 36 big=0; 37 judge=avg*n; 38 dfs(0,n); 39 printf("%.4f %.4f\n",sma/n,big/n); 40 } 41 } 42 return 0; 43 }
Killing Monsters http://acm.hdu.edu.cn/showproblem.php?pid=4970
这个题目是来卡nlogn的。左++右--处理离线区间覆盖问题。on。
1 #include<cstdio> 2 #include<cstring> 3 #define mt(a,b) memset(a,b,sizeof(a)) 4 typedef __int64 LL; 5 const int M=100010; 6 LL lazy[M],sum[M]; 7 int main(){ 8 int n,m,x,y,d; 9 while(~scanf("%d",&n),n){ 10 mt(lazy,0); 11 scanf("%d",&m); 12 while(m--){ 13 scanf("%d%d%d",&x,&y,&d); 14 lazy[x]+=d; 15 lazy[y+1]-=d; 16 } 17 LL now=0; 18 for(int i=1;i<=n;i++){ 19 now+=lazy[i]; 20 sum[i]=now; 21 } 22 for(int i=n-1;i>=1;i--){ 23 sum[i]+=sum[i+1]; 24 } 25 scanf("%d",&m); 26 LL h; 27 int ans=0; 28 while(m--){ 29 scanf("%I64d%d",&h,&x); 30 if(h>sum[x]) ans++; 31 } 32 printf("%d\n",ans); 33 } 34 return 0; 35 }
end