2018 hdu 多校 第三场

1001 小甜甜

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <climits>
14 
15 using namespace std;
16 
17 const int maxN=1e7+7;
18 
19 int a[maxN];
20 int nex[maxN];
21 
22 int sta[maxN],top;
23 
24 int que[maxN*2],head,tail;
25 int tmp[maxN];
26 
27 void work(){
28     int n,m,k,p,q,r,MOD;
29     scanf("%d%d%d%d%d%d%d",&n,&m,&k,&p,&q,&r,&MOD);
30     for (int i=1;i<=k;i++) scanf("%d",a+i);
31     for (int i=k+1;i<=n;i++) a[i]=(1LL*p*a[i-1]+1LL*q*i+r)%MOD;
32 
33     nex[n]=n+1;
34     top=0;
35     for (int i=n;i>0;i--){
36         while (top>0 && a[i]>=a[sta[top]]) top--;
37         if (top==0) nex[i]=maxN;
38         else nex[i]=sta[top];
39         sta[++top]=i;
40     }
41 
42     //for (int i=1;i<=n;i++) cerr<<i<<' '<<nex[i]<<endl;
43 
44     long long ans1=0,ans2=0;
45 
46 
47     head=tail=maxN;
48     head++;
49 
50     int cnt,j;
51     for (long long i=1;i+m-1<=n;i++) {
52         cnt=0; j=i;
53         while (j<i+m && (head>tail || a[j]<a[que[head]])) {
54             tmp[++cnt]=j;
55             j=nex[j];
56         }
57         for (j=cnt;j>0;j--) que[--head]=tmp[j];
58         j=nex[que[tail]];
59         while (j<i+m){
60             que[++tail]=j;
61             j=nex[j];
62         }
63 
64         ans1+=((long long)a[que[tail]])^i;
65         ans2+=((long long)(tail-head+1))^i;
66 
67         head++;
68     }
69 
70     printf("%lld %lld\n",ans1,ans2);
71 }
72 
73 int main(){
74     int T;
75     scanf("%d",&T);
76     while (T--) work();
77     return 0;
78 }
View Code

 

1004 小甜甜

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <climits>
14 
15 using namespace std;
16 
17 int _get(int x){
18     int ans=0;
19     for (int i=1;i<=x;i++) 
20         if (__gcd(x,i)==1) ans++;
21     return ans;
22 }
23 
24 int prime(int x){
25     for (int i=2;i*i<=x;i++) 
26         if (x%i==0) return 0;
27     return 1;
28 }
29 
30 bool judge(int x){
31     int k=_get(x);
32     if (prime(k)) return 0;
33     return 1;
34 }
35 
36 int main(){
37     int T;
38     scanf("%d",&T);
39     while (T--){
40         int k;
41         scanf("%d",&k);
42         if (k==1) puts("5");
43         else if (k==2) puts("7");
44         else printf("%d\n",k+5);
45     }
46 }
47 
48 /*
49 1 2 3 4 6
50 */
View Code

 

