2017.9.10 考试

T1

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #define ll long long
  5 #define mem(a,b) memset(a,b,sizeof(a))
  6 using namespace std;
  7 
  8 inline ll abss(ll x){return x<0?-x:x;}
  9 
 10 int T;
 11 ll a,b,c;
 12 
 13 ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
 14 
 15 void egcd(ll a,ll b,ll &x,ll &y)
 16 {
 17   if(b==0)
 18   {
 19     x=1;
 20     y=0;
 21     return ;
 22   }
 23   egcd(b,a%b,x,y);
 24   ll temp=x;
 25   x=y;
 26   y=temp-a/b*y;
 27 }
 28 
 29 inline void outans(ll ans)
 30 {
 31     if(ans>65535)
 32         printf("ZenMeZheMeDuo\n");
 33     else
 34         printf("%lld\n",ans);
 35 }
 36 
 37 void work()
 38 {
 39     if(a==b&&b==1)
 40     {
 41         if(c<2)
 42         {
 43             printf("0\n");
 44             return ;
 45         }
 46         outans( (ll)(c-1) );
 47         return ;
 48     }
 49     if(a*b==0)
 50     {
 51         if(a==0&&b==0)
 52         {
 53             if(c==0)
 54                 printf("ZenMeZheMeDuo\n");
 55             else
 56                 printf("0\n");
 57             return ;
 58         }
 59         if(a==0)
 60         {
 61             if(c%b!=0||c==0)
 62                 printf("0\n");
 63             else
 64             {
 65                 if(c*b>0)
 66                     printf("1\n");
 67                 else
 68                     printf("0\n");
 69             }
 70             return ;
 71         }
 72         if(b==0)
 73         {
 74             if(c%a!=0||c==0)
 75                 printf("0\n");
 76             else
 77             {
 78                 if(c*a>0)
 79                     printf("1\n");
 80                 else
 81                     printf("0\n");
 82             }
 83             return ;
 84         }
 85     }
 86     //cout<<0;
 87     ll x,y,temp,tempx,tempy;
 88     temp=gcd(a,b);
 89     if(c%temp!=0)
 90     {
 91         printf("0\n");
 92         return ;
 93     }
 94     a/=temp;b/=temp;c/=temp;
 95     egcd(a,b,x,y);
 96     x*=c;y*=c;
 97     //printf("a=%d b=%d c=%d x=%d y=%d\n",a,b,c,x,y);
 98     tempx=x;tempy=y;
 99     //if(x<0)temp=-(-tempx/b);
100     //else temp=tempx/b;
101     temp=tempx/b;
102     tempx=tempx-temp*b;
103     tempy=tempy+temp*a;
104     if(tempx<=0)
105     {
106         tempx+=b;
107         tempy-=a;
108     }
109     if(a*b<0)
110     {
111         printf("ZenMeZheMeDuo\n");
112         return ;
113     }
114     if(a*b>0)
115     {
116         a=abss(a);b=abss(b);
117         if(tempy<=0)
118         {
119             printf("0\n");
120             return ;
121         }
122         outans( (ll) ((tempy-1)/a+1) );
123         return ;
124     }
125 }
126 
127 int main(){
128 
129     //freopen("1.txt","r",stdin);
130     //freopen("sads.txt","w",stdout);
131     //freopen("fuction9.in","r",stdin);
132     //freopen("tempout.txt","w",stdout);
133 
134     scanf("%d",&T);
135     while(T--)
136     {
137         scanf("%lld%lld%lld",&a,&b,&c);
138         work();
139     }
140 
141 }
80分
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #define ll long long
  5 #define mem(a,b) memset(a,b,sizeof(a))
  6 using namespace std;
  7 
  8 inline ll abss(ll x){return x<0?-x:x;}
  9 
 10 int T;
 11 ll a,b,c;
 12 
 13 ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
 14 
 15 void egcd(ll a,ll b,ll &x,ll &y)
 16 {
 17   if(b==0)
 18   {
 19     x=1;
 20     y=0;
 21     return ;
 22   }
 23   egcd(b,a%b,x,y);
 24   ll temp=x;
 25   x=y;
 26   y=temp-a/b*y;
 27 }
 28 
 29 inline void outans(ll ans)
 30 {
 31     if(ans>65535)
 32         printf("ZenMeZheMeDuo\n");
 33     else
 34         printf("%lld\n",ans);
 35 }
 36 
 37 void work()
 38 {
 39     if(a==b&&b==1)
 40     {
 41         if(c<2)
 42         {
 43             printf("0\n");
 44             return ;
 45         }
 46         outans( (ll)(c-1) );
 47         return ;
 48     }
 49     if(a*b==0)
 50     {
 51         if(a==0&&b==0)
 52         {
 53             if(c==0)
 54                 printf("ZenMeZheMeDuo\n");
 55             else
 56                 printf("0\n");
 57             return ;
 58         }
 59         if(a==0)
 60         {
 61             if(c%b!=0||c==0)
 62                 printf("0\n");
 63             else
 64             {
 65                 if(c*b>0)
 66                     //printf("1\n");
 67                     printf("ZenMeZheMeDuo\n");
 68                 else
 69                     printf("0\n");
 70             }
 71             return ;
 72         }
 73         if(b==0)
 74         {
 75             if(c%a!=0||c==0)
 76                 printf("0\n");
 77             else
 78             {
 79                 if(c*a>0)
 80                     //printf("1\n");
 81                     printf("ZenMeZheMeDuo\n");
 82                 else
 83                     printf("0\n");
 84             }
 85             return ;
 86         }
 87     }
 88     //cout<<0;
 89     ll x,y,temp,tempx,tempy;
 90     temp=gcd(a,b);
 91     if(c%temp!=0)
 92     {
 93         printf("0\n");
 94         return ;
 95     }
 96     a/=temp;b/=temp;c/=temp;
 97     egcd(a,b,x,y);
 98     x*=c;y*=c;
 99     //printf("a=%d b=%d c=%d x=%d y=%d\n",a,b,c,x,y);
100     tempx=x;tempy=y;
101     //if(x<0)temp=-(-tempx/b);
102     //else temp=tempx/b;
103     temp=tempx/b;
104     tempx=tempx-temp*b;
105     tempy=tempy+temp*a;
106     if(tempx<=0)
107     {
108         tempx+=b;
109         tempy-=a;
110     }
111     if(a*b<0)
112     {
113         printf("ZenMeZheMeDuo\n");
114         return ;
115     }
116     if(a*b>0)
117     {
118         a=abss(a);b=abss(b);
119         if(tempy<=0)
120         {
121             printf("0\n");
122             return ;
123         }
124         outans( (ll) ((tempy-1)/a+1) );
125         return ;
126     }
127 }
128 
129 int main(){
130 
131     //freopen("1.txt","r",stdin);
132     //freopen("sads.txt","w",stdout);
133     //freopen("fuction9.in","r",stdin);
134     //freopen("tempout.txt","w",stdout);
135 
136     scanf("%d",&T);
137     while(T--)
138     {
139         scanf("%lld%lld%lld",&a,&b,&c);
140         work();
141     }
142 
143 }
100分

b=0,a*c>0,ZenMeNaMeDuo

因为y可以随便取啊...

T2

f[x][i]表示x这个子树所含的i条边对ans的最大贡献 (有用的性质:每一条边的贡献=(这条边两边的白点个数乘积+黑点乘积)*边val )

f[x][i]=MAX(f[son][j]+f[x][i-j]+边贡献)

注意:

1.注意树上背包时不能重复转移

2.枚举x原来的黑边数和son的黑边数

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define mem(a,b) memset(a,b,sizeof(a))
 5 #define ll long long
 6 using namespace std;
 7 inline ll maxn(ll a,ll b){return a>b?a:b;}
 8 inline int minn(int a,int b){return a<b?a:b;}
 9 struct son
10 {
11     int v,next;
12     ll w;
13 };
14 son a1[20006];
15 int first[20006],e;
16 void addbian(int u,int v,ll w)
17 {
18     a1[e].v=v;
19     a1[e].w=w;
20     a1[e].next=first[u];
21     first[u]=e++;
22 }
23 
24 int n,m;
25 int u,o;
26 ll oo;
27 
28 ll f[2006][2006];
29 int fa[2006],size[2006];
30 ll ha[2006];
31 
32 void dp(int x)
33 {
34     size[x]+=1;
35     f[x][0]=f[x][1]=0;
36     for(int i=first[x];i!=-1;i=a1[i].next)
37     {
38         int temp=a1[i].v;
39         if(temp==fa[x])continue;
40         fa[temp]=x;
41         dp(temp);
42         for(int j=0;j<=m;++j)ha[j]=f[x][j];
43         for(int j=minn(size[x],m);j>=0;--j)
44             for(int k=0;k<=size[temp]&&k+j<=m;++k)
45                 f[x][j+k]=maxn(f[x][j+k],ha[j] + f[temp][k] + a1[i].w * ( (size[temp]-k)*(n-m-size[temp]+k) + k*(m-k) ) );
46 
47         size[x]+=size[temp];
48     }
49 }
50 
51 int main(){
52 
53   //freopen("coloration7.in","r",stdin);
54 
55     mem(first,-1);
56 
57     scanf("%d%d",&n,&m);
58     for(int i=1;i<n;++i)
59     {
60         scanf("%d%d%lld",&u,&o,&oo);
61         addbian(u,o,oo);
62         addbian(o,u,oo);
63     }
64     mem(f,-50);
65     dp(1);
66     cout<<f[1][m];
67 }
100