1006 BPM136 考虑一下异或和就好了,很精妙

  1 /* ***********************************************
  2 Author        :BPM136
  3 Created Time  :2018/7/30 13:01:00
  4 File Name     :1006.cpp
  5 ************************************************ */
  6 
  7 #include<iostream>
  8 #include<cstdio>
  9 #include<algorithm>
 10 #include<cstdlib>
 11 #include<cmath>
 12 #include<cstring>
 13 #include<iomanip>
 14 #include<bitset>
 15 #include<queue>
 16 #include<ctime>
 17 #include<set>
 18 #include<map>
 19 #include<vector>
 20 #include<cassert>
 21 #include<functional>
 22 #define pb push_back
 23 #define popb pop_back
 24 #define MID ((l+r)>>1)
 25 #define LSON (k<<1)
 26 #define RSON (k<<1|1)
 27 #define get(a,i) ((a)&(1<<(i-1)))
 28 #define PAU putchar(32)
 29 #define ENT putchar(10)
 30 #define clr(a,b) memset(a,b,sizeof(a))
 31 #define rep(_i,_a,_b) for(int _i=(_a);_i<=(_b);_i++)
 32 #define down(_i,_a,_b) for(int _i=(_a);_i>=(_b);_i--)
 33 #define FOR(_i,_b) for(int _i=1;_i<=(_b);_i++)
 34 #define efo(_i,_a) for(int _i=last[(_a)];_i!=0;_i=e[_i].next)
 35 #define Remax(a,b) if(b>a) a=b;
 36 #define Remin(a,b) if(b<a) a=b;
 37 #define filein(x) freopen(#x".in","r",stdin)
 38 #define fileout(x) freopen(#x".out","w",stdout)
 39 #define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
 40 #define mkd(x) freopen(#x".in","w",stdout);
 41 #define setlargestack(x) int _SIZE=x<<20;char *_PPP=(char*)malloc(size)+_SIZE;__asm__("movl %0, %%esp\n" :: "r"(_PPP));
 42 #define END system("pause")
 43 #define read2(a,b) read(a),read(b)
 44 #define read3(a,b,c) read(a),read(b),read(c)
 45 #define readg(_x1,_y1,_x2,_y2) read(_x1),read(_y1),read(_x2),read(_y2)
 46 using namespace std;
 47 
 48 typedef long long ll;
 49 typedef double DB;
 50 typedef long double LD;
 51 typedef unsigned int  UI;
 52 typedef unsigned long long ULL;
 53 typedef vector<int> VI;
 54 typedef set<int> SI;
 55 typedef set<int , greater<int> > SIG;
 56 typedef map<int, int > MII;
 57 typedef map<int, int, greater<int> > MIIG;
 58 
 59 namespace fastIO{  
 60     #define BUF_SIZE 100000  
 61     #define OUT_SIZE 100000  
 62     //fread->read  
 63     bool IOerror=0;  
 64     inline char nc(){  
 65         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;  
 66         if (p1==pend){  
 67             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);  
 68             if (pend==p1){IOerror=1;return -1;}  
 69             //{printf("IO error!\n");system("pause");for (;;);exit(0);}  
 70         }  
 71         return *p1++;  
 72     }  
 73     inline bool blank(char ch){return ch==32||ch==10||ch==13||ch==9;}  
 74     inline bool enter(char ch){return ch==10||ch==13;}
 75     inline void read(int &x){  
 76         bool sign=0; char ch=nc(); x=0;  
 77         for (;blank(ch);ch=nc());  
 78         if (IOerror)return;  
 79         if (ch==45)sign=1,ch=nc();  
 80         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 81         if (sign)x=-x;  
 82     }  
 83     inline void read(ll &x){  
 84         bool sign=0; char ch=nc(); x=0;  
 85         for (;blank(ch);ch=nc());  
 86         if (IOerror)return;  
 87         if (ch==45)sign=1,ch=nc();  
 88         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 89         if (sign)x=-x;  
 90     }  
 91     inline void read(double &x){  
 92         bool sign=0; char ch=nc(); x=0;  
 93         for (;blank(ch);ch=nc());  
 94         if (IOerror)return;  
 95         if (ch==45)sign=1,ch=nc();  
 96         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 97         if (ch==46){  
 98             double tmp=1; ch=nc();  
 99             for (;ch>=48&&ch<=57;ch=nc())tmp/=10.0,x+=tmp*(ch-48);  
100         }  
101         if (sign)x=-x;  
102     }  
103     inline void read(char *s){  
104         char ch=nc();  
105         for (;blank(ch);ch=nc());  
106         if (IOerror)return;  
107         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;  
108         *s=0;  
109     }  
110     inline void read(char *s,bool f) {
111         char ch=nc();
112         for (;blank(ch);ch=nc());
113         if(IOerror)return;
114         for(;!enter(ch)&&!IOerror;ch=nc())*s++=ch;
115         *s=0;
116     }
117     inline void read(char &c){  
118         for (c=nc();blank(c);c=nc());  
119         if (IOerror){c=-1;return;}  
120     } 
121 #undef OUT_SIZE  
122 #undef BUF_SIZE  
123 }; using namespace fastIO;
124 
125 int a[100005];
126 
127 int main() {
128     int T;
129     read(T);
130     while(T--) {
131         int n,x,y;
132         read(n);
133         int sum=0;
134         rep(i,1,n) {
135             read(a[i]);
136             sum^=a[i];
137         }
138         rep(i,1,n-1) {
139             read2(x,y);
140         }
141         if(sum) puts("Q"); else puts("D");
142     }
143     return 0;
144 }
View Code

 