T3

对模拟进行优化

性质:

1.左上~右下 x-y 相同

2.左下~右上 x+y 相同

所以可以二分做

O( (n+m+k)*logk )

注意原路返回的情况,ans可能需要/2

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define mem(a,b) memset(a,b,sizeof(a))
 5 #define ll long long
 6 using namespace std;
 7 inline ll maxn(ll a,ll b){return a>b?a:b;}
 8 inline int minn(int a,int b){return a<b?a:b;}
 9 struct son
10 {
11     int v,next;
12     ll w;
13 };
14 son a1[20006];
15 int first[20006],e;
16 void addbian(int u,int v,ll w)
17 {
18     a1[e].v=v;
19     a1[e].w=w;
20     a1[e].next=first[u];
21     first[u]=e++;
22 }
23 
24 int n,m;
25 int u,o;
26 ll oo;
27 
28 ll f[2006][2006];
29 int fa[2006],size[2006];
30 ll ha[2006];
31 
32 void dp(int x)
33 {
34     size[x]+=1;
35     f[x][0]=f[x][1]=0;
36     for(int i=first[x];i!=-1;i=a1[i].next)
37     {
38         int temp=a1[i].v;
39         if(temp==fa[x])continue;
40         fa[temp]=x;
41         dp(temp);
42         for(int j=0;j<=m;++j)ha[j]=f[x][j];
43         for(int j=minn(size[x],m);j>=0;--j)
44             for(int k=0;k<=size[temp]&&k+j<=m;++k)
45                 f[x][j+k]=maxn(f[x][j+k],ha[j] + f[temp][k] + a1[i].w * ( (size[temp]-k)*(n-m-size[temp]+k) + k*(m-k) ) );
46 
47         size[x]+=size[temp];
48     }
49 }
50 
51 int main(){
52 
53   //freopen("coloration7.in","r",stdin);
54 
55     mem(first,-1);
56 
57     scanf("%d%d",&n,&m);
58     for(int i=1;i<n;++i)
59     {
60         scanf("%d%d%lld",&u,&o,&oo);
61         addbian(u,o,oo);
62         addbian(o,u,oo);
63     }
64     mem(f,-50);
65     dp(1);
66     cout<<f[1][m];
67 }
100

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <set>
 5 #include <iostream>
 6 #include <vector>
 7 using namespace std;
 8 #define rep(i,a,n) for(int i=a;i<n;i++)
 9 #define per(i,a,n) for(int i=n-1;i>=a;i--)
10 #define pb push_back
11 #define mp make_pair
12 #define all(x) (x).begin(),(x).end()
13 #define SZ(x) ((int)(x).size())
14 #define fi first
15 #define se second
16 typedef long long ll;
17 typedef pair<int,int> PII;
18 const int N=201000;
19 int n,m,k,x,y,dx,dy,bx[N],by[N];
20 char dir[10];
21 set<int> bk[N],pnt[N];
22 vector<PII> tv1[N],tv2[N];
23 ll ret;
24 void ff(){
25     int px=x,py=y;
26     if(dx==-1)px+=1;
27     if(dy==-1)py+=1;
28     if(!bk[px+dx].count(py+dy))return;
29     if((bk[px+dx].count(py)^bk[px].count(py+dy))==0)dx*=-1,dy*=-1;
30     else if(bk[px+dx].count(py))dx*=-1;
31     else dy*=-1;
32 }
33 int gg(){
34     if(dx==1&&dy==1)return *pnt[x+m-y].upper_bound(x)-x;
35     else if(dx==1&&dy==-1)return *pnt[x+y].upper_bound(x)-x;
36     else if(dx==-1&&dy==1)return x-*(--pnt[x+y].lower_bound(x));
37     else return x-*(--pnt[x+m-y].lower_bound(x));
38 }
39 void addpnt(int px,int py){
40     if((px+py+x+y)%2==0){
41         pnt[px+py].insert(px);
42         pnt[px+m-py].insert(px);
43     }
44 }
45 void addrec(int d){
46     if(dx==1&&dy==1)tv1[x+m-y].pb(mp(x+1,x+d+1));
47     else if(dx==-1&&dy==-1)tv1[x+m-y].pb(mp(x-d+1,x+1));
48     else if(dx==1&&dy==-1)tv2[x+y].pb(mp(x+1,x+d+1));
49     else tv2[x+y].pb(mp(x-d+1,x+1));
50 }
51 void gao(vector<PII> &a){
52     if (!SZ(a))return;
53     sort(all(a));
54     ret+=a[0].se-a[0].fi;
55     rep(i,1,SZ(a)){
56         a[i].fi=max(a[i-1].se,a[i].fi);
57         a[i].se=max(a[i].se,a[i].fi);
58         ret+=a[i].se-a[i].fi;
59     }
60 }
61 int main(){
62     scanf("%d%d%d",&n,&m,&k);
63     rep(i,0,k)scanf("%d%d",bx+i,by+i);
64     scanf("%d%d%s",&x,&y,dir);
65     if(dir[0]=='S')dx=1; else dx=-1;
66     if(dir[1]=='E')dy=1; else dy=-1;
67     if(dx==1)--x;
68     if(dy==1)--y;
69     addpnt(x,y);
70     rep(i,0,n+1)addpnt(i,0),addpnt(i,m);
71     rep(i,0,m+1)addpnt(0,i),addpnt(n,i);
72     rep(i,1,n+1)bk[i].insert(0),bk[i].insert(m+1);
73     rep(i,0,m+2)bk[0].insert(i),bk[n+1].insert(i);
74     rep(i,0,k){
75         int cx=bx[i],cy=by[i];
76         addpnt(cx-1,cy-1);
77         addpnt(cx,cy-1);
78         addpnt(cx-1,cy);
79         addpnt(cx,cy);
80         bk[cx].insert(cy);
81     }
82     int sx0=x,sy0=y,sx1=dx,sy1=dy;
83     while(1){
84         int d=gg();
85         addrec(d);
86         x=x+d*dx; y=y+d*dy;
87         ff();
88         if(x==sx0&&y==sy0&&dx==sx1&&dy==sy1)break;
89     }
90     rep(i,0,n+m+1)gao(tv1[i]),gao(tv2[i]);
91     cout<<ret;
92 }
std

 

posted @ 2017-09-10 20:23  A_LEAF  阅读(98)  评论(0编辑  收藏  举报