1007 小洛洛

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 typedef long long ll;
10 
11 template<typename T = ll>
12 struct P_ {
13     T x, y;
14     int idx;
15     bool opt;
16 
17     bool operator<(const P_<T> &r) const {
18         if(x != r.x) return x < r.x;
19         if(y != r.y) return y < r.y;
20         return idx < r.idx;
21     }
22     bool operator==(const P_<T> &r) const {
23         return x == r.x && y == r.y;
24     }
25 
26     P_(T _x = 0, T _y = 0, int _idx = 0): x(_x), y(_y), idx(_idx), opt(false) {}
27     P_<T> operator+(const P_<T> &r) const { return P_(x + r.x, y + r.y); }
28     P_<T> operator-(const P_<T> &r) const { return P_(x - r.x, y - r.y); }
29     T operator^(const P_<T> &r) const { return x * r.y - y * r.x; }
30 };
31 
32 bool cmp1(const P_<ll> *a, const P_<ll> *b) {
33     if(a->idx != b->idx) return a->idx < b->idx;
34     return a->x < b->x;
35 }
36 
37 template<typename T>
38 P_<T> **hull_(P_<T> *l, P_<T> *r, P_<T> **o) {
39     sort(l, r);
40     r = unique(l, r);
41     auto po = o;
42     for(auto p = l; p != r; *po++ = p++) {
43         while(po - o >= 2 && ((*po[-1] - *po[-2]) ^ (*p - *po[-1])) > 0)
44             --po;
45         if(po - o >= 2 && ((*po[-1] - *po[-2]) ^ (*p - *po[-1])) == 0)
46             po[-1]->opt = true;
47     }
48     return po;
49 }
50 
51 const int N = 200003;
52 P_<ll> pts[N], *ss[N];
53 
54 int main() {
55     int t;
56     scanf("%d", &t);
57     while(t--) {
58         int n;
59         scanf("%d", &n);
60         for(int i = 0; i < n; ++i) {
61             int x, y;
62             scanf("%d%d", &x, &y);
63             pts[i] = P_<ll>((ll)x, (ll)y, i + 1);
64         }
65         pts[n] = pts[0], pts[n + 1] = pts[1];
66         auto sp = hull_(pts, pts + n, ss);
67         printf("1");
68         sp[-1]->opt = false;
69         for(auto p = ss + 1; p != sp; ++p) {
70             if((*p)->opt) {
71                 auto q = p + 1;
72                 while(q[0]->opt)
73                     ++q;
74                 sort(p, q, cmp1);
75                 int lastx = 0;
76                 for(; p != q && p[0]->idx < q[0]->idx; ++p)
77                     if(p[0]->x > lastx) {
78                         printf(" %d", p[0]->idx);
79                         lastx = p[0]->x;
80                     }
81                 p = q - 1;
82             } else
83                 printf(" %d", p[0]->idx);
84         }
85         putchar('\n');
86     }
87     return 0;
88 }
View Code

 

1009 BPM136

考虑暴力,用fi,j,k,l表示处理到第i位,且前3个数分别是j,k,l的期望是多少

然后发现可以变成第i位为j,x=gcd(j,k),y=gcd(x,l),那么用fi,j,x,y也是可以转移的

那么就预处理一下状态发现合法的j,x,y是非常少的

暴力转移就好了

  1 /* ***********************************************
  2 Author        :BPM136
  3 Created Time  :2018/8/3 12:06:46
  4 File Name     :1009.cpp
  5 ************************************************ */
  6 
  7 #include<iostream>
  8 #include<cstdio>
  9 #include<algorithm>
 10 #include<cstdlib>
 11 #include<cmath>
 12 #include<cstring>
 13 #include<iomanip>
 14 #include<bitset>
 15 #include<queue>
 16 #include<ctime>
 17 #include<set>
 18 #include<map>
 19 #include<vector>
 20 #include<cassert>
 21 #include<functional>
 22 #define pb push_back
 23 #define popb pop_back
 24 #define MID ((l+r)>>1)
 25 #define LSON (k<<1)
 26 #define RSON (k<<1|1)
 27 #define get(a,i) ((a)&(1<<(i-1)))
 28 #define PAU putchar(32)
 29 #define ENT putchar(10)
 30 #define clr(a,b) memset(a,b,sizeof(a))
 31 #define rep(_i,_a,_b) for(int _i=(_a);_i<=(_b);_i++)
 32 #define down(_i,_a,_b) for(int _i=(_a);_i>=(_b);_i--)
 33 #define FOR(_i,_b) for(int _i=1;_i<=(_b);_i++)
 34 #define efo(_i,_a) for(int _i=last[(_a)];_i!=0;_i=e[_i].next)
 35 #define Remax(a,b) if(b>a) a=b;
 36 #define Remin(a,b) if(b<a) a=b;
 37 #define filein(x) freopen(#x".in","r",stdin)
 38 #define fileout(x) freopen(#x".out","w",stdout)
 39 #define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
 40 #define mkd(x) freopen(#x".in","w",stdout);
 41 #define setlargestack(x) int _SIZE=x<<20;char *_PPP=(char*)malloc(size)+_SIZE;__asm__("movl %0, %%esp\n" :: "r"(_PPP));
 42 #define END system("pause")
 43 #define read2(a,b) read(a),read(b)
 44 #define read3(a,b,c) read(a),read(b),read(c)
 45 #define readg(_x1,_y1,_x2,_y2) read(_x1),read(_y1),read(_x2),read(_y2)
 46 using namespace std;
 47 
 48 typedef long long ll;
 49 typedef double DB;
 50 typedef long double LD;
 51 typedef unsigned int  UI;
 52 typedef unsigned long long ULL;
 53 typedef vector<int> VI;
 54 typedef set<int> SI;
 55 typedef set<int , greater<int> > SIG;
 56 typedef map<int, int > MII;
 57 typedef map<int, int, greater<int> > MIIG;
 58 
 59 namespace fastIO{  
 60     #define BUF_SIZE 100000  
 61     #define OUT_SIZE 100000  
 62     //fread->read  
 63     bool IOerror=0;  
 64     inline char nc(){  
 65         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;  
 66         if (p1==pend){  
 67             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);  
 68             if (pend==p1){IOerror=1;return -1;}  
 69             //{printf("IO error!\n");system("pause");for (;;);exit(0);}  
 70         }  
 71         return *p1++;  
 72     }  
 73     inline bool blank(char ch){return ch==32||ch==10||ch==13||ch==9;}  
 74     inline bool enter(char ch){return ch==10||ch==13;}
 75     inline void read(int &x){  
 76         bool sign=0; char ch=nc(); x=0;  
 77         for (;blank(ch);ch=nc());  
 78         if (IOerror)return;  
 79         if (ch==45)sign=1,ch=nc();  
 80         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 81         if (sign)x=-x;  
 82     }  
 83     inline void read(ll &x){  
 84         bool sign=0; char ch=nc(); x=0;  
 85         for (;blank(ch);ch=nc());  
 86         if (IOerror)return;  
 87         if (ch==45)sign=1,ch=nc();  
 88         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 89         if (sign)x=-x;  
 90     }  
 91     inline void read(double &x){  
 92         bool sign=0; char ch=nc(); x=0;  
 93         for (;blank(ch);ch=nc());  
 94         if (IOerror)return;  
 95         if (ch==45)sign=1,ch=nc();  
 96         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 97         if (ch==46){  
 98             double tmp=1; ch=nc();  
 99             for (;ch>=48&&ch<=57;ch=nc())tmp/=10.0,x+=tmp*(ch-48);  
100         }  
101         if (sign)x=-x;  
102     }  
103     inline void read(char *s){  
104         char ch=nc();  
105         for (;blank(ch);ch=nc());  
106         if (IOerror)return;  
107         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;  
108         *s=0;  
109     }  
110     inline void read(char *s,bool f) {
111         char ch=nc();
112         for (;blank(ch);ch=nc());
113         if(IOerror)return;
114         for(;!enter(ch)&&!IOerror;ch=nc())*s++=ch;
115         *s=0;
116     }
117     inline void read(char &c){  
118         for (c=nc();blank(c);c=nc());  
119         if (IOerror){c=-1;return;}  
120     } 
121 #undef OUT_SIZE  
122 #undef BUF_SIZE  
123 }; using namespace fastIO;
124 
125 const int N = 105;
126 const int ST = 1500;
127 const ll MOD = 1e9 + 7;
128 
129 struct status {
130     int p,gcd_2,gcd_3;
131 }st[ST];
132 int cnt=0;
133 
134 int n,m;
135 ll a[N];
136 ll v[N];
137 ll inv[N];
138 ll f[N][ST];
139 int gcd[N][N];
140 
141 int dfn[N][N][N];
142 
143 int gcd_(int x,int y) {
144     if(y==0) return x;
145     return gcd_(y,x%y);
146 }
147 
148 void prework() {
149     inv[0]=inv[1]=1;
150     rep(i,2,N-1) inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
151 
152     rep(i,1,100) rep(j,1,100) gcd[i][j]=gcd_(i,j);
153 
154     rep(i,1,100) rep(j,1,i) rep(k,1,j) if(i%j==0 && j%k==0) {
155         dfn[i][j][k]=++cnt;
156         st[cnt]=status{i,j,k};
157     }
158 }
159 
160 bool is_(int x,int y) {
161     if(y==0) return 1;
162     if(x==y) return 1;
163     return 0;
164 }
165 
166 int main() {
167     prework();
168 
169     int T;
170     read(T);
171     while(T--) {
172         read2(n,m);
173         rep(i,1,n) read(a[i]);
174         rep(i,1,m) read(v[i]);
175 
176         clr(f,0);
177 
178         int x,y;
179         rep(i,1,m) rep(j,1,m) rep(k,1,m) if(is_(i,a[1]) && is_(j,a[2]) && is_(k,a[3])) {
180             f[3][ dfn[k][x=gcd[k][j]][gcd[x][i]] ]++;
181         }
182 
183         rep(i,3,n-1) rep(j,1,cnt) if(f[i][j]) rep(p,1,m) if(is_(p,a[i+1])) {
184             x=gcd[p][st[j].p];
185             y=gcd[p][st[j].gcd_2];
186             (f[i+1][ dfn[p][x][y] ]+=(ll)v[ gcd[p][st[j].gcd_3] ]*f[i][j])%=MOD;
187         }
188         
189         ll ans=0;
190         rep(i,1,cnt) (ans+=f[n][i])%=MOD;
191         rep(i,1,n) if(a[i]==0) (ans*=inv[m])%=MOD;
192         cout<<ans<<endl;
193     }
194     return 0;
195 }
View Code

 

1012 BPM136

  1 /* ***********************************************
  2 Author        :BPM136
  3 Created Time  :2018/7/30 12:19:58
  4 File Name     :1012.cpp
  5 ************************************************ */
  6 
  7 #include<iostream>
  8 #include<cstdio>
  9 #include<algorithm>
 10 #include<cstdlib>
 11 #include<cmath>
 12 #include<cstring>
 13 #include<iomanip>
 14 #include<bitset>
 15 #include<queue>
 16 #include<ctime>
 17 #include<set>
 18 #include<vector>
 19 #include<cassert>
 20 #include<functional>
 21 #define pb push_back
 22 #define popb pop_back
 23 #define MID ((l+r)>>1)
 24 #define LSON (k<<1)
 25 #define RSON (k<<1|1)
 26 #define get(a,i) ((a)&(1<<(i-1)))
 27 #define PAU putchar(32)
 28 #define ENT putchar(10)
 29 #define clr(a,b) memset(a,b,sizeof(a))
 30 #define rep(_i,_a,_b) for(int _i=(_a);_i<=(_b);_i++)
 31 #define down(_i,_a,_b) for(int _i=(_a);_i>=(_b);_i--)
 32 #define FOR(_i,_b) for(int _i=1;_i<=(_b);_i++)
 33 #define efo(_i,_a) for(int _i=last[(_a)];_i!=0;_i=e[_i].next)
 34 #define Remax(a,b) if(b>a) a=b;
 35 #define Remin(a,b) if(b<a) a=b;
 36 #define filein(x) freopen(#x".in","r",stdin)
 37 #define fileout(x) freopen(#x".out","w",stdout)
 38 #define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
 39 #define mkd(x) freopen(#x".in","w",stdout);
 40 #define setlargestack(x) int _SIZE=x<<20;char *_PPP=(char*)malloc(size)+_SIZE;__asm__("movl %0, %%esp\n" :: "r"(_PPP));
 41 #define END system("pause")
 42 #define read2(a,b) read(a),read(b)
 43 #define read3(a,b,c) read(a),read(b),read(c)
 44 #define readg(_x1,_y1,_x2,_y2) read(_x1),read(_y1),read(_x2),read(_y2)
 45 using namespace std;
 46 
 47 typedef long long ll;
 48 typedef double DB;
 49 typedef long double LD;
 50 typedef unsigned int  UI;
 51 typedef unsigned long long ULL;
 52 typedef vector<int> VI;
 53 typedef set<int> SI;
 54 typedef set<int , greater<int> > SIG;
 55 
 56 namespace fastIO{  
 57     #define BUF_SIZE 100000  
 58     #define OUT_SIZE 100000  
 59     //fread->read  
 60     bool IOerror=0;  
 61     inline char nc(){  
 62         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;  
 63         if (p1==pend){  
 64             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);  
 65             if (pend==p1){IOerror=1;return -1;}  
 66             //{printf("IO error!\n");system("pause");for (;;);exit(0);}  
 67         }  
 68         return *p1++;  
 69     }  
 70     inline bool blank(char ch){return ch==32||ch==10||ch==13||ch==9;}  
 71     inline bool enter(char ch){return ch==10||ch==13;}
 72     inline void read(int &x){  
 73         bool sign=0; char ch=nc(); x=0;  
 74         for (;blank(ch);ch=nc());  
 75         if (IOerror)return;  
 76         if (ch==45)sign=1,ch=nc();  
 77         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 78         if (sign)x=-x;  
 79     }  
 80     inline void read(ll &x){  
 81         bool sign=0; char ch=nc(); x=0;  
 82         for (;blank(ch);ch=nc());  
 83         if (IOerror)return;  
 84         if (ch==45)sign=1,ch=nc();  
 85         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 86         if (sign)x=-x;  
 87     }  
 88     inline void read(double &x){  
 89         bool sign=0; char ch=nc(); x=0;  
 90         for (;blank(ch);ch=nc());  
 91         if (IOerror)return;  
 92         if (ch==45)sign=1,ch=nc();  
 93         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 94         if (ch==46){  
 95             double tmp=1; ch=nc();  
 96             for (;ch>=48&&ch<=57;ch=nc())tmp/=10.0,x+=tmp*(ch-48);  
 97         }  
 98         if (sign)x=-x;  
 99     }  
100     inline void read(char *s){  
101         char ch=nc();  
102         for (;blank(ch);ch=nc());  
103         if (IOerror)return;  
104         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;  
105         *s=0;  
106     }  
107     inline void read(char *s,bool f) {
108         char ch=nc();
109         for (;blank(ch);ch=nc());
110         if(IOerror)return;
111         for(;!enter(ch)&&!IOerror;ch=nc())*s++=ch;
112         *s=0;
113     }
114     inline void read(char &c){  
115         for (c=nc();blank(c);c=nc());  
116         if (IOerror){c=-1;return;}  
117     } 
118 #undef OUT_SIZE  
119 #undef BUF_SIZE  
120 }; using namespace fastIO;
121 
122 const int N = 105;
123 
124 char map[N][N];
125 
126 int main() {
127     int T;
128     read(T);
129     while(T--) {
130         int n,m,k;
131         read3(n,m,k);
132         rep(i,0,100) rep(j,0,100) map[i][j]='.';
133         int len=m*2+k*2+1;
134         int be=m*2+1;
135         int flag=0;
136         rep(i,be,len) {
137             char tmp;
138             if(flag==0) tmp='+'; else tmp='|';
139             rep(j,1,n*2+1) {
140                 map[i][j]=tmp;
141                 if(tmp=='+') tmp='-'; else
142                 if(tmp=='-') tmp='+';
143                 if(tmp=='|') tmp='.'; else
144                 if(tmp=='.') tmp='|';
145             }
146             flag=1-flag;
147         }
148         int p=be,q=n*2+1;
149         char tmp='+';
150         while(p>0 && q>0) {
151             map[p][q]=tmp;
152             if(tmp=='+') tmp='/'; else tmp='+';
153             p--;
154             q++;
155             char new_c=tmp;
156             down(i,q,q-n*2) {
157                 map[p][i]=new_c;
158                 if(new_c=='/') new_c='.'; else 
159                 if(new_c=='.') new_c='/';
160                 if(new_c=='+') new_c='-'; else
161                 if(new_c=='-') new_c='+';
162             }
163             new_c=tmp;
164             rep(i,p,p+k*2) {
165                 map[i][q]=new_c;
166                 if(new_c=='/') new_c='.'; else
167                 if(new_c=='.') new_c='/';
168                 if(new_c=='+') new_c='|'; else
169                 if(new_c=='|') new_c='+';
170             }
171         }
172         rep(i,1,len) {
173             rep(j,1,m*2+n*2+1) putchar(map[i][j]);
174             ENT;
175         }
176     }
177     return 0;
178 }
View Code

 

1013 BPM136

分块,

定义dp_a(i,j,k)表示从i出发,到达j点,并经过刚好100k(k100)条边的最短路。

定义dp_b(i,j,k)表示从i出发,到达j点,并经过刚好k(k100)条边的最短路。

定义dp_c(i,j,k)表示从i出发,到达j点,并经过至少k(k100)条边的最短路

dp_b(i,j,k)=min{dp_b(i,l,k1)+w[l][j]}

dp_a(i,j,k)=min{dp_a(i,l,k1)+dp_b(l,j,100)}

dp_c(i,j,k)=min{dp_b(i,l,k)+dis(l,j)}

  1 /* ***********************************************
  2 Author        :BPM136
  3 Created Time  :2018/8/2 22:16:32
  4 File Name     :1013G.cpp
  5 ************************************************ */
  6 
  7 #include<iostream>
  8 #include<cstdio>
  9 #include<algorithm>
 10 #include<cstdlib>
 11 #include<cmath>
 12 #include<cstring>
 13 #include<iomanip>
 14 #include<bitset>
 15 #include<queue>
 16 #include<ctime>
 17 #include<set>
 18 #include<map>
 19 #include<vector>
 20 #include<cassert>
 21 #include<functional>
 22 #define pb push_back
 23 #define popb pop_back
 24 #define MID ((l+r)>>1)
 25 #define LSON (k<<1)
 26 #define RSON (k<<1|1)
 27 #define get(a,i) ((a)&(1<<(i-1)))
 28 #define PAU putchar(32)
 29 #define ENT putchar(10)
 30 #define clr(a,b) memset(a,b,sizeof(a))
 31 #define rep(_i,_a,_b) for(int _i=(_a);_i<=(_b);_i++)
 32 #define down(_i,_a,_b) for(int _i=(_a);_i>=(_b);_i--)
 33 #define FOR(_i,_b) for(int _i=1;_i<=(_b);_i++)
 34 #define efo(_i,_a) for(int _i=last[(_a)];_i!=0;_i=e[_i].next)
 35 #define Remax(a,b) if(b>a) a=b;
 36 #define Remin(a,b) if(b<a) a=b;
 37 #define filein(x) freopen(#x".in","r",stdin)
 38 #define fileout(x) freopen(#x".out","w",stdout)
 39 #define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
 40 #define mkd(x) freopen(#x".in","w",stdout);
 41 #define setlargestack(x) int _SIZE=x<<20;char *_PPP=(char*)malloc(size)+_SIZE;__asm__("movl %0, %%esp\n" :: "r"(_PPP));
 42 #define END system("pause")
 43 #define read2(a,b) read(a),read(b)
 44 #define read3(a,b,c) read(a),read(b),read(c)
 45 #define readg(_x1,_y1,_x2,_y2) read(_x1),read(_y1),read(_x2),read(_y2)
 46 using namespace std;
 47 
 48 typedef long long ll;
 49 typedef double DB;
 50 typedef long double LD;
 51 typedef unsigned int  UI;
 52 typedef unsigned long long ULL;
 53 typedef vector<int> VI;
 54 typedef set<int> SI;
 55 typedef set<int , greater<int> > SIG;
 56 typedef map<int, int > MII;
 57 typedef map<int, int, greater<int> > MIIG;
 58 
 59 namespace fastIO{  
 60     #define BUF_SIZE 100000  
 61     #define OUT_SIZE 100000  
 62     //fread->read  
 63     bool IOerror=0;  
 64     inline char nc(){  
 65         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;  
 66         if (p1==pend){  
 67             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);  
 68             if (pend==p1){IOerror=1;return -1;}  
 69             //{printf("IO error!\n");system("pause");for (;;);exit(0);}  
 70         }  
 71         return *p1++;  
 72     }  
 73     inline bool blank(char ch){return ch==32||ch==10||ch==13||ch==9;}  
 74     inline bool enter(char ch){return ch==10||ch==13;}
 75     inline void read(int &x){  
 76         bool sign=0; char ch=nc(); x=0;  
 77         for (;blank(ch);ch=nc());  
 78         if (IOerror)return;  
 79         if (ch==45)sign=1,ch=nc();  
 80         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 81         if (sign)x=-x;  
 82     }  
 83     inline void read(ll &x){  
 84         bool sign=0; char ch=nc(); x=0;  
 85         for (;blank(ch);ch=nc());  
 86         if (IOerror)return;  
 87         if (ch==45)sign=1,ch=nc();  
 88         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 89         if (sign)x=-x;  
 90     }  
 91     inline void read(double &x){  
 92         bool sign=0; char ch=nc(); x=0;  
 93         for (;blank(ch);ch=nc());  
 94         if (IOerror)return;  
 95         if (ch==45)sign=1,ch=nc();  
 96         for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48;  
 97         if (ch==46){  
 98             double tmp=1; ch=nc();  
 99             for (;ch>=48&&ch<=57;ch=nc())tmp/=10.0,x+=tmp*(ch-48);  
100         }  
101         if (sign)x=-x;  
102     }  
103     inline void read(char *s){  
104         char ch=nc();  
105         for (;blank(ch);ch=nc());  
106         if (IOerror)return;  
107         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;  
108         *s=0;  
109     }  
110     inline void read(char *s,bool f) {
111         char ch=nc();
112         for (;blank(ch);ch=nc());
113         if(IOerror)return;
114         for(;!enter(ch)&&!IOerror;ch=nc())*s++=ch;
115         *s=0;
116     }
117     inline void read(char &c){  
118         for (c=nc();blank(c);c=nc());  
119         if (IOerror){c=-1;return;}  
120     } 
121 #undef OUT_SIZE  
122 #undef BUF_SIZE  
123 }; using namespace fastIO;
124 
125 const int N = 105;
126 
127 int dp_a[N][N][N];
128 int dp_b[N][N][N];
129 int dp_c[N][N][N];
130 int n,m;
131 int w[N][N];
132 int dis[N][N];
133 
134 int min_(int x,int y) {
135     if(x==-1) return y;
136     if(y==-1) return x;
137     return min(x,y);
138 }
139 
140 void init() {
141     clr(w,-1);
142     clr(dp_a,-1);
143     clr(dp_b,-1);
144     clr(dp_c,-1);
145     clr(dis,-1);
146 }
147 
148 int main() {
149     int T;
150     read(T);
151     while(T--) {
152         read2(n,m);
153         init();
154         rep(i,1,m) {
155             int x,y,_w;
156             read3(x,y,_w);
157             w[x][y]=min_(w[x][y], _w);
158             dis[x][y]=w[x][y];
159         }
160         rep(i,1,n) dis[i][i]=dp_a[i][i][0]=dp_b[i][i][0]=0;
161 
162         rep(k,1,n) rep(i,1,n) rep(j,1,n) if(dis[i][k]!=-1 && dis[k][j]!=-1) 
163             dis[i][j]=min_(dis[i][j], dis[i][k]+dis[k][j]);
164 
165         rep(k,1,100) rep(i,1,n) rep(j,1,n) rep(l,1,n) if(dp_b[i][l][k-1]!=-1 && w[l][j]!=-1) 
166             dp_b[i][j][k]=min_(dp_b[i][j][k], dp_b[i][l][k-1]+w[l][j]);
167 
168         rep(k,0,100) rep(i,1,n) rep(j,1,n) rep(l,1,n) {
169             if(k && dp_a[i][l][k-1]!=-1 && dp_b[l][j][100]!=-1) 
170               dp_a[i][j][k]=min_(dp_a[i][j][k], dp_a[i][l][k-1]+dp_b[l][j][100]);
171             if(dp_b[i][l][k]!=-1 && dis[l][j]!=-1) 
172               dp_c[i][j][k]=min_(dp_c[i][j][k], dp_b[i][l][k]+dis[l][j]);
173         }
174 
175         int Q;
176         read(Q);
177         rep(o,1,Q) {
178             int u,v,ki;
179             read3(u, v, ki);
180             int ans=-1;
181             rep(i,1,n) if(dp_a[u][i][ki / 100]!=-1 && dp_c[i][v][ki % 100]!=-1) 
182                 ans=min_(ans, dp_a[u][i][ki / 100]+dp_c[i][v][ki % 100]);
183             printf("%d\n",ans);
184         }
185     }
186     return 0;
187 }
View Code

 

posted @ 2018-08-02 21:51  My_Girlfriends  阅读(178)  评论(0编辑  收藏  举报