.Uva&LA部分题目代码

1.LA 5694 Adding New Machine

关键词:数据结构,线段树,扫描线(FIFO)

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #define pi acos(-1.)
 12 using namespace std;
 13 typedef long long ll;
 14 const int int_inf = 0x3f3f3f3f;
 15 const ll ll_inf = 1ll << 62;
 16 const int INT_INF = (int)((1ll << 31) - 1);
 17 const int mod = 1e6 + 7;
 18 const double double_inf = 1e30;
 19 typedef unsigned long long ul;
 20 #pragma comment(linker, "/STACK:102400000,102400000")
 21 #define max(a, b) ((a) > (b) ? (a) : (b))
 22 #define min(a, b) ((a) < (b) ? (a) : (b))
 23 #define mp make_pair
 24 #define st first
 25 #define nd second
 26 #define keyn (root->ch[1]->ch[0])
 27 #define lson (u << 1)
 28 #define rson (u << 1 | 1)
 29 #define pii pair<int, int>
 30 #define pll pair<ll, ll>
 31 #define pb push_back
 32 #define type(x) __typeof(x.begin())
 33 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 34 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 35 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 36 #define dbg(x) cout << x << endl
 37 #define dbg2(x, y) cout << x << " " << y << endl
 38 #define clr(x, i) memset(x, (i), sizeof(x))
 39 #define maximize(x, y) x = max((x), (y))
 40 #define minimize(x, y) x = min((x), (y))
 41 
 42 inline int readint(){
 43     bool neg = 0; char ch, t[11];
 44     int k = 0;
 45     while((ch = getchar()) == ' ' || ch == '
' || ch == 9) ;
 46     neg = ch == '-';
 47     ch == '-' ? neg = 1 : t[k++] = ch;
 48     while((ch = getchar()) >= '0' && ch <= '9') t[k++] = ch;
 49     //__ch = ch;
 50     int x = 0, y = 1;
 51     while(k) x += (t[--k] - '0') * y, y *= 10;
 52     return neg ? -x : x;
 53 }
 54 
 55 inline int readstr(char *s){
 56     char ch;
 57     int len = 0;
 58     while((ch = getchar()) == ' ' || ch == '
' || ch == 9) ;
 59     if(ch == EOF) return 0;
 60     *(s++) = ch, ++len;
 61     while((ch = getchar()) != ' ' && ch != '
' && ch != EOF) *(s++) = ch, ++len;
 62     *s = '';
 63     return len;
 64 }
 65 class cmpt{
 66 public:
 67     bool operator () (const int &x, const int &y) const{
 68         return x > y;
 69     }
 70 };
 71 int Rand(int x, int o){
 72     //if o set, return [1, x], else return [0, x - 1]
 73     if(!x) return 0;
 74     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 75     return o ? tem + 1 : tem;
 76 }
 77 
 78 void data_gen(){
 79     int n = 1e2 + 10;
 80     //freopen("in.txt", "w", stdout);
 81     printf("%d
", n);
 82     srand(time(0));
 83     FOR(i, 1, n){
 84         int x = Rand(1e5, 0), y = Rand(x, 0) + (x ? Rand(2, 0) : 0), z = Rand(y, 0) + (y ? Rand(2, 0) : 0);
 85         printf("%d %d %d
", x, y, z);
 86         //solve(x, y, z);
 87     }
 88 }
 89 
 90 struct cmpx{
 91     bool operator () (int x, int y) { return x > y; }
 92 };
 93 int debug = 0;
 94 int dx[] = {-1, 1, 0, 0};
 95 int dy[] = {0, 0, -1, 1};
 96 //-------------------------------------------------------------------------
 97 const int maxn = 5e4 + 10;
 98 const int maxm = maxn << 2;
 99 struct Seg{
100     int l, r, lg, rg;
101     int cnt, length, lazy;
102 }seg[maxm << 2];
103 pair<pii, pii> a[maxn];
104 int n, m, w, h;
105 int buf[maxm], k;
106 map<int, int> mapx, mapy;
107 vector<pii> info[maxm];
108 int anx[maxm], any[maxm];
109 
110 void build(int u, int l, int r, int o){
111     seg[u].l = l, seg[u].r = r;
112     seg[u].lazy = 0;
113     seg[u].length = o ? anx[r] - anx[l] : any[r] - any[l];
114     seg[u].lg = seg[u].rg = seg[u].length;
115     seg[u].cnt = max(0, seg[u].length - m + 1);
116     if(r - l < 2) return;
117     int mid = (l + r) >> 1;
118     build(lson, l, mid, o), build(rson, mid, r, o);
119 }
120 
121 void push_down(int u){
122     seg[lson].lazy += seg[u].lazy, seg[rson].lazy += seg[u].lazy;
123     if(seg[u].lazy < 0){
124         seg[lson].cnt = max(0, seg[lson].length - m + 1);
125         seg[rson].cnt = max(0, seg[rson].length - m + 1);
126         seg[lson].lg = seg[lson].rg = seg[lson].length;
127         seg[rson].lg = seg[rson].rg = seg[rson].length;
128     }else if(seg[u].lazy > 0){
129         seg[lson].cnt = seg[rson].cnt = 0;
130         seg[lson].lg = seg[lson].rg = 0;
131         seg[rson].lg = seg[rson].rg = 0;
132     }
133 }
134 
135 void push_up(int u){
136     int tem = seg[lson].cnt + seg[rson].cnt;
137     tem -= max(0, seg[lson].rg - m + 1);
138     tem -= max(0, seg[rson].lg - m + 1);
139     tem += max(0, seg[lson].rg + seg[rson].lg - m + 1);
140     seg[u].cnt = tem;
141     seg[u].lg = seg[lson].lg + (seg[lson].lg == seg[lson].length ? seg[rson].lg : 0);
142     seg[u].rg = seg[rson].rg + (seg[rson].rg == seg[rson].length ? seg[lson].rg : 0);
143 }
144 
145 void update(int u, int l, int r, int o){
146     if(seg[u].l == l && seg[u].r == r){
147         seg[u].lazy += o;
148         if(o < 0){
149             seg[u].cnt = max(0, seg[u].length - m + 1);
150             seg[u].lg = seg[u].rg = seg[u].length;
151         }else seg[u].cnt = seg[u].lg = seg[u].rg = 0;
152         return;
153     }
154     push_down(u);
155     int mid = (seg[u].l + seg[u].r) >> 1;
156     if(r <= mid) update(lson, l, r, o);
157     else if(l >= mid) update(rson, l, r, o);
158     else update(lson, l, mid, o), update(rson, mid, r, o);
159     push_up(u);
160 }
161 
162 //-------------------------------------------------------------------------
163 int main(){
164     //data_gen(); return 0;
165     //C(); return 0;
166     debug = 0;
167     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
168     if(debug) freopen("in.txt", "r", stdin);
169     //freopen("out.txt", "w", stdout);
170     while(~scanf("%d", &w)){
171         h = readint(), n = readint(), m = readint();
172         FOR(i, 0, n - 1){
173             int x1 = readint(), y1 = readint(), x2 = readint(), y2 = readint();
174             a[i] = mp(mp(x1, y1), mp(x2, y2));
175         }
176         k = 0;
177         FOR(i, 0, n - 1) buf[k++] = a[i].st.nd, buf[k++] = a[i].nd.nd + 1;
178         buf[k++] = 1, buf[k++] = h + 1;
179         sort(buf, buf + k);
180         k = unique(buf, buf + k) - buf;
181         mapy.clear();
182         FOR(i, 0, k - 1) mapy[buf[i]] = i + 1, any[i + 1] = buf[i];
183         build(1, 1, k, 0);
184         k = 0;
185         FOR(i, 0, n - 1) buf[k++] = a[i].st.st, buf[k++] = a[i].nd.st + 1;
186         buf[k++] = 1, buf[k++] = w + 1;
187         sort(buf, buf + k);
188         k = unique(buf, buf + k) - buf;
189         mapx.clear();
190         FOR(i, 0, k - 1) mapx[buf[i]] = i + 1, anx[i + 1] = buf[i];
191         FOR(i, 1, k) info[i].clear();
192         FOR(i, 0, n - 1) info[mapx[a[i].st.st]].pb(mp(1, i)), info[mapx[a[i].nd.st + 1]].pb(mp(-1, i));
193         ll ans = 0;
194         FOR(i, 1, k - 1){
195             int sz = info[i].size();
196             sort(info[i].begin(), info[i].end());
197             FOR(j, 0, sz - 1){
198                 int lhs = mapy[a[info[i][j].nd].st.nd], rhs = mapy[a[info[i][j].nd].nd.nd + 1];
199                 update(1, lhs, rhs, info[i][j].st);
200             }
201             ans += (ll)(anx[i + 1] - anx[i]) * seg[1].cnt;
202         }
203         build(1, 1, mapx[w + 1], 1);
204         k = mapy[h + 1];
205         FOR(i, 1, k) info[i].clear();
206         FOR(i, 0, n - 1) info[mapy[a[i].st.nd]].pb(mp(1, i)), info[mapy[a[i].nd.nd + 1]].pb(mp(-1, i));
207         FOR(i, 1, k - 1){
208             int sz = info[i].size();
209             sort(info[i].begin(), info[i].end());
210             FOR(j, 0, sz - 1){
211                 int lhs = mapx[a[info[i][j].nd].st.st], rhs = mapx[a[info[i][j].nd].nd.st + 1];
212                 update(1, lhs, rhs, info[i][j].st);
213             }
214             ans += (ll)(any[i + 1] - any[i]) * seg[1].cnt;
215         }
216         if(m == 1) ans >>= 1ll;
217         printf("%lld
", ans);
218     }
219     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
220     return 0;
221 }
code:

 2.LA 5698 Draw A Mess

关键词:数据结构,线段树,平时时间空间开销

(此题另有并查集解法)

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #define pi acos(-1.)
 12 using namespace std;
 13 typedef long long ll;
 14 const int int_inf = 0x3f3f3f3f;
 15 const ll ll_inf = 1ll << 62;
 16 const int INT_INF = (int)((1ll << 31) - 1);
 17 const int mod = 1e6 + 7;
 18 const double double_inf = 1e30;
 19 typedef unsigned long long ul;
 20 #pragma comment(linker, "/STACK:102400000,102400000")
 21 #define max(a, b) ((a) > (b) ? (a) : (b))
 22 #define min(a, b) ((a) < (b) ? (a) : (b))
 23 #define mp make_pair
 24 #define st first
 25 #define nd second
 26 #define keyn (root->ch[1]->ch[0])
 27 #define lson (u << 1)
 28 #define rson (u << 1 | 1)
 29 #define pii pair<int, int>
 30 #define pll pair<ll, ll>
 31 #define pb push_back
 32 #define type(x) __typeof(x.begin())
 33 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 34 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 35 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 36 #define dbg(x) cout << x << endl
 37 #define dbg2(x, y) cout << x << " " << y << endl
 38 #define clr(x, i) memset(x, (i), sizeof(x))
 39 #define maximize(x, y) x = max((x), (y))
 40 #define minimize(x, y) x = min((x), (y))
 41 
 42 inline int readint(){
 43     bool neg = 0; char ch, t[11];
 44     int k = 0;
 45     while((ch = getchar()) == ' ' || ch == '
' || ch == 9) ;
 46     neg = ch == '-';
 47     ch == '-' ? neg = 1 : t[k++] = ch;
 48     while((ch = getchar()) >= '0' && ch <= '9') t[k++] = ch;
 49     //__ch = ch;
 50     int x = 0, y = 1;
 51     while(k) x += (t[--k] - '0') * y, y *= 10;
 52     return neg ? -x : x;
 53 }
 54 
 55 inline int readstr(char *s){
 56     char ch;
 57     int len = 0;
 58     while((ch = getchar()) == ' ' || ch == '
' || ch == 9) ;
 59     if(ch == EOF) return 0;
 60     *(s++) = ch, ++len;
 61     while((ch = getchar()) != ' ' && ch != '
' && ch != EOF) *(s++) = ch, ++len;
 62     *s = '';
 63     return len;
 64 }
 65 class cmpt{
 66 public:
 67     bool operator () (const int &x, const int &y) const{
 68         return x > y;
 69     }
 70 };
 71 int Rand(int x, int o){
 72     //if o set, return [1, x], else return [0, x - 1]
 73     if(!x) return 0;
 74     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 75     return o ? tem + 1 : tem;
 76 }
 77 
 78 void data_gen(){
 79     int n = 1e2 + 10;
 80     //freopen("in.txt", "w", stdout);
 81     printf("%d
", n);
 82     srand(time(0));
 83     FOR(i, 1, n){
 84         int x = Rand(1e5, 0), y = Rand(x, 0) + (x ? Rand(2, 0) : 0), z = Rand(y, 0) + (y ? Rand(2, 0) : 0);
 85         printf("%d %d %d
", x, y, z);
 86         //solve(x, y, z);
 87     }
 88 }
 89 
 90 struct cmpx{
 91     bool operator () (int x, int y) { return x > y; }
 92 };
 93 int debug = 0;
 94 int dx[] = {-1, 1, 0, 0};
 95 int dy[] = {0, 0, -1, 1};
 96 //-------------------------------------------------------------------------
 97 struct Seg{
 98     int l, r;
 99     int cnt[10];
100     int lazy;
101 }seg[50003 << 2];
102 int n, m, q;
103 void build(int u, int l, int r){
104     seg[u].lazy = 0;
105     seg[u].l = l, seg[u].r = r;
106     clr(seg[u].cnt, 0);
107     if(r - l < 2) return;
108     int mid = (r + l) >> 1;
109     build(lson, l, mid), build(rson, mid, r);
110 }
111 
112 void push_up(int u){
113     FOR(i, 1, 9) seg[u].cnt[i] = seg[lson].cnt[i] + seg[rson].cnt[i];
114 }
115 
116 void push_down(int u){
117     if(seg[u].lazy == 0) return;
118     clr(seg[lson].cnt, 0), clr(seg[rson].cnt, 0);
119     seg[lson].cnt[seg[u].lazy] = seg[lson].r - seg[lson].l;
120     seg[rson].cnt[seg[u].lazy] = seg[rson].r - seg[rson].l;
121     seg[lson].lazy = seg[rson].lazy = seg[u].lazy;
122     seg[u].lazy = 0;
123 }
124 
125 void update(int u, int l, int r, int c){
126     if(seg[u].l == l && seg[u].r == r){
127         seg[u].lazy = c;
128         clr(seg[u].cnt, 0);
129         seg[u].cnt[c] = seg[u].r - seg[u].l;
130         return;
131     }
132     push_down(u);
133     int mid = (seg[u].l + seg[u].r) >> 1;
134     if(r <= mid) update(lson, l, r, c);
135     else if(l >= mid) update(rson, l, r, c);
136     else update(lson, l, mid, c), update(rson, mid, r, c);
137     push_up(u);
138 }
139 int op[50003][7];
140 //-------------------------------------------------------------------------
141 int main(){
142     //data_gen(); return 0;
143     //C(); return 0;
144     debug = 0;
145     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
146     if(debug) freopen("in.txt", "r", stdin);
147     //freopen("out.txt", "w", stdout);
148     while(~scanf("%d", &n)){
149         m = readint(), q = readint();
150         char cmd[15];
151         FOR(i, 1, q){
152             readstr(cmd);
153             if(cmd[0] == 'D') op[i][0] = 1;
154             else if(cmd[0] == 'T') op[i][0] = 2;
155             else if(cmd[0] == 'R') op[i][0] = 3;
156             else if(cmd[0] == 'C') op[i][0] = 4;
157             FOR(j, 1, 4) op[i][j] = readint();
158             if(cmd[0] == 'R') op[i][5] = readint();
159         }
160         int ans[10];
161         clr(ans, 0);
162         FOR(i, 0, n - 1){
163             build(1, 0, m);
164             FOR(j, 1, q){
165                 int x = op[j][1], y = op[j][2], z = op[j][3], u = op[j][4], v = op[j][5];
166                 if(op[j][0] == 1){
167                     int low = max(0, x - z), high = min(n - 1, x + z);
168                     if(i < low || i > high) continue;
169                     int len = z - abs(i - x);
170                     int l = max(0, y - len), r = min(m - 1, y + len);
171                     update(1, l, r + 1, u);
172                 }else if(op[j][0] == 2){
173                     int low = x, high = min(n - 1, x + (z - 1) / 2);
174                     if(i < low || i > high) continue;
175                     int len = (z - 1) / 2 - (i - x);
176                     int l = max(0, y - len), r = min(m - 1, y + len);
177                     update(1, l, r + 1, u);
178                 }else if(op[j][0] == 3){
179                     int low = x, high = min(n - 1, x + z - 1);
180                     if(i < low || i > high) continue;
181                     int l = y, r = min(m - 1, y + u - 1);
182                     update(1, l, r + 1, v);
183                 }else{
184                     int low = max(0, x - z), high = min(n - 1, x + z);
185                     if(i < low || i > high) continue;
186                     ll len2 = (ll)z * z - (ll)(abs(i - x)) * abs(i - x);
187                     ll len = (ll)sqrt(len2);
188                     ll l = max(0, y - len), r = min(m - 1, y + len);
189                     update(1, l, r + 1, u);
190                 }
191             }
192             FOR(i, 1, 9) ans[i] += seg[1].cnt[i];
193         }
194         printf("%d", ans[1]);
195         FOR(i, 2, 9) printf(" %d", ans[i]);
196         printf("
");
197     }
198     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
199     return 0;
200 }
code:

 3.LA 4013 A Sequence of Numbers

关键词:数据结构,线段树,记忆化处理,问题转换

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #define pi acos(-1.)
 12 using namespace std;
 13 typedef long long ll;
 14 const int int_inf = 0x3f3f3f3f;
 15 const ll ll_inf = 1ll << 62;
 16 const int INT_INF = (int)((1ll << 31) - 1);
 17 const int mod = 1e6 + 7;
 18 const double double_inf = 1e30;
 19 typedef unsigned long long ul;
 20 #pragma comment(linker, "/STACK:102400000,102400000")
 21 #define max(a, b) ((a) > (b) ? (a) : (b))
 22 #define min(a, b) ((a) < (b) ? (a) : (b))
 23 #define mp make_pair
 24 #define st first
 25 #define nd second
 26 #define keyn (root->ch[1]->ch[0])
 27 #define lson (u << 1)
 28 #define rson (u << 1 | 1)
 29 #define pii pair<int, int>
 30 #define pll pair<ll, ll>
 31 #define pb push_back
 32 #define type(x) __typeof(x.begin())
 33 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 34 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 35 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 36 #define dbg(x) cout << x << endl
 37 #define dbg2(x, y) cout << x << " " << y << endl
 38 #define clr(x, i) memset(x, (i), sizeof(x))
 39 #define maximize(x, y) x = max((x), (y))
 40 #define minimize(x, y) x = min((x), (y))
 41 
 42 inline int readint(){
 43     bool neg = 0; char ch, t[11];
 44     int k = 0;
 45     while((ch = getchar()) == ' ' || ch == '
' || ch == 9) ;
 46     neg = ch == '-';
 47     ch == '-' ? neg = 1 : t[k++] = ch;
 48     while((ch = getchar()) >= '0' && ch <= '9') t[k++] = ch;
 49     //__ch = ch;
 50     int x = 0, y = 1;
 51     while(k) x += (t[--k] - '0') * y, y *= 10;
 52     return neg ? -x : x;
 53 }
 54 
 55 inline int readstr(char *s){
 56     char ch;
 57     int len = 0;
 58     while((ch = getchar()) == ' ' || ch == '
' || ch == 9) ;
 59     if(ch == EOF) return 0;
 60     *(s++) = ch, ++len;
 61     while((ch = getchar()) != ' ' && ch != '
' && ch != EOF) *(s++) = ch, ++len;
 62     *s = '';
 63     return len;
 64 }
 65 class cmpt{
 66 public:
 67     bool operator () (const int &x, const int &y) const{
 68         return x > y;
 69     }
 70 };
 71 int Rand(int x, int o){
 72     //if o set, return [1, x], else return [0, x - 1]
 73     if(!x) return 0;
 74     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 75     return o ? tem + 1 : tem;
 76 }
 77 
 78 void data_gen(){
 79     int times = 20;
 80     freopen("in.txt", "w", stdout);
 81     while(times--){
 82     int n = 1e5;
 83     printf("%d
", n);
 84     srand(time(0));
 85     FOR(i, 1, n){
 86         int x = Rand(1 << 16, 0);
 87         printf("%d
", x);
 88         //solve(x, y, z);
 89     }
 90     int m = 1e5;
 91     FOR(i, 1, m){
 92         int op = Rand(2, 0);
 93         if(op){
 94             printf("Q %d
", Rand(16, 0));
 95         }else printf("C %d
", Rand(1 << 30, 0));
 96     }
 97     puts("E
");
 98     }
 99     puts("-1");
100 }
101 
102 struct cmpx{
103     bool operator () (int x, int y) { return x > y; }
104 };
105 int debug = 0;
106 int dx[] = {-1, 1, 0, 0};
107 int dy[] = {0, 0, -1, 1};
108 //------------------------------------------------------------------------
109 int a[1 << 16];
110 const int maxn = 2e5 + 10;
111 struct Seg{
112     int l, r;
113     int cnt;
114 }seg[(1 << 17) << 2];
115 int bg;//[bg, bg + (1 << 16))
116 
117 void push_up(int u){
118     seg[u].cnt = seg[lson].cnt + seg[rson].cnt;
119 }
120 
121 void build(int u, int l, int r){
122     seg[u].r = r, seg[u].l = l;
123     seg[u].cnt = 0;
124     if(r - l < 2){
125         if(l >= bg && l < bg + (1 << 16)) seg[u].cnt += a[l - bg];
126         return;
127     }
128     int mid = (l + r) >> 1;
129     build(lson, l, mid), build(rson, mid, r);
130     push_up(u);
131 }
132 
133 void insert(int u, int l, int r, int v){
134     if(seg[u].r == r && seg[u].l == l){
135         seg[u].cnt = v;
136         return;
137     }
138     int mid = (seg[u].l + seg[u].r) >> 1;
139     if(r <= mid) insert(lson, l, r, v);
140     else if(l >= mid) insert(rson, l, r, v);
141     push_up(u);
142 }
143 
144 int query(int u, int l, int r){
145     if(seg[u].l == l && r == seg[u].r) return seg[u].cnt;
146     int mid = (seg[u].l + seg[u].r) >> 1;
147     if(r <= mid) return query(lson, l, r);
148     else if(l >= mid) return query(rson, l, r);
149     else return query(lson, l, mid) + query(rson, mid, r);
150 }
151 
152 int _query(int l, int r){
153     l = (l + (1 << 16)) % (1 << 16), r = (r + (1 << 16)) % (1 << 16);
154     if(l <= r) return query(1, l, r + 1);
155     else return query(1, l, 1 << 16) + query(1, 0, r + 1);
156 }
157 
158 int ans[16][1 << 16];
159 int n;
160 int getAns(int i, int times){
161     if(ans[i][times] != -1) return ans[i][times];
162     int lim = (1 << (i + 1)) - 1;
163     int tans = 0;
164     int tem = 1 << i;
165     while(tem <= (1 << 16) - 1){
166         tans += _query(tem - times, tem + (1 << i) - times - 1);
167         tem += (1 << (i + 1));
168     }
169     return ans[i][times] = tans;
170 }
171 
172 //-------------------------------------------------------------------------
173 int main(){
174     //data_gen(); return 0;
175     //C(); return 0;
176     debug = 0;
177     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
178     if(debug) freopen("in.txt", "r", stdin);
179     //freopen("out.txt", "w", stdout);
180     int kase = 0;
181     while(~scanf("%d", &n) && ~n){
182         clr(a, 0);
183         FOR(i, 0, n - 1) ++a[readint()];
184         build(1, 0, 1 << 16);
185         int lim = (1 << 16) - 1;
186         clr(ans, -1);
187         char ch;
188         ll _ans = 0;
189         int shift = 0;
190         while(~scanf(" %c", &ch) && ch != 'E'){
191             int tem = readint();
192             tem %= 1 << 16;
193             if(ch == 'C') shift = (shift + tem) % (1 << 16);
194             else _ans += getAns(tem, shift % (1 << (tem + 1)));
195         }
196         printf("Case %d: %lld
", ++kase, _ans);
197     }
198     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
199     return 0;
200 }
code:

 4.Uva 11019 Matrix Matcher

关键词:数据结构,字符串,AC自动机

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 1ll << 62;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e6 + 7;
 19 const double double_inf = 1e30;
 20 typedef unsigned long long ul;
 21 #pragma comment(linker, "/STACK:102400000,102400000")
 22 #define max(a, b) ((a) > (b) ? (a) : (b))
 23 #define min(a, b) ((a) < (b) ? (a) : (b))
 24 #define mp make_pair
 25 #define st first
 26 #define nd second
 27 #define keyn (root->ch[1]->ch[0])
 28 #define lson (u << 1)
 29 #define rson (u << 1 | 1)
 30 #define pii pair<int, int>
 31 #define pll pair<ll, ll>
 32 #define pb push_back
 33 #define type(x) __typeof(x.begin())
 34 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 35 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 36 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 37 #define dbg(x) cout << x << endl
 38 #define dbg2(x, y) cout << x << " " << y << endl
 39 #define clr(x, i) memset(x, (i), sizeof(x))
 40 #define maximize(x, y) x = max((x), (y))
 41 #define minimize(x, y) x = min((x), (y))
 42 
 43 inline int readint(){
 44     int x;
 45     scanf("%d", &x);
 46     return x;
 47 }
 48 
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 
 54 class cmpt{
 55 public:
 56     bool operator () (const int &x, const int &y) const{
 57         return x > y;
 58     }
 59 };
 60 
 61 int Rand(int x, int o){
 62     //if o set, return [1, x], else return [0, x - 1]
 63     if(!x) return 0;
 64     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 65     return o ? tem + 1 : tem;
 66 }
 67 
 68 void data_gen(){
 69     srand(time(0));
 70     freopen("in.txt", "w", stdout);
 71     int times = 10;
 72     printf("%d
", times);
 73     while(times--){
 74         int n = Rand(500, 1), m = Rand(500, 1);
 75         printf("%d %d
", n, m);
 76         FOR(i, 1, n){
 77             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 78             putchar('
');
 79         }
 80         n = Rand(min(10, n), 1), m = Rand(min(10, m), 1);
 81         printf("%d %d
", n, m);
 82         FOR(i, 1, n){
 83             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 84             putchar('
');
 85         }
 86     }
 87 }
 88 
 89 struct cmpx{
 90     bool operator () (int x, int y) { return x > y; }
 91 };
 92 int debug = 0;
 93 int dx[] = {-1, 1, 0, 0};
 94 int dy[] = {0, 0, -1, 1};
 95 //-------------------------------------------------------------------------
 96 const int maxn = 1e3 + 10;
 97 char mt_t[maxn][maxn];
 98 char mt_p[105][105];
 99 int mt_t_n[maxn][maxn];
100 int mt_p_n[105];
101 const int sigma_size = 26;
102 int n, m, X, Y;
103 int ch[105 * 105][sigma_size];
104 int idx(char c) { return c - 'a'; }
105 int id[105 * 105];
106 struct Trie{
107     int sz, cnt;
108     void init() { clr(ch[0], 0); cnt = sz = 0; clr(id, 0); }
109     int insert(char *src){
110         int u = 0;
111         while(*src){
112             int v = ch[u][idx(*src)];
113             if(!v) { v = ch[u][idx(*src)] = ++sz; clr(ch[sz], 0); }
114             u = v;
115             src++;
116         }
117         if(!id[u]) id[u] = ++cnt;
118         return id[u];
119     }
120 }trie;
121 int fail[105 * 105];
122 queue<int> q;
123 //-------------------------------------------------------------------------
124 int main(){
125     //data_gen(); return 0;
126     //C(); return 0;
127     debug = 0;
128     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
129     if(debug) freopen("_in.txt", "r", stdin);
130     //freopen("out.txt", "w", stdout);
131     int kases = readint();
132     while(kases--){
133         n = readint(), m = readint();
134         trie.init();
135         FOR(i, 0, n - 1) scanf("%s", mt_t[i]);
136         X = readint(), Y = readint();
137         FOR(i, 0, X - 1) scanf("%s", mt_p[i]);
138         FOR(i, 0, X - 1) mt_p_n[i] = trie.insert(mt_p[i]);
139         while(!q.empty()) q.pop();
140         fail[0] = 0;
141         FOR(i, 0, sigma_size - 1) if(ch[0][i]) q.push(ch[0][i]), fail[ch[0][i]] = 0;
142         while(!q.empty()){
143             int u = q.front(); q.pop();
144             FOR(i, 0, sigma_size - 1){
145                 int v = ch[u][i];
146                 if(!v) continue;
147                 int j = fail[u];
148                 while(j && !ch[j][i]) j = fail[j];
149                 fail[v] = ch[j][i];
150                 q.push(v);
151             }
152         }
153         FOR(i, 0, n - 1){
154             int pointer = 0;
155             FOR(j, 0, m - 1){
156                 int c = idx(mt_t[i][j]);
157                 if(ch[pointer][c]) pointer = ch[pointer][c];
158                 else{
159                     pointer = fail[pointer];
160                     while(pointer && !ch[pointer][c]) pointer = fail[pointer];
161                     pointer = ch[pointer][c];
162                 }
163                 mt_t_n[i][j] = j >= Y - 1 ? id[pointer] : 0;
164             }
165         }
166         fail[0] = -1;
167         FOR(i, 1, X - 1){
168             int j = fail[i - 1];
169             while(j != -1 && mt_p_n[j + 1] != mt_p_n[i]) j = fail[j];
170             fail[i] = mt_p_n[j + 1] == mt_p_n[i] ? j + 1 : -1;
171         }
172         int ans = 0;
173         FOR(i, 0, m - 1){
174             int pointer = -1;
175             FOR(j, 0, n - 1){
176                 if(pointer < X - 1 && mt_p_n[pointer + 1] == mt_t_n[j][i]) ++pointer;
177                 else if(pointer != -1){
178                     pointer = fail[pointer];
179                     while(pointer != -1 && mt_p_n[pointer + 1] != mt_t_n[j][i]) pointer = fail[pointer];
180                     pointer = mt_p_n[pointer + 1] == mt_t_n[j][i] ? 1 + pointer : -1;
181                 }
182                 ans += pointer == X - 1;
183             }
184         }
185         printf("%d
", ans);
186     }
187     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
188     return 0;
189 }
code:

 5.LA 5913 Dictionary Size

关键词:树结构,字符串,trie,前缀后缀

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 1ll << 62;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e6 + 7;
 19 const double double_inf = 1e30;
 20 typedef unsigned long long ul;
 21 #pragma comment(linker, "/STACK:102400000,102400000")
 22 #define max(a, b) ((a) > (b) ? (a) : (b))
 23 #define min(a, b) ((a) < (b) ? (a) : (b))
 24 #define mp make_pair
 25 #define st first
 26 #define nd second
 27 #define keyn (root->ch[1]->ch[0])
 28 #define lson (u << 1)
 29 #define rson (u << 1 | 1)
 30 #define pii pair<int, int>
 31 #define pll pair<ll, ll>
 32 #define pb push_back
 33 #define type(x) __typeof(x.begin())
 34 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 35 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 36 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 37 #define dbg(x) cout << x << endl
 38 #define dbg2(x, y) cout << x << " " << y << endl
 39 #define clr(x, i) memset(x, (i), sizeof(x))
 40 #define maximize(x, y) x = max((x), (y))
 41 #define minimize(x, y) x = min((x), (y))
 42 
 43 inline int readint(){
 44     int x;
 45     scanf("%d", &x);
 46     return x;
 47 }
 48 
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 
 54 class cmpt{
 55 public:
 56     bool operator () (const int &x, const int &y) const{
 57         return x > y;
 58     }
 59 };
 60 
 61 int Rand(int x, int o){
 62     //if o set, return [1, x], else return [0, x - 1]
 63     if(!x) return 0;
 64     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 65     return o ? tem + 1 : tem;
 66 }
 67 
 68 void data_gen(){
 69     srand(time(0));
 70     freopen("in.txt", "w", stdout);
 71     int times = 10;
 72     printf("%d
", times);
 73     while(times--){
 74         int n = Rand(500, 1), m = Rand(500, 1);
 75         printf("%d %d
", n, m);
 76         FOR(i, 1, n){
 77             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 78             putchar('
');
 79         }
 80         n = Rand(min(10, n), 1), m = Rand(min(10, m), 1);
 81         printf("%d %d
", n, m);
 82         FOR(i, 1, n){
 83             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 84             putchar('
');
 85         }
 86     }
 87 }
 88 
 89 struct cmpx{
 90     bool operator () (int x, int y) { return x > y; }
 91 };
 92 int debug = 0;
 93 int dx[] = {-1, 1, 0, 0};
 94 int dy[] = {0, 0, -1, 1};
 95 //-------------------------------------------------------------------------
 96 const int maxn = 1e4 + 10;
 97 const int sigma_size = 26;
 98 int cnt[26], valid[26], cnt_all;
 99 struct Trie{
100     int ch[maxn * 45][sigma_size];
101     int w[maxn * 45];
102     int sz;
103     void init() { clr(ch[0], 0); sz = 0; clr(w, 0); }
104     int idx(int c) { return c - 'a'; }
105     void insert(char *s, int o){
106         int u = 0;
107         while(*s){
108             int v = ch[u][idx(*s)];
109             if(!v) { v = ch[u][idx(*s)] = ++sz; clr(ch[sz], 0); }
110             u = v;
111             s += o;
112         }
113         w[u] = 1;
114     }
115     void dfs_2(int u){
116         ++cnt_all;
117         FOR(i, 0, sigma_size - 1) if(!ch[u][i]) ++cnt[i];
118         FOR(i, 0, sigma_size - 1) valid[i] = ch[0][i] > 0;
119         FOR(i, 0, sigma_size - 1) if(ch[u][i]) dfs_2(ch[u][i]);
120     }
121     void cal_2(){
122         clr(cnt, 0);
123         cnt_all = 0;
124         FOR(i, 0, sigma_size - 1) if(ch[0][i]) dfs_2(ch[0][i]);
125     }
126     ll dfs_1(int fa, int u, int o){
127         ll tem = !fa ? cnt_all : cnt[o];
128         if(w[u] && !(fa && valid[o])) ++tem;
129         FOR(i, 0, sigma_size - 1) if(ch[u][i]) tem += dfs_1(u, ch[u][i], i);
130         return tem;
131     }
132     ll cal_1(){
133         ll ans = 0;
134         FOR(i, 0, sigma_size - 1) if(ch[0][i]) ans += dfs_1(0, ch[0][i], i);
135         return ans;
136     }
137 }trie1, trie2;
138 char mt[maxn][45];
139 int len[maxn];
140 //-------------------------------------------------------------------------
141 int main(){
142     //data_gen(); return 0;
143     //C(); return 0;
144     debug = 0;
145     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
146     if(debug) freopen("in.txt", "r", stdin);
147     //freopen("out.txt", "w", stdout);
148     int n;
149     while(~scanf("%d", &n)){
150         FOR(i, 0, n - 1) len[i] = readstr(mt[i] + 1), mt[i][0] = '';
151         trie1.init(), trie2.init();
152         FOR(i, 0, n - 1) trie1.insert(mt[i] + 1, 1), trie2.insert(mt[i] + len[i], -1);
153         trie2.cal_2();
154         ll ans = trie1.cal_1();
155         printf("%lld
", ans);
156     }
157     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
158     return 0;
159 }
code:

 6.LA 4108 Skyline

关键词:线段树,注意数据范围

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 1ll << 62;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e6 + 7;
 19 const double double_inf = 1e30;
 20 typedef unsigned long long ul;
 21 #pragma comment(linker, "/STACK:102400000,102400000")
 22 #define max(a, b) ((a) > (b) ? (a) : (b))
 23 #define min(a, b) ((a) < (b) ? (a) : (b))
 24 #define mp make_pair
 25 #define st first
 26 #define nd second
 27 #define keyn (root->ch[1]->ch[0])
 28 #define lson (u << 1)
 29 #define rson (u << 1 | 1)
 30 #define pii pair<int, int>
 31 #define pll pair<ll, ll>
 32 #define pb push_back
 33 #define type(x) __typeof(x.begin())
 34 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 35 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 36 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 37 #define dbg(x) cout << x << endl
 38 #define dbg2(x, y) cout << x << " " << y << endl
 39 #define clr(x, i) memset(x, (i), sizeof(x))
 40 #define maximize(x, y) x = max((x), (y))
 41 #define minimize(x, y) x = min((x), (y))
 42 
 43 inline int readint(){
 44     int x;
 45     scanf("%d", &x);
 46     return x;
 47 }
 48 
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 
 54 class cmpt{
 55 public:
 56     bool operator () (const int &x, const int &y) const{
 57         return x > y;
 58     }
 59 };
 60 
 61 int Rand(int x, int o){
 62     //if o set, return [1, x], else return [0, x - 1]
 63     if(!x) return 0;
 64     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 65     return o ? tem + 1 : tem;
 66 }
 67 
 68 void data_gen(){
 69     srand(time(0));
 70     freopen("in.txt", "w", stdout);
 71     int times = 10;
 72     printf("%d
", times);
 73     while(times--){
 74         int n = Rand(500, 1), m = Rand(500, 1);
 75         printf("%d %d
", n, m);
 76         FOR(i, 1, n){
 77             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 78             putchar('
');
 79         }
 80         n = Rand(min(10, n), 1), m = Rand(min(10, m), 1);
 81         printf("%d %d
", n, m);
 82         FOR(i, 1, n){
 83             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 84             putchar('
');
 85         }
 86     }
 87 }
 88 
 89 struct cmpx{
 90     bool operator () (int x, int y) { return x > y; }
 91 };
 92 int debug = 0;
 93 int dx[] = {-1, 1, 0, 0};
 94 int dy[] = {0, 0, -1, 1};
 95 //-------------------------------------------------------------------------
 96 const int maxn = 1e5 + 10;
 97 struct Seg{
 98     int l, r, maxi, mini;
 99     int lazy;
100 }seg[maxn << 2];
101 void build(int u, int l, int r){
102     seg[u].l = l, seg[u].r = r;
103     seg[u].maxi = seg[u].mini = seg[u].lazy = 0;
104     if(r - l < 2) return;
105     int mid = (l + r) >> 1;
106     build(lson, l, mid), build(rson, mid, r);
107 }
108 
109 void push_up(int u){
110     seg[u].mini = min(seg[lson].mini, seg[rson].mini);
111     seg[u].maxi = max(seg[lson].maxi, seg[rson].maxi);
112 }
113 
114 void push_down(int u){
115     if(!seg[u].lazy) return;
116     seg[lson].maxi = seg[lson].mini = seg[u].lazy;
117     seg[rson].maxi = seg[rson].mini = seg[u].lazy;
118     seg[lson].lazy = seg[rson].lazy = seg[u].lazy;
119     seg[u].lazy = 0;
120 }
121 
122 int update(int u, int l, int r, int value){
123     if(seg[u].l == l && seg[u].r == r){
124         if(seg[u].mini > value) return 0;
125         if(seg[u].maxi <= value){
126             seg[u].maxi = seg[u].mini = value;
127             seg[u].lazy = value;
128             return seg[u].r - seg[u].l;
129         }
130         int mid = (l + r) >> 1;
131         return update(lson, l, mid, value) + update(rson, mid, r, value);
132     }
133     push_down(u);
134     int mid = (seg[u].l + seg[u].r) >> 1;
135     int tem = 0;
136     if(r <= mid) tem = update(lson, l, r, value);
137     else if(l >= mid) tem = update(rson, l, r, value);
138     else tem = update(lson, l, mid, value) + update(rson, mid, r, value);
139     push_up(u);
140     return tem;
141 }
142 
143 int n;
144 //-------------------------------------------------------------------------
145 int main(){
146     //data_gen(); return 0;
147     //C(); return 0;
148     debug = 0;
149     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
150     if(debug) freopen("in.txt", "r", stdin);
151     //freopen("out.txt", "w", stdout);
152     int T = readint();
153     while(T--){
154         n = readint();
155         build(1, 1, maxn - 2);
156         int ans = 0;
157         FOR(i, 1, n){
158             int x = readint(), y = readint(), z = readint();
159             ans += update(1, x, y, z);
160         }
161         printf("%d
", ans);
162     }
163     T = readint();
164     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
165     return 0;
166 }
code:

 7.LA 4119 Always an integer

关键词:递推,模拟,字符处理

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 1ll << 62;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e9 + 7;
 19 const double double_inf = 1e30;
 20 typedef unsigned long long ul;
 21 #pragma comment(linker, "/STACK:102400000,102400000")
 22 #define max(a, b) ((a) > (b) ? (a) : (b))
 23 #define min(a, b) ((a) < (b) ? (a) : (b))
 24 #define mp make_pair
 25 #define st first
 26 #define nd second
 27 #define keyn (root->ch[1]->ch[0])
 28 #define lson (u << 1)
 29 #define rson (u << 1 | 1)
 30 #define pii pair<int, int>
 31 #define pll pair<ll, ll>
 32 #define pb push_back
 33 #define type(x) __typeof(x.begin())
 34 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 35 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 36 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 37 #define dbg(x) cout << x << endl
 38 #define dbg2(x, y) cout << x << " " << y << endl
 39 #define clr(x, i) memset(x, (i), sizeof(x))
 40 #define maximize(x, y) x = max((x), (y))
 41 #define minimize(x, y) x = min((x), (y))
 42 #define low_bit(x) ((x) & (-x))
 43 
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 
 50 inline int readstr(char *s){
 51     scanf("%s", s);
 52     return strlen(s);
 53 }
 54 
 55 class cmpt{
 56 public:
 57     bool operator () (const int &x, const int &y) const{
 58         return x > y;
 59     }
 60 };
 61 
 62 int Rand(int x, int o){
 63     //if o set, return [1, x], else return [0, x - 1]
 64     if(!x) return 0;
 65     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 66     return o ? tem + 1 : tem;
 67 }
 68 
 69 void data_gen(){
 70     srand(time(0));
 71     freopen("in.txt", "w", stdout);
 72     int times = 10;
 73     printf("%d
", times);
 74     while(times--){
 75         int n = Rand(500, 1), m = Rand(500, 1);
 76         printf("%d %d
", n, m);
 77         FOR(i, 1, n){
 78             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 79             putchar('
');
 80         }
 81         n = Rand(min(10, n), 1), m = Rand(min(10, m), 1);
 82         printf("%d %d
", n, m);
 83         FOR(i, 1, n){
 84             FOR(j, 1, m) printf("%c", Rand(2, 0) + 'a');
 85             putchar('
');
 86         }
 87     }
 88 }
 89 
 90 struct cmpx{
 91     bool operator () (int x, int y) { return x > y; }
 92 };
 93 int debug = 1;
 94 int dx[] = {-1, 1, 0, 0};
 95 int dy[] = {0, 0, -1, 1};
 96 //-------------------------------------------------------------------------
 97 const int maxn = 1e2 + 10;
 98 ll v[2][maxn];
 99 ll M;
100 char s[maxn * maxn];
101 char* pointer;
102 ll C[maxn][maxn];
103 bool isNumber(char c) { return c >= '0' && c <= '9'; }
104 ll anayInt(ll dft){
105     int op = 1;
106     if(*pointer == '+') ++pointer;
107     if(*pointer == '-') op = -1, ++pointer;
108     if(!isNumber(*pointer)) return op * dft;
109     char tem[20];
110     int k = 0;
111     while(*pointer <= '9' && *pointer >= '0') tem[k++] = *pointer, ++pointer;
112     ll ans = 0, base = 1;
113     while(k--){
114         ans += base * (tem[k] - '0');
115         base *= 10;
116     }
117     return ans * op;
118 }
119 
120 void anayTerm(){
121     ll C = anayInt(1);
122     if(*pointer != 'n'){
123         v[0][0] += (C % M + M) % M;
124         return;
125     }
126     ++pointer;
127     if(*pointer != '^'){
128         v[0][1] += (C % M + M) % M;
129         return;
130     }
131     ++pointer;
132     ll E = anayInt(1);
133     v[0][E] += (C % M + M) % M;
134 }
135 
136 void anaystr(){
137     int p = 0;
138     int tem = strlen(s);
139     FOR(i, 0, tem - 1) if(s[i] != ' ') s[p++] = s[i];
140     s[p] = '';
141     pointer = s + 1;
142     while(*pointer != '/') ++pointer;
143     ++pointer;
144     M = anayInt(1);
145     pointer = s + 1;
146     do{
147         anayTerm();
148     }while(*pointer != ')');
149 }
150 
151 bool check(int o){
152     int p = 100;
153     while(p && v[o][p] == 0) --p;
154     return !p;
155 }
156 
157 ll getSum(int o){
158     ll ans = 0;
159     FOR(i, 0, 100) ans = (ans + v[o][i]) % M;
160     return ans;
161 }
162 
163 void init(){
164     FOR(i, 1, 100) C[i][i] = C[i][0] = 1 % M;
165     FOR(i, 2, 100){
166         FOR(j, 1, i - 1) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % M;
167     }
168 }
169 
170 int getAns(){
171     int o = 0;
172     while(!check(o)){
173         ll tem = getSum(o);
174         if(tem % M) return 0;
175         int no = o ^ 1;
176         clr(v[no], 0);
177         FOR(i, 1, 100){
178             FOR(j, 0, i - 1) v[no][j] = (v[no][j] + v[o][i] * C[i][j] % M) % M;
179         }
180         o = no;
181     }
182     return !v[o][0];
183 }
184 
185 //-------------------------------------------------------------------------
186 int main(){
187     //data_gen(); return 0;
188     //C(); return 0;
189     debug = 0;
190     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
191     if(debug) freopen("in.txt", "r", stdin);
192     //freopen("out.txt", "w", stdout);
193     int kase = 0;
194     while(gets(s) && s[0] != '.'){
195         clr(v, 0);
196         anaystr();
197         init();
198         int ok = getAns();
199         printf("Case %d: %s
", ++kase, ok ? "Always an integer" : "Not always an integer");
200     }
201     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
202     return 0;
203 }
code:

 8.LA 5059 Play with Stones

关键词:组合游戏,找规律,SG函数

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e9 + 9;
 19 const double double_inf = 1e30;
 20 typedef unsigned long long ul;
 21 #pragma comment(linker, "/STACK:102400000,102400000")
 22 #define max(a, b) ((a) > (b) ? (a) : (b))
 23 #define min(a, b) ((a) < (b) ? (a) : (b))
 24 #define mp make_pair
 25 #define st first
 26 #define nd second
 27 #define keyn (root->ch[1]->ch[0])
 28 #define lson (u << 1)
 29 #define rson (u << 1 | 1)
 30 #define pii pair<int, int>
 31 #define pll pair<ll, ll>
 32 #define pb push_back
 33 #define type(x) __typeof(x.begin())
 34 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 35 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 36 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 37 #define dbg(x) cout << x << endl
 38 #define dbg2(x, y) cout << x << " " << y << endl
 39 #define clr(x, i) memset(x, (i), sizeof(x))
 40 #define maximize(x, y) x = max((x), (y))
 41 #define minimize(x, y) x = min((x), (y))
 42 #define low_bit(x) ((x) & (-x))
 43 
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 
 50 inline int readstr(char *s){
 51     scanf("%s", s);
 52     return strlen(s);
 53 }
 54 
 55 class cmpt{
 56 public:
 57     bool operator () (const int &x, const int &y) const{
 58         return x > y;
 59     }
 60 };
 61 
 62 int Rand(int x, int o){
 63     //if o set, return [1, x], else return [0, x - 1]
 64     if(!x) return 0;
 65     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 66     return o ? tem + 1 : tem;
 67 }
 68 
 69 void data_gen(){
 70     srand(time(0));
 71     freopen("in.txt", "w", stdout);
 72     int times = 100;
 73     printf("%d
", times);
 74     while(times--){
 75         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
 76         int b = Rand(r, 1), d = Rand(r, 1);
 77         int m = Rand(100, 1), n = Rand(m, 1);
 78         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
 79     }
 80 }
 81 
 82 struct cmpx{
 83     bool operator () (int x, int y) { return x > y; }
 84 };
 85 int debug = 1;
 86 int dx[] = {-1, 1, 0, 0};
 87 int dy[] = {0, 0, -1, 1};
 88 //-------------------------------------------------------------------------
 89 int n;
 90 ll a[110];
 91 bool solve(){
 92     ll tem = 0;
 93     FOR(i, 1, n){
 94         ll sg = 1;
 95         do{
 96             ll t = 1ll << sg;
 97             ll res = a[i] % t;
 98             if(res == (t >> 1) - 1) break;
 99             ++sg;
100         }while(1);
101         tem ^= a[i] / (1ll << sg);
102     }
103     return tem == 0;
104 }
105 //-------------------------------------------------------------------------
106 int main(){
107     //data_gen(); return 0;
108     //C(); return 0;
109     debug = 0;
110     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
111     if(debug) freopen("in.txt", "r", stdin);
112     //freopen("out.txt", "w", stdout);
113     //init(); return 0;
114     int T = readint();
115     while(T--){
116         n = readint();
117         FOR(i, 1, n) scanf("%lld", &a[i]);
118         bool ans = solve();
119         puts(ans ? "NO" : "YES");
120     }
121     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
122     return 0;
123 }
code:

 9.Uva 11021 Tribles

关键词:概率期望,递推

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e9 + 9;
 19 const double double_inf = 1e30;
 20 typedef unsigned long long ul;
 21 #pragma comment(linker, "/STACK:102400000,102400000")
 22 #define max(a, b) ((a) > (b) ? (a) : (b))
 23 #define min(a, b) ((a) < (b) ? (a) : (b))
 24 #define mp make_pair
 25 #define st first
 26 #define nd second
 27 #define keyn (root->ch[1]->ch[0])
 28 #define lson (u << 1)
 29 #define rson (u << 1 | 1)
 30 #define pii pair<int, int>
 31 #define pll pair<ll, ll>
 32 #define pb push_back
 33 #define type(x) __typeof(x.begin())
 34 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 35 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 36 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 37 #define dbg(x) cout << x << endl
 38 #define dbg2(x, y) cout << x << " " << y << endl
 39 #define clr(x, i) memset(x, (i), sizeof(x))
 40 #define maximize(x, y) x = max((x), (y))
 41 #define minimize(x, y) x = min((x), (y))
 42 #define low_bit(x) ((x) & (-x))
 43 
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 
 50 inline int readstr(char *s){
 51     scanf("%s", s);
 52     return strlen(s);
 53 }
 54 
 55 class cmpt{
 56 public:
 57     bool operator () (const int &x, const int &y) const{
 58         return x > y;
 59     }
 60 };
 61 
 62 int Rand(int x, int o){
 63     //if o set, return [1, x], else return [0, x - 1]
 64     if(!x) return 0;
 65     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 66     return o ? tem + 1 : tem;
 67 }
 68 
 69 void data_gen(){
 70     srand(time(0));
 71     freopen("in.txt", "w", stdout);
 72     int times = 100;
 73     printf("%d
", times);
 74     while(times--){
 75         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
 76         int b = Rand(r, 1), d = Rand(r, 1);
 77         int m = Rand(100, 1), n = Rand(m, 1);
 78         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
 79     }
 80 }
 81 
 82 struct cmpx{
 83     bool operator () (int x, int y) { return x > y; }
 84 };
 85 int debug = 1;
 86 int dx[] = {-1, 1, 0, 0};
 87 int dy[] = {0, 0, -1, 1};
 88 //-------------------------------------------------------------------------
 89 const int maxn = 1e3 + 10;
 90 double p[maxn], P[maxn];
 91 int n, m, k;
 92 double power(double x, int p){
 93     double ans = 1;
 94     while(p){
 95         if(p & 1) ans *= x;
 96         p >>= 1;
 97         x = x * x;
 98     }
 99     return ans;
100 }
101 double getAns(){
102     p[0] = 1;
103     FOR(i, 1, m){
104         p[i] = 0;
105         FOR(j, 0, n - 1) p[i] += P[j] * (1. - power((1 - p[i - 1]), j));
106     }
107     return power((1. - p[m]), k);
108 }
109 //-------------------------------------------------------------------------
110 int main(){
111     //data_gen(); return 0;
112     //C(); return 0;
113     debug = 0;
114     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
115     if(debug) freopen("in.txt", "r", stdin);
116     //freopen("out.txt", "w", stdout);
117     int T = readint(), kase = 0;
118     while(~scanf("%d%d%d", &n, &k, &m)){
119         FOR(i, 0, n - 1) scanf("%lf", &P[i]);
120         double ans = getAns();
121         printf("Case #%d: %.7f
", ++kase, ans);
122     }
123     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
124     return 0;
125 }
code:

 10.Uva10341 Solve It!

关键词:数值方法,高精度

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e9 + 9;
 19 const double double_inf = 1e30;
 20 const double eps = 1e-14;
 21 typedef unsigned long long ul;
 22 #pragma comment(linker, "/STACK:102400000,102400000")
 23 #define max(a, b) ((a) > (b) ? (a) : (b))
 24 #define min(a, b) ((a) < (b) ? (a) : (b))
 25 #define mp make_pair
 26 #define st first
 27 #define nd second
 28 #define keyn (root->ch[1]->ch[0])
 29 #define lson (u << 1)
 30 #define rson (u << 1 | 1)
 31 #define pii pair<int, int>
 32 #define pll pair<ll, ll>
 33 #define pb push_back
 34 #define type(x) __typeof(x.begin())
 35 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 36 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 37 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 38 #define dbg(x) cout << x << endl
 39 #define dbg2(x, y) cout << x << " " << y << endl
 40 #define clr(x, i) memset(x, (i), sizeof(x))
 41 #define maximize(x, y) x = max((x), (y))
 42 #define minimize(x, y) x = min((x), (y))
 43 #define low_bit(x) ((x) & (-x))
 44 
 45 inline int readint(){
 46     int x;
 47     scanf("%d", &x);
 48     return x;
 49 }
 50 
 51 inline int readstr(char *s){
 52     scanf("%s", s);
 53     return strlen(s);
 54 }
 55 
 56 class cmpt{
 57 public:
 58     bool operator () (const int &x, const int &y) const{
 59         return x > y;
 60     }
 61 };
 62 
 63 int Rand(int x, int o){
 64     //if o set, return [1, x], else return [0, x - 1]
 65     if(!x) return 0;
 66     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 67     return o ? tem + 1 : tem;
 68 }
 69 
 70 void data_gen(){
 71     srand(time(0));
 72     freopen("in.txt", "w", stdout);
 73     int times = 100;
 74     printf("%d
", times);
 75     while(times--){
 76         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
 77         int b = Rand(r, 1), d = Rand(r, 1);
 78         int m = Rand(100, 1), n = Rand(m, 1);
 79         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
 80     }
 81 }
 82 
 83 struct cmpx{
 84     bool operator () (int x, int y) { return x > y; }
 85 };
 86 int debug = 1;
 87 int dx[] = {-1, 1, 0, 0};
 88 int dy[] = {0, 0, -1, 1};
 89 //-------------------------------------------------------------------------
 90 double p, q, r, s, t, u;
 91 double f(double x){
 92     return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u;
 93 }
 94 //-------------------------------------------------------------------------
 95 int main(){
 96     //data_gen(); return 0;
 97     //C(); return 0;
 98     debug = 0;
 99     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
100     if(debug) freopen("in.txt", "r", stdin);
101     //freopen("out.txt", "w", stdout);
102     while(~scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u)){
103         double high = f(0), low = f(1);
104         if(low > eps || high < -eps){
105             //dbg2(low, high);
106             puts("No solution");
107         }else{
108             double l = 0, r = 1, mid;
109             while(r - l > eps){
110                 mid = l + (r - l) / 2;
111                 double tem = f(mid);
112                 if(tem < 0) r = mid;
113                 else l = mid;
114             }
115             printf("%.4f
", mid);
116         }
117     }
118     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
119     return 0;
120 }
code:

 11.LA 5009 Error Curves

关键词:数值方法,二分枚举

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define pi acos(-1.)
 13 using namespace std;
 14 typedef long long ll;
 15 const int int_inf = 0x3f3f3f3f;
 16 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 17 const int INT_INF = (int)((1ll << 31) - 1);
 18 const int mod = 1e9 + 9;
 19 const double double_inf = 1e30;
 20 const double eps = 1e-14;
 21 typedef unsigned long long ul;
 22 #pragma comment(linker, "/STACK:102400000,102400000")
 23 #define max(a, b) ((a) > (b) ? (a) : (b))
 24 #define min(a, b) ((a) < (b) ? (a) : (b))
 25 #define mp make_pair
 26 #define st first
 27 #define nd second
 28 #define keyn (root->ch[1]->ch[0])
 29 #define lson (u << 1)
 30 #define rson (u << 1 | 1)
 31 #define pii pair<int, int>
 32 #define pll pair<ll, ll>
 33 #define pb push_back
 34 #define type(x) __typeof(x.begin())
 35 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 36 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 37 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 38 #define dbg(x) cout << x << endl
 39 #define dbg2(x, y) cout << x << " " << y << endl
 40 #define clr(x, i) memset(x, (i), sizeof(x))
 41 #define maximize(x, y) x = max((x), (y))
 42 #define minimize(x, y) x = min((x), (y))
 43 #define low_bit(x) ((x) & (-x))
 44 
 45 inline int readint(){
 46     int x;
 47     scanf("%d", &x);
 48     return x;
 49 }
 50 
 51 inline int readstr(char *s){
 52     scanf("%s", s);
 53     return strlen(s);
 54 }
 55 
 56 class cmpt{
 57 public:
 58     bool operator () (const int &x, const int &y) const{
 59         return x > y;
 60     }
 61 };
 62 
 63 int Rand(int x, int o){
 64     //if o set, return [1, x], else return [0, x - 1]
 65     if(!x) return 0;
 66     int tem = (int)((double)rand() / RAND_MAX * x) % x;
 67     return o ? tem + 1 : tem;
 68 }
 69 
 70 void data_gen(){
 71     srand(time(0));
 72     freopen("in.txt", "w", stdout);
 73     int times = 100;
 74     printf("%d
", times);
 75     while(times--){
 76         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
 77         int b = Rand(r, 1), d = Rand(r, 1);
 78         int m = Rand(100, 1), n = Rand(m, 1);
 79         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
 80     }
 81 }
 82 
 83 struct cmpx{
 84     bool operator () (int x, int y) { return x > y; }
 85 };
 86 int debug = 1;
 87 int dx[] = {-1, 1, 0, 0};
 88 int dy[] = {0, 0, -1, 1};
 89 //-------------------------------------------------------------------------
 90 int n;
 91 const int maxn = 1e4 + 10;
 92 pair<pii, int> a[maxn];
 93 double minimum[maxn];
 94 double f(int id, double x){
 95     return a[id].st.st * x * x + a[id].st.nd * x + a[id].nd;
 96 }
 97 double solve(){
 98     double low = 1e20, high = -1e20;
 99     FOR(i, 1, n){
100         double tem = f(i, 0);
101         maximize(high, tem), minimize(low, tem);
102         tem = f(i, 1000);
103         maximize(high, tem), minimize(low, tem);
104         if(a[i].st.st && -a[i].st.nd >= 0 && -a[i].st.nd <= 2000 * a[i].st.st){
105             tem = f(i, -a[i].st.nd / (double)(2 * a[i].st.st));
106             maximize(high, tem), minimize(low, tem);
107         }
108     }
109     FOR(i, 1, n) if(a[i].st.st)
110     minimum[i] = ((double)a[i].st.st * a[i].nd * 4 - (double)a[i].st.nd * a[i].st.nd) / ((double)4 * a[i].st.st);
111     int times = 100;
112     double mid;
113     while(times--){
114         mid = low + (high - low) / 2;
115         double L = -1, R = 1001;
116         FOR(i, 1, n){
117             if(a[i].st.st){
118                 if(minimum[i] >= mid){
119                     maximize(L, 1000), minimize(R, 0);
120                     continue;
121                 }
122                 double delta = (double)a[i].st.nd * a[i].st.nd - 4 * (double)a[i].st.st * (a[i].nd - mid);
123                 double x1 = (-a[i].st.nd - sqrt(delta)) / (2 * (double)a[i].st.st);
124                 double x2 = (-a[i].st.nd + sqrt(delta)) / (2 * (double)a[i].st.st);
125                 maximize(L, x1), minimize(R, x2);
126             }else{
127                 double y1 = f(i, 0), y2 = f(i, 1000);
128                 double _low = min(y1, y2), _high = max(y1, y2);
129                 if(_low >= mid) maximize(L, 1000), minimize(R, 0);
130                 else if(_high <= mid) continue;
131                 else if(a[i].st.nd){
132                     double x = (mid - a[i].nd) / a[i].st.nd;
133                     if(a[i].st.nd > 0) minimize(R, x);
134                     else maximize(L, x);
135                 }
136             }
137         }
138         if(L >= 1000 || R <= 0 || L >= R) low = mid;
139         else high = mid;
140     }
141     return mid;
142 }
143 //-------------------------------------------------------------------------
144 int main(){
145     //data_gen(); return 0;
146     //C(); return 0;
147     debug = 0;
148     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
149     if(debug) freopen("in.txt", "r", stdin);
150     //freopen("out.txt", "w", stdout);
151     int T = readint();
152     while(T--){
153         n = readint();
154         FOR(i, 1, n){
155             a[i].st.st = readint();
156             a[i].st.nd = readint();
157             a[i].nd = readint();
158         }
159         double ans = solve();
160         printf("%.4f
", ans);
161     }
162     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
163     return 0;
164 }
code:

 12.Uva 12304 2D Geometry 110 in 1!

关键词:计算几何,高精度

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 #define low_bit(x) ((x) & (-x))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const int mod = 1e9 + 9;
 41 const double double_inf = 1e30;
 42 const double eps = 1e-14;
 43 typedef unsigned long long ul;
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 //Here goes 2d geometry templates
 54 struct Point{
 55     double x, y;
 56     Point(double x = 0, double y = 0) : x(x), y(y) {}
 57 };
 58 typedef Point Vector;
 59 Vector operator + (Vector A, Vector B){
 60     return Vector(A.x + B.x, A.y + B.y);
 61 }
 62 Vector operator - (Point A, Point B){
 63     return Vector(A.x - B.x, A.y - B.y);
 64 }
 65 Vector operator * (Vector A, double p){
 66     return Vector(A.x * p, A.y * p);
 67 }
 68 Vector operator / (Vector A, double p){
 69     return Vector(A.x / p, A.y / p);
 70 }
 71 bool operator < (const Point& a, const Point& b){
 72     return a.x < b.x || (a.x == b.x && a.y < b.y);
 73 }
 74 int dcmp(double x){
 75     if(abs(x) < eps) return 0;
 76     return x < 0 ? -1 : 1;
 77 }
 78 bool operator == (const Point& a, const Point& b){
 79     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 80 }
 81 double Dot(Vector A, Vector B){
 82     return A.x * B.x + A.y * B.y;
 83 }
 84 double Len(Vector A){
 85     return sqrt(Dot(A, A));
 86 }
 87 double Angle(Vector A, Vector B){
 88     return acos(Dot(A, B) / Len(A) / Len(B));
 89 }
 90 double Cross(Vector A, Vector B){
 91     return A.x * B.y - A.y * B.x;
 92 }
 93 double Area2(Point A, Point B, Point C){
 94     return Cross(B - A, C - A);
 95 }
 96 Vector Rotate(Vector A, double rad){
 97     //rotate counterclockwise
 98     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 99 }
100 Vector Normal(Vector A){
101     double L = Len(A);
102     return Vector(-A.y / L, A.x / L);
103 }
104 void Normallize(Vector &A){
105     double L = Len(A);
106     A.x /= L, A.y /= L;
107 }
108 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
109     Vector u = P - Q;
110     double t = Cross(w, u) / Cross(v, w);
111     return P + v * t;
112 }
113 double DistanceToLine(Point P, Point A, Point B){
114     Vector v1 = B - A, v2 = P - A;
115     return abs(Cross(v1, v2)) / Len(v1);
116 }
117 double DistanceSegment(Point P, Point A, Point B){
118     if(A == B) return Len(P - A);
119     Vector v1 = B - A, v2 = P - A, v3 = P - B;
120     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
121     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
122     else return abs(Cross(v1, v2)) / Len(v1);
123 }
124 Point GetLineProjection(Point P, Point A, Point B){
125     Vector v = B - A;
126     return A + v * (Dot(v, P - A) / Dot(v, v));
127 }
128 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
129     //Line1:(a1, a2) Line2:(b1,b2)
130     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
131            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
132     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
133 }
134 bool OnSegment(Point p, Point a1, Point a2){
135     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
136 }
137 Vector GetBisector(Vector v, Vector w){
138     Normallize(v), Normallize(w);
139     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
140 }
141 
142 bool OnLine(Point p, Point a1, Point a2){
143     Vector v1 = p - a1, v2 = a2 - a1;
144     double tem = Cross(v1, v2);
145     return dcmp(tem) == 0;
146 }
147 struct Line{
148     Point p;
149     Vector v;
150     Point point(double t){
151         return Point(p.x + t * v.x, p.y + t * v.y);
152     }
153     Line(Point p, Vector v) : p(p), v(v) {}
154 };
155 struct Circle{
156     Point c;
157     double r;
158     Circle(Point c, double r) : c(c), r(r) {}
159     Circle(int x, int y, int _r){
160         c = Point(x, y);
161         r = _r;
162     }
163     Point point(double a){
164         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
165     }
166 };
167 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
168     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
169     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
170     double delta = f * f - 4 * e * g;
171     if(dcmp(delta) < 0) return 0;
172     if(dcmp(delta) == 0){
173         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
174         return 1;
175     }
176     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
177     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
178     return 2;
179 }
180 double angle(Vector v){
181     return atan2(v.y, v.x);
182     //(-pi, pi]
183 }
184 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
185     double d = Len(C1.c - C2.c);
186     if(dcmp(d) == 0){
187         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
188         return 0; //two circles share identical center
189     }
190     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
191     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
192     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
193     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
194     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
195     sol.pb(p1);
196     if(p1 == p2) return 1;
197     sol.pb(p2);
198     return 2;
199 }
200 int GetPointCircleTangents(Point p, Circle C, Vector* v){
201     Vector u = C.c - p;
202     double dist = Len(u);
203     if(dist < C.r) return 0;//p is inside the circle, no tangents
204     else if(dcmp(dist - C.r) == 0){
205         // p is on the circles, one tangent only
206         v[0] = Rotate(u, PI / 2);
207         return 1;
208     }else{
209         double ang = asin(C.r / dist);
210         v[0] = Rotate(u, -ang);
211         v[1] = Rotate(u, +ang);
212         return 2;
213     }
214 }
215 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
216     //a[i] store point of tangency on Circle A of tangent i
217     //b[i] store point of tangency on Circle B of tangent i
218     //six conditions is in consideration
219     int cnt = 0;
220     if(A.r < B.r) { swap(A, B); swap(a, b); }
221     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
222     int rdiff = A.r - B.r;
223     int rsum = A.r + B.r;
224     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
225     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
226     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
227     if(d2 == rdiff * rdiff){ // internal tangency
228         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
229         return 1;
230     }
231     double ang = acos((A.r - B.r) / sqrt(d2));
232     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
233     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
234     if(d2 == rsum * rsum){
235         //one internal tangent
236         a[cnt] = A.point(base);
237         b[cnt++] = B.point(base + PI);
238     }else if(d2 > rsum * rsum){
239         //two internal tangents
240         double ang = acos((A.r + B.r) / sqrt(d2));
241         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
242         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
243     }
244     return cnt;
245 }
246 Point ReadPoint(){
247     double x, y;
248     scanf("%lf%lf", &x, &y);
249     return Point(x, y);
250 }
251 Circle ReadCircle(){
252     double x, y, r;
253     scanf("%lf%lf%lf", &x, &y, &r);
254     return Circle(x, y, r);
255 }
256 //Here goes 3d geometry templates
257 class cmpt{
258 public:
259     bool operator () (const int &x, const int &y) const{
260         return x > y;
261     }
262 };
263 
264 int Rand(int x, int o){
265     //if o set, return [1, x], else return [0, x - 1]
266     if(!x) return 0;
267     int tem = (int)((double)rand() / RAND_MAX * x) % x;
268     return o ? tem + 1 : tem;
269 }
270 ////////////////////////////////////////////////////////////////////////////////////
271 ////////////////////////////////////////////////////////////////////////////////////
272 void data_gen(){
273     srand(time(0));
274     freopen("in.txt", "w", stdout);
275     int times = 100;
276     printf("%d
", times);
277     while(times--){
278         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
279         int b = Rand(r, 1), d = Rand(r, 1);
280         int m = Rand(100, 1), n = Rand(m, 1);
281         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
282     }
283 }
284 
285 struct cmpx{
286     bool operator () (int x, int y) { return x > y; }
287 };
288 int debug = 1;
289 int dx[] = {-1, 1, 0, 0};
290 int dy[] = {0, 0, -1, 1};
291 //-------------------------------------------------------------------------
292 int n;
293 char cmd[100];
294 map<string, int> mapi;
295 //-------------------------------------------------------------------------
296 int main(){
297     //data_gen(); return 0;
298     //C(); return 0;
299     debug = 0;
300     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
301     if(debug) freopen("in.txt", "r", stdin);
302     //freopen("out.txt", "w", stdout);
303     mapi.clear();
304     mapi[string("CircumscribedCircle")] = 0;
305     mapi[string("InscribedCircle")] = 1;
306     mapi[string("TangentLineThroughPoint")] = 2;
307     mapi[string("CircleThroughAPointAndTangentToALineWithRadius")] = 3;
308     mapi[string("CircleTangentToTwoLinesWithRadius")] = 4;
309     mapi[string("CircleTangentToTwoDisjointCirclesWithRadius")] = 5;
310     while(~scanf("%s", cmd)){
311         int op = mapi[string(cmd)];
312         if(!op){
313             Point A = ReadPoint(), B = ReadPoint(), C = ReadPoint();
314             Point p1 = Point((A.x + B.x) / 2, (A.y + B.y) / 2);
315             Point p2 = Point((A.x + C.x) / 2, (A.y + C.y) / 2);
316             Vector v1 = Normal(B - A), v2 = Normal(C - A);
317             Point p3 = GetLineIntersection(p1, v1, p2, v2);
318             double R = Len(p3 - A);
319             printf("(%.6f,%.6f,%.6f)
", p3.x, p3.y, R);
320         }else if(op == 1){
321             Point A = ReadPoint(), B = ReadPoint(), C = ReadPoint();
322             double a2 = angle(C - A), a1 = angle(B - A);
323             double a3 = (a1 + a2) / 2;
324             Vector v1 = Vector(cos(a3), sin(a3));
325             a2 = angle(A - B), a1 = angle(C - B);
326             a3 = (a2 + a1) / 2;
327             Vector v2 = Vector(cos(a3), sin(a3));
328             Point p3 = GetLineIntersection(A, v1, B, v2);
329             double R = DistanceToLine(p3, A, B);
330             printf("(%.6f,%.6f,%.6f)
", p3.x, p3.y, R);
331         }else if(op == 2){
332             Circle A = ReadCircle();
333             Point p = ReadPoint();
334             Vector tem[5];
335             int cnt = GetPointCircleTangents(p, A, tem);
336             putchar('[');
337             if(cnt == 1){
338                 double ang = angle(tem[0]);
339                 ang *= 180 / PI;
340                 if(dcmp(ang - 180) == 0) ang = 0;
341                 else if(dcmp(ang) < 0) ang += 180;
342                 printf("%.6f", ang);
343             }else if(cnt == 2){
344                 double ang1 = angle(tem[0]), ang2 = angle(tem[1]);
345                 ang1 *= 180 / PI, ang2 *= 180 / PI;
346                 if(dcmp(ang1 - 180) == 0) ang1 = 0;
347                 else if(dcmp(ang1) < 0) ang1 += 180;
348                 if(dcmp(ang2 - 180) == 0) ang2 = 0;
349                 else if(dcmp(ang2) < 0) ang2 += 180;
350                 if(ang1 > ang2) swap(ang1, ang2);
351                 printf("%.6f,%.6f", ang1, ang2);
352             }
353             puts("]");
354         }else if(op == 3){
355             Point p = ReadPoint();
356             Point a1 = ReadPoint(), a2 = ReadPoint();
357             double R;
358             scanf("%lf", &R);
359             double dist = DistanceToLine(p, a1, a2);
360             if(dcmp(2 * R - dist) == 0){
361                 Point tp = GetLineProjection(p, a1, a2);
362                 Point p3 = Point((tp.x + p.x) / 2, (tp.y + p.y) / 2);
363                 printf("[(%.6f,%.6f)]
", p3.x, p3.y);
364             }else if(dcmp(2 * R - dist) < 0){
365                 puts("[]");
366             }else{
367                 if(OnLine(p, a1, a2)){
368                     Vector v1 = Normal(a2 - a1);
369                     Normallize(v1);
370                     Point p3 = Point(p.x + v1.x * R, p.y + v1.y * R);
371                     v1.x = -v1.x, v1.y = -v1.y;
372                     Point p4 = Point(p.x + v1.x * R, p.y + v1.y * R);
373                     if(p4 < p3) swap(p3, p4);
374                     printf("[(%.6f,%.6f),(%.6f,%.6f)]
", p3.x, p3.y, p4.x, p4.y);
375                 }else{
376                     double ang = acos((dist - R) / R);
377                     Point tp = GetLineProjection(p, a1, a2);
378                     Vector v1 = Rotate(tp - p, ang), v2 = Rotate(tp - p, -ang);
379                     ang = angle(v1);
380                     Point p3 = Point(p.x + R * cos(ang), p.y + R * sin(ang));
381                     ang = angle(v2);
382                     Point p4 = Point(p.x + R * cos(ang), p.y + R * sin(ang));
383                     if(p4 < p3) swap(p3, p4);
384                     printf("[(%.6f,%.6f),(%.6f,%.6f)]
", p3.x, p3.y, p4.x, p4.y);
385                 }
386             }
387         }else if(op == 4){
388             Point a1 = ReadPoint(), a2 = ReadPoint(), b1 = ReadPoint(), b2 = ReadPoint();
389             double R;
390             scanf("%lf", &R);
391             Vector v1 = a2 - a1, v2 = b2 - b1;
392             int dx[] = {-1, -1, 1, 1}, dy[] = {-1, 1, 1, -1};
393             Point ans[4];
394             FOR(i, 0, 3){
395                 Vector _v1 = Vector(v1.x * dx[i], v1.y * dx[i]);
396                 Vector _v2 = Vector(v2.x * dy[i], v2.y * dy[i]);
397                 Point p = GetLineIntersection(a1, v1, b1, v2);
398                 double L = R / sin(Angle(_v1, _v2) / 2);
399                 Vector mid = GetBisector(_v1, _v2);
400                 Normallize(mid);
401                 Point p3 = Point(p.x + L * mid.x, p.y + L * mid.y);
402                 ans[i] = p3;
403             }
404             printf("[");
405             sort(ans, ans + 4);
406             FOR(i, 0, 3){
407                 if(i) printf(",");
408                 printf("(%.6f,%.6f)", ans[i].x, ans[i].y);
409             }
410             printf("]
");
411         }else if(op == 5){
412             Circle C1 = ReadCircle(), C2 = ReadCircle();
413             double R;
414             scanf("%lf", &R);
415             double dist = Len(C1.c - C2.c);
416             printf("[");
417             if(dcmp(dist - C1.r - C2.r - 2 * R) == 0){
418                 Line l = Line(C2.c, C1.c - C2.c);
419                 Point p3 = l.point((C2.r + R) / Len(C1.c - C2.c));
420                 printf("(%.6f,%.6f)", p3.x, p3.y);
421             }else if(dcmp(dist - C1.r - C2.r - 2 * R) < 0){
422                 double _a = R + C2.r, _b = Len(C1.c - C2.c), _c = C1.r + R;
423                 double da = acos((_a * _a + _b * _b - _c * _c) / (2 * _a * _b));
424                 double ang = angle(C1.c - C2.c);
425                 double ang1 = ang + da;
426                 Point p3 = Point(C2.c.x + cos(ang1) * _a, C2.c.y + sin(ang1) * _a);
427                 double ang2 = ang - da;
428                 Point p4 = Point(C2.c.x + cos(ang2) * _a, C2.c.y + sin(ang2) * _a);
429                 if(p4 < p3) swap(p3, p4);
430                 printf("(%.6f,%.6f),(%.6f,%.6f)", p3.x, p3.y, p4.x, p4.y);
431             }
432             puts("]");
433         }
434     }
435     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
436     return 0;
437 }
code:

 13.Uva 10561 Treblecross

关键词:组合游戏,SG函数

题意:在一个一维棋盘上放石子,若使得三个石子连续,则胜。初始时棋盘上没有三个连续的石子。

分析:P状态是不难判定的,但如果把所有石子摆放情况当作一个状态,是没法进行处理的。注意到为了避免进入P态,任何石子其左右2个位置应该被空出。

那么我们把这段当作禁止区间,把完整游戏分割为若干独立的子游戏,状态就是连续空出的格子长度。这样用SG函数就可以很方便地求解。

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 #define low_bit(x) ((x) & (-x))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const int mod = 1e9 + 9;
 41 const double double_inf = 1e30;
 42 const double eps = 1e-14;
 43 typedef unsigned long long ul;
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 //Here goes 2d geometry templates
 54 struct Point{
 55     double x, y;
 56     Point(double x = 0, double y = 0) : x(x), y(y) {}
 57 };
 58 typedef Point Vector;
 59 Vector operator + (Vector A, Vector B){
 60     return Vector(A.x + B.x, A.y + B.y);
 61 }
 62 Vector operator - (Point A, Point B){
 63     return Vector(A.x - B.x, A.y - B.y);
 64 }
 65 Vector operator * (Vector A, double p){
 66     return Vector(A.x * p, A.y * p);
 67 }
 68 Vector operator / (Vector A, double p){
 69     return Vector(A.x / p, A.y / p);
 70 }
 71 bool operator < (const Point& a, const Point& b){
 72     return a.x < b.x || (a.x == b.x && a.y < b.y);
 73 }
 74 int dcmp(double x){
 75     if(abs(x) < eps) return 0;
 76     return x < 0 ? -1 : 1;
 77 }
 78 bool operator == (const Point& a, const Point& b){
 79     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 80 }
 81 double Dot(Vector A, Vector B){
 82     return A.x * B.x + A.y * B.y;
 83 }
 84 double Len(Vector A){
 85     return sqrt(Dot(A, A));
 86 }
 87 double Angle(Vector A, Vector B){
 88     return acos(Dot(A, B) / Len(A) / Len(B));
 89 }
 90 double Cross(Vector A, Vector B){
 91     return A.x * B.y - A.y * B.x;
 92 }
 93 double Area2(Point A, Point B, Point C){
 94     return Cross(B - A, C - A);
 95 }
 96 Vector Rotate(Vector A, double rad){
 97     //rotate counterclockwise
 98     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 99 }
100 Vector Normal(Vector A){
101     double L = Len(A);
102     return Vector(-A.y / L, A.x / L);
103 }
104 void Normallize(Vector &A){
105     double L = Len(A);
106     A.x /= L, A.y /= L;
107 }
108 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
109     Vector u = P - Q;
110     double t = Cross(w, u) / Cross(v, w);
111     return P + v * t;
112 }
113 double DistanceToLine(Point P, Point A, Point B){
114     Vector v1 = B - A, v2 = P - A;
115     return abs(Cross(v1, v2)) / Len(v1);
116 }
117 double DistanceToSegment(Point P, Point A, Point B){
118     if(A == B) return Len(P - A);
119     Vector v1 = B - A, v2 = P - A, v3 = P - B;
120     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
121     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
122     else return abs(Cross(v1, v2)) / Len(v1);
123 }
124 Point GetLineProjection(Point P, Point A, Point B){
125     Vector v = B - A;
126     return A + v * (Dot(v, P - A) / Dot(v, v));
127 }
128 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
129     //Line1:(a1, a2) Line2:(b1,b2)
130     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
131            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
132     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
133 }
134 bool OnSegment(Point p, Point a1, Point a2){
135     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
136 }
137 Vector GetBisector(Vector v, Vector w){
138     Normallize(v), Normallize(w);
139     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
140 }
141 
142 bool OnLine(Point p, Point a1, Point a2){
143     Vector v1 = p - a1, v2 = a2 - a1;
144     double tem = Cross(v1, v2);
145     return dcmp(tem) == 0;
146 }
147 struct Line{
148     Point p;
149     Vector v;
150     Point point(double t){
151         return Point(p.x + t * v.x, p.y + t * v.y);
152     }
153     Line(Point p, Vector v) : p(p), v(v) {}
154 };
155 struct Circle{
156     Point c;
157     double r;
158     Circle(Point c, double r) : c(c), r(r) {}
159     Circle(int x, int y, int _r){
160         c = Point(x, y);
161         r = _r;
162     }
163     Point point(double a){
164         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
165     }
166 };
167 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
168     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
169     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
170     double delta = f * f - 4 * e * g;
171     if(dcmp(delta) < 0) return 0;
172     if(dcmp(delta) == 0){
173         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
174         return 1;
175     }
176     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
177     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
178     return 2;
179 }
180 double angle(Vector v){
181     return atan2(v.y, v.x);
182     //(-pi, pi]
183 }
184 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
185     double d = Len(C1.c - C2.c);
186     if(dcmp(d) == 0){
187         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
188         return 0; //two circles share identical center
189     }
190     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
191     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
192     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
193     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
194     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
195     sol.pb(p1);
196     if(p1 == p2) return 1;
197     sol.pb(p2);
198     return 2;
199 }
200 int GetPointCircleTangents(Point p, Circle C, Vector* v){
201     Vector u = C.c - p;
202     double dist = Len(u);
203     if(dist < C.r) return 0;//p is inside the circle, no tangents
204     else if(dcmp(dist - C.r) == 0){
205         // p is on the circles, one tangent only
206         v[0] = Rotate(u, PI / 2);
207         return 1;
208     }else{
209         double ang = asin(C.r / dist);
210         v[0] = Rotate(u, -ang);
211         v[1] = Rotate(u, +ang);
212         return 2;
213     }
214 }
215 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
216     //a[i] store point of tangency on Circle A of tangent i
217     //b[i] store point of tangency on Circle B of tangent i
218     //six conditions is in consideration
219     int cnt = 0;
220     if(A.r < B.r) { swap(A, B); swap(a, b); }
221     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
222     int rdiff = A.r - B.r;
223     int rsum = A.r + B.r;
224     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
225     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
226     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
227     if(d2 == rdiff * rdiff){ // internal tangency
228         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
229         return 1;
230     }
231     double ang = acos((A.r - B.r) / sqrt(d2));
232     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
233     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
234     if(d2 == rsum * rsum){
235         //one internal tangent
236         a[cnt] = A.point(base);
237         b[cnt++] = B.point(base + PI);
238     }else if(d2 > rsum * rsum){
239         //two internal tangents
240         double ang = acos((A.r + B.r) / sqrt(d2));
241         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
242         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
243     }
244     return cnt;
245 }
246 Point ReadPoint(){
247     double x, y;
248     scanf("%lf%lf", &x, &y);
249     return Point(x, y);
250 }
251 Circle ReadCircle(){
252     double x, y, r;
253     scanf("%lf%lf%lf", &x, &y, &r);
254     return Circle(x, y, r);
255 }
256 //Here goes 3d geometry templates
257 struct Point3{
258     double x, y, z;
259     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
260 };
261 typedef Point3 Vector3;
262 Vector3 operator + (Vector3 A, Vector3 B){
263     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
264 }
265 Vector3 operator - (Vector3 A, Vector3 B){
266     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
267 }
268 Vector3 operator * (Vector3 A, double p){
269     return Vector3(A.x * p, A.y * p, A.z * p);
270 }
271 Vector3 operator / (Vector3 A, double p){
272     return Vector3(A.x / p, A.y / p, A.z / p);
273 }
274 double Dot3(Vector3 A, Vector3 B){
275     return A.x * B.x + A.y * B.y + A.z * B.z;
276 }
277 double Len3(Vector3 A){
278     return sqrt(Dot3(A, A));
279 }
280 double Angle3(Vector3 A, Vector3 B){
281     return acos(Dot3(A, B) / Len3(A) / Len3(B));
282 }
283 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
284     return abs(Dot3(p - p0, n));
285 }
286 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
287     return p - n * Dot3(p - p0, n);
288 }
289 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
290     Vector3 v = p2 - p1;
291     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
292     return p1 + v * t;//if t in range [0, 1], intersection on segment
293 }
294 Vector3 Cross(Vector3 A, Vector3 B){
295     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
296 }
297 double Area3(Point3 A, Point3 B, Point3 C){
298     return Len3(Cross(B - A, C - A));
299 }
300 class cmpt{
301 public:
302     bool operator () (const int &x, const int &y) const{
303         return x > y;
304     }
305 };
306 
307 int Rand(int x, int o){
308     //if o set, return [1, x], else return [0, x - 1]
309     if(!x) return 0;
310     int tem = (int)((double)rand() / RAND_MAX * x) % x;
311     return o ? tem + 1 : tem;
312 }
313 ////////////////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////////////////
315 void data_gen(){
316     srand(time(0));
317     freopen("in.txt", "w", stdout);
318     int times = 100;
319     printf("%d
", times);
320     while(times--){
321         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
322         int b = Rand(r, 1), d = Rand(r, 1);
323         int m = Rand(100, 1), n = Rand(m, 1);
324         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
325     }
326 }
327 
328 struct cmpx{
329     bool operator () (int x, int y) { return x > y; }
330 };
331 int debug = 1;
332 int dx[] = {-1, 1, 0, 0};
333 int dy[] = {0, 0, -1, 1};
334 //-------------------------------------------------------------------------
335 const int maxn = 3e2 + 10;
336 int dp[maxn];
337 char s[maxn], s1[maxn];
338 int _ans[maxn], k;
339 pii buf[maxn];
340 int k1;
341 int n;
342 bool vis[maxn];
343 
344 void solve(){
345     k = 0;
346     FOR(i, 0, n - 1){
347         if(s[i] != '.') continue;
348         if(i - 2 >= 0 && s[i - 2] == 'X' && s[i - 1] == 'X'){
349             _ans[k++] = i;
350             continue;
351         }else if(i - 1 >= 0 && i + 1 < n && s[i - 1] == 'X' && s[i + 1] == 'X'){
352             _ans[k++] = i;
353             continue;
354         }else if(i + 2 < n && s[i + 1] == 'X' && s[i + 2] == 'X'){
355             _ans[k++] = i;
356             continue;
357         }
358     }
359     if(k){
360         puts("WINNING");
361         printf("%d", _ans[0] + 1);
362         FOR(i, 1, k - 1) printf(" %d", _ans[i] + 1);
363         printf("
");
364         return;
365     }else{
366         int ans = 0;
367         memcpy(s1, s, sizeof(s));
368         FOR(i, 0, n - 1){
369             if(s[i] == '.') continue;
370             int l = max(0, i - 2), r = min(n - 1, i + 2);
371             FOR(j, l, r) s1[j] = 'P';
372         }
373         memcpy(s, s1, sizeof(s1));
374         k1 = 0;
375         for(int i = 0; i < n; ){
376             if(s[i] != '.'){
377                 ++i;
378                 continue;
379             }else{
380                 int j = i;
381                 while(j < n && s[j] == '.') ++j;
382                 ans ^= dp[j - i];
383                 buf[k1++] = mp(i, j - i);
384                 i = j;
385             }
386         }
387         if(!ans){
388             puts("LOSING
");
389         }else{
390             k = 0;
391             FOR(i, 0, k1 - 1){
392                 int tans = ans;
393                 tans ^= dp[buf[i].nd];
394                 FOR(j, 0, buf[i].nd - 1){
395                     int ttans = tans;
396                     ttans ^= dp[max(0, j - 2)];
397                     ttans ^= dp[max(0, buf[i].nd - j - 3)];
398                     if(!ttans) _ans[k++] = buf[i].st + j;
399                 }
400             }
401             puts("WINNING");
402             printf("%d", _ans[0] + 1);
403             FOR(i, 1, k - 1) printf(" %d", _ans[i] + 1);
404             printf("
");
405         }
406     }
407 }
408 
409 //-------------------------------------------------------------------------
410 int main(){
411     //data_gen(); return 0;
412     //C(); return 0;
413     debug = 0;
414     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
415     if(debug) freopen("in.txt", "r", stdin);
416     //freopen("out.txt", "w", stdout);
417     dp[0] = 0;
418     FOR(i, 1, 305){
419         clr(vis, 0);
420         FOR(j, 0, i - 1){
421             int lhs = dp[max(0, j - 2)];
422             int rhs = dp[max(0, i - j - 3)];
423             vis[lhs ^ rhs] = 1;
424         }
425         int bg = 0;
426         while(vis[bg]) ++bg;
427         dp[i] = bg;
428     }
429     int T = readint();
430     //FOR(i, 1, 9) DP(i);
431     while(T--){
432         n = readstr(s);
433         solve();
434     }
435     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
436     return 0;
437 }
code:

 14.Uva 11762 Race to 1

关键词:数学期望,递推,素数分解

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 #define low_bit(x) ((x) & (-x))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const int mod = 1e9 + 9;
 41 const double double_inf = 1e30;
 42 const double eps = 1e-14;
 43 typedef unsigned long long ul;
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 //Here goes 2d geometry templates
 54 struct Point{
 55     double x, y;
 56     Point(double x = 0, double y = 0) : x(x), y(y) {}
 57 };
 58 typedef Point Vector;
 59 Vector operator + (Vector A, Vector B){
 60     return Vector(A.x + B.x, A.y + B.y);
 61 }
 62 Vector operator - (Point A, Point B){
 63     return Vector(A.x - B.x, A.y - B.y);
 64 }
 65 Vector operator * (Vector A, double p){
 66     return Vector(A.x * p, A.y * p);
 67 }
 68 Vector operator / (Vector A, double p){
 69     return Vector(A.x / p, A.y / p);
 70 }
 71 bool operator < (const Point& a, const Point& b){
 72     return a.x < b.x || (a.x == b.x && a.y < b.y);
 73 }
 74 int dcmp(double x){
 75     if(abs(x) < eps) return 0;
 76     return x < 0 ? -1 : 1;
 77 }
 78 bool operator == (const Point& a, const Point& b){
 79     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 80 }
 81 double Dot(Vector A, Vector B){
 82     return A.x * B.x + A.y * B.y;
 83 }
 84 double Len(Vector A){
 85     return sqrt(Dot(A, A));
 86 }
 87 double Angle(Vector A, Vector B){
 88     return acos(Dot(A, B) / Len(A) / Len(B));
 89 }
 90 double Cross(Vector A, Vector B){
 91     return A.x * B.y - A.y * B.x;
 92 }
 93 double Area2(Point A, Point B, Point C){
 94     return Cross(B - A, C - A);
 95 }
 96 Vector Rotate(Vector A, double rad){
 97     //rotate counterclockwise
 98     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 99 }
100 Vector Normal(Vector A){
101     double L = Len(A);
102     return Vector(-A.y / L, A.x / L);
103 }
104 void Normallize(Vector &A){
105     double L = Len(A);
106     A.x /= L, A.y /= L;
107 }
108 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
109     Vector u = P - Q;
110     double t = Cross(w, u) / Cross(v, w);
111     return P + v * t;
112 }
113 double DistanceToLine(Point P, Point A, Point B){
114     Vector v1 = B - A, v2 = P - A;
115     return abs(Cross(v1, v2)) / Len(v1);
116 }
117 double DistanceToSegment(Point P, Point A, Point B){
118     if(A == B) return Len(P - A);
119     Vector v1 = B - A, v2 = P - A, v3 = P - B;
120     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
121     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
122     else return abs(Cross(v1, v2)) / Len(v1);
123 }
124 Point GetLineProjection(Point P, Point A, Point B){
125     Vector v = B - A;
126     return A + v * (Dot(v, P - A) / Dot(v, v));
127 }
128 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
129     //Line1:(a1, a2) Line2:(b1,b2)
130     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
131            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
132     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
133 }
134 bool OnSegment(Point p, Point a1, Point a2){
135     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
136 }
137 Vector GetBisector(Vector v, Vector w){
138     Normallize(v), Normallize(w);
139     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
140 }
141 
142 bool OnLine(Point p, Point a1, Point a2){
143     Vector v1 = p - a1, v2 = a2 - a1;
144     double tem = Cross(v1, v2);
145     return dcmp(tem) == 0;
146 }
147 struct Line{
148     Point p;
149     Vector v;
150     Point point(double t){
151         return Point(p.x + t * v.x, p.y + t * v.y);
152     }
153     Line(Point p, Vector v) : p(p), v(v) {}
154 };
155 struct Circle{
156     Point c;
157     double r;
158     Circle(Point c, double r) : c(c), r(r) {}
159     Circle(int x, int y, int _r){
160         c = Point(x, y);
161         r = _r;
162     }
163     Point point(double a){
164         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
165     }
166 };
167 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
168     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
169     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
170     double delta = f * f - 4 * e * g;
171     if(dcmp(delta) < 0) return 0;
172     if(dcmp(delta) == 0){
173         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
174         return 1;
175     }
176     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
177     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
178     return 2;
179 }
180 double angle(Vector v){
181     return atan2(v.y, v.x);
182     //(-pi, pi]
183 }
184 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
185     double d = Len(C1.c - C2.c);
186     if(dcmp(d) == 0){
187         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
188         return 0; //two circles share identical center
189     }
190     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
191     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
192     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
193     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
194     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
195     sol.pb(p1);
196     if(p1 == p2) return 1;
197     sol.pb(p2);
198     return 2;
199 }
200 int GetPointCircleTangents(Point p, Circle C, Vector* v){
201     Vector u = C.c - p;
202     double dist = Len(u);
203     if(dist < C.r) return 0;//p is inside the circle, no tangents
204     else if(dcmp(dist - C.r) == 0){
205         // p is on the circles, one tangent only
206         v[0] = Rotate(u, PI / 2);
207         return 1;
208     }else{
209         double ang = asin(C.r / dist);
210         v[0] = Rotate(u, -ang);
211         v[1] = Rotate(u, +ang);
212         return 2;
213     }
214 }
215 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
216     //a[i] store point of tangency on Circle A of tangent i
217     //b[i] store point of tangency on Circle B of tangent i
218     //six conditions is in consideration
219     int cnt = 0;
220     if(A.r < B.r) { swap(A, B); swap(a, b); }
221     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
222     int rdiff = A.r - B.r;
223     int rsum = A.r + B.r;
224     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
225     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
226     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
227     if(d2 == rdiff * rdiff){ // internal tangency
228         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
229         return 1;
230     }
231     double ang = acos((A.r - B.r) / sqrt(d2));
232     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
233     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
234     if(d2 == rsum * rsum){
235         //one internal tangent
236         a[cnt] = A.point(base);
237         b[cnt++] = B.point(base + PI);
238     }else if(d2 > rsum * rsum){
239         //two internal tangents
240         double ang = acos((A.r + B.r) / sqrt(d2));
241         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
242         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
243     }
244     return cnt;
245 }
246 Point ReadPoint(){
247     double x, y;
248     scanf("%lf%lf", &x, &y);
249     return Point(x, y);
250 }
251 Circle ReadCircle(){
252     double x, y, r;
253     scanf("%lf%lf%lf", &x, &y, &r);
254     return Circle(x, y, r);
255 }
256 //Here goes 3d geometry templates
257 struct Point3{
258     double x, y, z;
259     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
260 };
261 typedef Point3 Vector3;
262 Vector3 operator + (Vector3 A, Vector3 B){
263     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
264 }
265 Vector3 operator - (Vector3 A, Vector3 B){
266     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
267 }
268 Vector3 operator * (Vector3 A, double p){
269     return Vector3(A.x * p, A.y * p, A.z * p);
270 }
271 Vector3 operator / (Vector3 A, double p){
272     return Vector3(A.x / p, A.y / p, A.z / p);
273 }
274 double Dot3(Vector3 A, Vector3 B){
275     return A.x * B.x + A.y * B.y + A.z * B.z;
276 }
277 double Len3(Vector3 A){
278     return sqrt(Dot3(A, A));
279 }
280 double Angle3(Vector3 A, Vector3 B){
281     return acos(Dot3(A, B) / Len3(A) / Len3(B));
282 }
283 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
284     return abs(Dot3(p - p0, n));
285 }
286 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
287     return p - n * Dot3(p - p0, n);
288 }
289 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
290     Vector3 v = p2 - p1;
291     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
292     return p1 + v * t;//if t in range [0, 1], intersection on segment
293 }
294 Vector3 Cross(Vector3 A, Vector3 B){
295     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
296 }
297 double Area3(Point3 A, Point3 B, Point3 C){
298     return Len3(Cross(B - A, C - A));
299 }
300 class cmpt{
301 public:
302     bool operator () (const int &x, const int &y) const{
303         return x > y;
304     }
305 };
306 
307 int Rand(int x, int o){
308     //if o set, return [1, x], else return [0, x - 1]
309     if(!x) return 0;
310     int tem = (int)((double)rand() / RAND_MAX * x) % x;
311     return o ? tem + 1 : tem;
312 }
313 ////////////////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////////////////
315 void data_gen(){
316     srand(time(0));
317     freopen("in.txt", "w", stdout);
318     int times = 100;
319     printf("%d
", times);
320     while(times--){
321         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
322         int b = Rand(r, 1), d = Rand(r, 1);
323         int m = Rand(100, 1), n = Rand(m, 1);
324         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
325     }
326 }
327 
328 struct cmpx{
329     bool operator () (int x, int y) { return x > y; }
330 };
331 int debug = 1;
332 int dx[] = {-1, 1, 0, 0};
333 int dy[] = {0, 0, -1, 1};
334 //-------------------------------------------------------------------------
335 const int maxn = 1e6 + 10;
336 int n;
337 int prime[maxn], k;
338 int cnt_prime[maxn];
339 bool vis[maxn];
340 double dp[maxn];
341 bool is_prime[maxn];
342 int p[100], k1;
343 void init(){
344     k = 0;
345     prime[k++] = 2;
346     is_prime[2] = 1;
347     clr(cnt_prime, 0), clr(vis, 0);
348     cnt_prime[2] = 1;
349     for(int i = 3; i < maxn; i += 2){
350         if(vis[i]) continue;
351         prime[k++] = i, cnt_prime[i] = 1, is_prime[i] = 1;
352         for(int j = i; j < maxn; j += i) vis[j] = 1;
353     }
354     cnt_prime[1] = 0;
355     FOR(i, 2, maxn - 1) cnt_prime[i] += cnt_prime[i - 1];
356     clr(vis, 0);
357     vis[1] = 1, dp[1] = 0;
358 }
359 double DP(int x){
360     if(vis[x]) return dp[x];
361     vis[x] = 1;
362     k1 = 0;
363     double tem = 0;
364     int x1 = x;
365     int cnt = 0;
366     int mid = (int)sqrt(x1);
367     for(int i = 0; prime[i] <= mid; i++){
368         if(x1 % prime[i]) continue;
369         tem += DP(x / prime[i]);
370         ++cnt;
371         while(x1 % prime[i] == 0) x1 /= prime[i];
372     }
373     if(x1 > 1) ++cnt, tem += DP(x / x1);
374     tem *= 1. / cnt_prime[x];
375     tem += 1;
376     tem *= cnt_prime[x] / (double)cnt;
377     return dp[x] = tem;
378 }
379 //-------------------------------------------------------------------------
380 int main(){
381     //data_gen(); return 0;
382     //C(); return 0;
383     debug = 0;
384     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
385     if(debug) freopen("in.txt", "r", stdin);
386     //freopen("out.txt", "w", stdout);
387     init();
388     int T = readint(), kase = 0;
389     while(T--){
390         n = readint();
391         double ans = DP(n);
392         printf("Case %d: %.10f
", ++kase, ans);
393     }
394     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
395     return 0;
396 }
code:

 15.LA 11427 Expect the Expected

关键词:数学期望,递推

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 #define low_bit(x) ((x) & (-x))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const int mod = 1e9 + 9;
 41 const double double_inf = 1e30;
 42 const double eps = 1e-14;
 43 typedef unsigned long long ul;
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 //Here goes 2d geometry templates
 54 struct Point{
 55     double x, y;
 56     Point(double x = 0, double y = 0) : x(x), y(y) {}
 57 };
 58 typedef Point Vector;
 59 Vector operator + (Vector A, Vector B){
 60     return Vector(A.x + B.x, A.y + B.y);
 61 }
 62 Vector operator - (Point A, Point B){
 63     return Vector(A.x - B.x, A.y - B.y);
 64 }
 65 Vector operator * (Vector A, double p){
 66     return Vector(A.x * p, A.y * p);
 67 }
 68 Vector operator / (Vector A, double p){
 69     return Vector(A.x / p, A.y / p);
 70 }
 71 bool operator < (const Point& a, const Point& b){
 72     return a.x < b.x || (a.x == b.x && a.y < b.y);
 73 }
 74 int dcmp(double x){
 75     if(abs(x) < eps) return 0;
 76     return x < 0 ? -1 : 1;
 77 }
 78 bool operator == (const Point& a, const Point& b){
 79     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 80 }
 81 double Dot(Vector A, Vector B){
 82     return A.x * B.x + A.y * B.y;
 83 }
 84 double Len(Vector A){
 85     return sqrt(Dot(A, A));
 86 }
 87 double Angle(Vector A, Vector B){
 88     return acos(Dot(A, B) / Len(A) / Len(B));
 89 }
 90 double Cross(Vector A, Vector B){
 91     return A.x * B.y - A.y * B.x;
 92 }
 93 double Area2(Point A, Point B, Point C){
 94     return Cross(B - A, C - A);
 95 }
 96 Vector Rotate(Vector A, double rad){
 97     //rotate counterclockwise
 98     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 99 }
100 Vector Normal(Vector A){
101     double L = Len(A);
102     return Vector(-A.y / L, A.x / L);
103 }
104 void Normallize(Vector &A){
105     double L = Len(A);
106     A.x /= L, A.y /= L;
107 }
108 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
109     Vector u = P - Q;
110     double t = Cross(w, u) / Cross(v, w);
111     return P + v * t;
112 }
113 double DistanceToLine(Point P, Point A, Point B){
114     Vector v1 = B - A, v2 = P - A;
115     return abs(Cross(v1, v2)) / Len(v1);
116 }
117 double DistanceToSegment(Point P, Point A, Point B){
118     if(A == B) return Len(P - A);
119     Vector v1 = B - A, v2 = P - A, v3 = P - B;
120     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
121     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
122     else return abs(Cross(v1, v2)) / Len(v1);
123 }
124 Point GetLineProjection(Point P, Point A, Point B){
125     Vector v = B - A;
126     return A + v * (Dot(v, P - A) / Dot(v, v));
127 }
128 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
129     //Line1:(a1, a2) Line2:(b1,b2)
130     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
131            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
132     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
133 }
134 bool OnSegment(Point p, Point a1, Point a2){
135     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
136 }
137 Vector GetBisector(Vector v, Vector w){
138     Normallize(v), Normallize(w);
139     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
140 }
141 
142 bool OnLine(Point p, Point a1, Point a2){
143     Vector v1 = p - a1, v2 = a2 - a1;
144     double tem = Cross(v1, v2);
145     return dcmp(tem) == 0;
146 }
147 struct Line{
148     Point p;
149     Vector v;
150     Point point(double t){
151         return Point(p.x + t * v.x, p.y + t * v.y);
152     }
153     Line(Point p, Vector v) : p(p), v(v) {}
154 };
155 struct Circle{
156     Point c;
157     double r;
158     Circle(Point c, double r) : c(c), r(r) {}
159     Circle(int x, int y, int _r){
160         c = Point(x, y);
161         r = _r;
162     }
163     Point point(double a){
164         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
165     }
166 };
167 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
168     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
169     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
170     double delta = f * f - 4 * e * g;
171     if(dcmp(delta) < 0) return 0;
172     if(dcmp(delta) == 0){
173         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
174         return 1;
175     }
176     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
177     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
178     return 2;
179 }
180 double angle(Vector v){
181     return atan2(v.y, v.x);
182     //(-pi, pi]
183 }
184 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
185     double d = Len(C1.c - C2.c);
186     if(dcmp(d) == 0){
187         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
188         return 0; //two circles share identical center
189     }
190     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
191     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
192     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
193     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
194     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
195     sol.pb(p1);
196     if(p1 == p2) return 1;
197     sol.pb(p2);
198     return 2;
199 }
200 int GetPointCircleTangents(Point p, Circle C, Vector* v){
201     Vector u = C.c - p;
202     double dist = Len(u);
203     if(dist < C.r) return 0;//p is inside the circle, no tangents
204     else if(dcmp(dist - C.r) == 0){
205         // p is on the circles, one tangent only
206         v[0] = Rotate(u, PI / 2);
207         return 1;
208     }else{
209         double ang = asin(C.r / dist);
210         v[0] = Rotate(u, -ang);
211         v[1] = Rotate(u, +ang);
212         return 2;
213     }
214 }
215 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
216     //a[i] store point of tangency on Circle A of tangent i
217     //b[i] store point of tangency on Circle B of tangent i
218     //six conditions is in consideration
219     int cnt = 0;
220     if(A.r < B.r) { swap(A, B); swap(a, b); }
221     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
222     int rdiff = A.r - B.r;
223     int rsum = A.r + B.r;
224     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
225     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
226     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
227     if(d2 == rdiff * rdiff){ // internal tangency
228         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
229         return 1;
230     }
231     double ang = acos((A.r - B.r) / sqrt(d2));
232     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
233     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
234     if(d2 == rsum * rsum){
235         //one internal tangent
236         a[cnt] = A.point(base);
237         b[cnt++] = B.point(base + PI);
238     }else if(d2 > rsum * rsum){
239         //two internal tangents
240         double ang = acos((A.r + B.r) / sqrt(d2));
241         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
242         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
243     }
244     return cnt;
245 }
246 Point ReadPoint(){
247     double x, y;
248     scanf("%lf%lf", &x, &y);
249     return Point(x, y);
250 }
251 Circle ReadCircle(){
252     double x, y, r;
253     scanf("%lf%lf%lf", &x, &y, &r);
254     return Circle(x, y, r);
255 }
256 //Here goes 3d geometry templates
257 struct Point3{
258     double x, y, z;
259     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
260 };
261 typedef Point3 Vector3;
262 Vector3 operator + (Vector3 A, Vector3 B){
263     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
264 }
265 Vector3 operator - (Vector3 A, Vector3 B){
266     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
267 }
268 Vector3 operator * (Vector3 A, double p){
269     return Vector3(A.x * p, A.y * p, A.z * p);
270 }
271 Vector3 operator / (Vector3 A, double p){
272     return Vector3(A.x / p, A.y / p, A.z / p);
273 }
274 double Dot3(Vector3 A, Vector3 B){
275     return A.x * B.x + A.y * B.y + A.z * B.z;
276 }
277 double Len3(Vector3 A){
278     return sqrt(Dot3(A, A));
279 }
280 double Angle3(Vector3 A, Vector3 B){
281     return acos(Dot3(A, B) / Len3(A) / Len3(B));
282 }
283 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
284     return abs(Dot3(p - p0, n));
285 }
286 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
287     return p - n * Dot3(p - p0, n);
288 }
289 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
290     Vector3 v = p2 - p1;
291     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
292     return p1 + v * t;//if t in range [0, 1], intersection on segment
293 }
294 Vector3 Cross(Vector3 A, Vector3 B){
295     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
296 }
297 double Area3(Point3 A, Point3 B, Point3 C){
298     return Len3(Cross(B - A, C - A));
299 }
300 class cmpt{
301 public:
302     bool operator () (const int &x, const int &y) const{
303         return x > y;
304     }
305 };
306 
307 int Rand(int x, int o){
308     //if o set, return [1, x], else return [0, x - 1]
309     if(!x) return 0;
310     int tem = (int)((double)rand() / RAND_MAX * x) % x;
311     return o ? tem + 1 : tem;
312 }
313 ////////////////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////////////////
315 void data_gen(){
316     srand(time(0));
317     freopen("in.txt", "w", stdout);
318     int times = 100;
319     printf("%d
", times);
320     while(times--){
321         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
322         int b = Rand(r, 1), d = Rand(r, 1);
323         int m = Rand(100, 1), n = Rand(m, 1);
324         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
325     }
326 }
327 
328 struct cmpx{
329     bool operator () (int x, int y) { return x > y; }
330 };
331 int debug = 1;
332 int dx[] = {-1, 1, 0, 0};
333 int dy[] = {0, 0, -1, 1};
334 //-------------------------------------------------------------------------
335 const int maxn = 1e2 + 10;
336 pii p;
337 int n;
338 double dp[maxn][maxn];
339 double _p;
340 int lim[maxn];
341 double ans[maxn][maxn];
342 char s[2000];
343 void dealstr(){
344     char tem[20];
345     char *pointer = s;
346     int k = 0;
347     while(*pointer >= '0' && *pointer <= '9') tem[k++] = *pointer, ++pointer;
348     int base = 1, cur = 0;
349     ROF(i, k - 1, 0) cur += base * (tem[i] - '0'), base *= 10;
350     p.st = cur;
351     ++pointer, k = 0;
352     while(*pointer >= '0' && *pointer <= '9') tem[k++] = *pointer, ++pointer;
353     base = 1, cur = 0;
354     ROF(i, k - 1, 0) cur += base * (tem[i] - '0'), base *= 10;
355     p.nd = cur;
356 }
357 //-------------------------------------------------------------------------
358 int main(){
359     //data_gen(); return 0;
360     //C(); return 0;
361     debug = 1;
362     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
363     if(debug) freopen("in.txt", "r", stdin);
364     //freopen("out.txt", "w", stdout);
365     int T = readint(), kase = 0;
366     while(T--){
367         scanf("%s", s);
368         dealstr();
369         scanf("%d", &n);
370         _p = (double)p.st / p.nd;
371         dp[1][0] = 1 - _p, dp[1][1] = _p;
372         FOR(i, 2, n){
373             dp[i][0] = dp[i - 1][0] * (1 - _p);
374             FOR(j, 1, i) dp[i][j] = (dp[i - 1][j] * (1 - _p) + dp[i - 1][j - 1] * _p);
375         }
376         FOR(i, 1, n) lim[i] = p.st * i / p.nd;
377         ans[1][0] = 1 - _p;
378         ans[1][1] = _p;
379         FOR(i, 2, n){
380             FOR(j, 0, lim[i]) ans[i][j] = 0;
381             FOR(j, 0, lim[i - 1]){
382                 ans[i][j] += ans[i - 1][j] * (1 - _p);
383                 if(j + 1 <= lim[i]) ans[i][j + 1] += ans[i - 1][j] * _p;
384             }
385         }
386         double q = 0;
387         FOR(i, 0, lim[n]) q += ans[n][i];
388         //dbg(1 - q);
389         double _ans = 1. / q;
390         printf("Case #%d: %d
", ++kase, (int)_ans);
391     }
392     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
393     return 0;
394 }
code:

 16.LA 3485 Bridge

关键词:数值分析,数论

提示:此题有坑点,另外注意合理转换问题提高精度

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 #define low_bit(x) ((x) & (-x))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const double double_inf = 1e30;
 41 const double eps = 1e-14;
 42 typedef unsigned long long ul;
 43 inline int readint(){
 44     int x;
 45     scanf("%d", &x);
 46     return x;
 47 }
 48 inline int readstr(char *s){
 49     scanf("%s", s);
 50     return strlen(s);
 51 }
 52 //Here goes 2d geometry templates
 53 struct Point{
 54     double x, y;
 55     Point(double x = 0, double y = 0) : x(x), y(y) {}
 56 };
 57 typedef Point Vector;
 58 Vector operator + (Vector A, Vector B){
 59     return Vector(A.x + B.x, A.y + B.y);
 60 }
 61 Vector operator - (Point A, Point B){
 62     return Vector(A.x - B.x, A.y - B.y);
 63 }
 64 Vector operator * (Vector A, double p){
 65     return Vector(A.x * p, A.y * p);
 66 }
 67 Vector operator / (Vector A, double p){
 68     return Vector(A.x / p, A.y / p);
 69 }
 70 bool operator < (const Point& a, const Point& b){
 71     return a.x < b.x || (a.x == b.x && a.y < b.y);
 72 }
 73 int dcmp(double x){
 74     if(abs(x) < eps) return 0;
 75     return x < 0 ? -1 : 1;
 76 }
 77 bool operator == (const Point& a, const Point& b){
 78     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 79 }
 80 double Dot(Vector A, Vector B){
 81     return A.x * B.x + A.y * B.y;
 82 }
 83 double Len(Vector A){
 84     return sqrt(Dot(A, A));
 85 }
 86 double Angle(Vector A, Vector B){
 87     return acos(Dot(A, B) / Len(A) / Len(B));
 88 }
 89 double Cross(Vector A, Vector B){
 90     return A.x * B.y - A.y * B.x;
 91 }
 92 double Area2(Point A, Point B, Point C){
 93     return Cross(B - A, C - A);
 94 }
 95 Vector Rotate(Vector A, double rad){
 96     //rotate counterclockwise
 97     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 98 }
 99 Vector Normal(Vector A){
100     double L = Len(A);
101     return Vector(-A.y / L, A.x / L);
102 }
103 void Normallize(Vector &A){
104     double L = Len(A);
105     A.x /= L, A.y /= L;
106 }
107 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
108     Vector u = P - Q;
109     double t = Cross(w, u) / Cross(v, w);
110     return P + v * t;
111 }
112 double DistanceToLine(Point P, Point A, Point B){
113     Vector v1 = B - A, v2 = P - A;
114     return abs(Cross(v1, v2)) / Len(v1);
115 }
116 double DistanceToSegment(Point P, Point A, Point B){
117     if(A == B) return Len(P - A);
118     Vector v1 = B - A, v2 = P - A, v3 = P - B;
119     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
120     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
121     else return abs(Cross(v1, v2)) / Len(v1);
122 }
123 Point GetLineProjection(Point P, Point A, Point B){
124     Vector v = B - A;
125     return A + v * (Dot(v, P - A) / Dot(v, v));
126 }
127 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
128     //Line1:(a1, a2) Line2:(b1,b2)
129     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
130            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
131     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
132 }
133 bool OnSegment(Point p, Point a1, Point a2){
134     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
135 }
136 Vector GetBisector(Vector v, Vector w){
137     Normallize(v), Normallize(w);
138     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
139 }
140 
141 bool OnLine(Point p, Point a1, Point a2){
142     Vector v1 = p - a1, v2 = a2 - a1;
143     double tem = Cross(v1, v2);
144     return dcmp(tem) == 0;
145 }
146 struct Line{
147     Point p;
148     Vector v;
149     Point point(double t){
150         return Point(p.x + t * v.x, p.y + t * v.y);
151     }
152     Line(Point p, Vector v) : p(p), v(v) {}
153 };
154 struct Circle{
155     Point c;
156     double r;
157     Circle(Point c, double r) : c(c), r(r) {}
158     Circle(int x, int y, int _r){
159         c = Point(x, y);
160         r = _r;
161     }
162     Point point(double a){
163         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
164     }
165 };
166 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
167     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
168     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
169     double delta = f * f - 4 * e * g;
170     if(dcmp(delta) < 0) return 0;
171     if(dcmp(delta) == 0){
172         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
173         return 1;
174     }
175     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
176     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
177     return 2;
178 }
179 double angle(Vector v){
180     return atan2(v.y, v.x);
181     //(-pi, pi]
182 }
183 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
184     double d = Len(C1.c - C2.c);
185     if(dcmp(d) == 0){
186         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
187         return 0; //two circles share identical center
188     }
189     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
190     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
191     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
192     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
193     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
194     sol.pb(p1);
195     if(p1 == p2) return 1;
196     sol.pb(p2);
197     return 2;
198 }
199 int GetPointCircleTangents(Point p, Circle C, Vector* v){
200     Vector u = C.c - p;
201     double dist = Len(u);
202     if(dist < C.r) return 0;//p is inside the circle, no tangents
203     else if(dcmp(dist - C.r) == 0){
204         // p is on the circles, one tangent only
205         v[0] = Rotate(u, PI / 2);
206         return 1;
207     }else{
208         double ang = asin(C.r / dist);
209         v[0] = Rotate(u, -ang);
210         v[1] = Rotate(u, +ang);
211         return 2;
212     }
213 }
214 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
215     //a[i] store point of tangency on Circle A of tangent i
216     //b[i] store point of tangency on Circle B of tangent i
217     //six conditions is in consideration
218     int cnt = 0;
219     if(A.r < B.r) { swap(A, B); swap(a, b); }
220     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
221     int rdiff = A.r - B.r;
222     int rsum = A.r + B.r;
223     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
224     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
225     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
226     if(d2 == rdiff * rdiff){ // internal tangency
227         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
228         return 1;
229     }
230     double ang = acos((A.r - B.r) / sqrt(d2));
231     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
232     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
233     if(d2 == rsum * rsum){
234         //one internal tangent
235         a[cnt] = A.point(base);
236         b[cnt++] = B.point(base + PI);
237     }else if(d2 > rsum * rsum){
238         //two internal tangents
239         double ang = acos((A.r + B.r) / sqrt(d2));
240         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
241         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
242     }
243     return cnt;
244 }
245 Point ReadPoint(){
246     double x, y;
247     scanf("%lf%lf", &x, &y);
248     return Point(x, y);
249 }
250 Circle ReadCircle(){
251     double x, y, r;
252     scanf("%lf%lf%lf", &x, &y, &r);
253     return Circle(x, y, r);
254 }
255 //Here goes 3d geometry templates
256 struct Point3{
257     double x, y, z;
258     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
259 };
260 typedef Point3 Vector3;
261 Vector3 operator + (Vector3 A, Vector3 B){
262     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
263 }
264 Vector3 operator - (Vector3 A, Vector3 B){
265     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
266 }
267 Vector3 operator * (Vector3 A, double p){
268     return Vector3(A.x * p, A.y * p, A.z * p);
269 }
270 Vector3 operator / (Vector3 A, double p){
271     return Vector3(A.x / p, A.y / p, A.z / p);
272 }
273 double Dot3(Vector3 A, Vector3 B){
274     return A.x * B.x + A.y * B.y + A.z * B.z;
275 }
276 double Len3(Vector3 A){
277     return sqrt(Dot3(A, A));
278 }
279 double Angle3(Vector3 A, Vector3 B){
280     return acos(Dot3(A, B) / Len3(A) / Len3(B));
281 }
282 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
283     return abs(Dot3(p - p0, n));
284 }
285 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
286     return p - n * Dot3(p - p0, n);
287 }
288 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
289     Vector3 v = p2 - p1;
290     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
291     return p1 + v * t;//if t in range [0, 1], intersection on segment
292 }
293 Vector3 Cross(Vector3 A, Vector3 B){
294     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
295 }
296 double Area3(Point3 A, Point3 B, Point3 C){
297     return Len3(Cross(B - A, C - A));
298 }
299 class cmpt{
300 public:
301     bool operator () (const int &x, const int &y) const{
302         return x > y;
303     }
304 };
305 
306 int Rand(int x, int o){
307     //if o set, return [1, x], else return [0, x - 1]
308     if(!x) return 0;
309     int tem = (int)((double)rand() / RAND_MAX * x) % x;
310     return o ? tem + 1 : tem;
311 }
312 ////////////////////////////////////////////////////////////////////////////////////
313 ////////////////////////////////////////////////////////////////////////////////////
314 void data_gen(){
315     srand(time(0));
316     freopen("in.txt", "w", stdout);
317     int times = 100;
318     printf("%d
", times);
319     while(times--){
320         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
321         int b = Rand(r, 1), d = Rand(r, 1);
322         int m = Rand(100, 1), n = Rand(m, 1);
323         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
324     }
325 }
326 
327 struct cmpx{
328     bool operator () (int x, int y) { return x > y; }
329 };
330 int debug = 1;
331 int dx[] = {-1, 1, 0, 0};
332 int dy[] = {0, 0, -1, 1};
333 int mod;
334 //-------------------------------------------------------------------------
335 const int maxn = 1e2 + 10;
336 ll D, H, B, L;
337 double _ratio;
338 double P(double x){
339     return x * sqrt(1 + x * x) + log(sqrt(1 + x * x) + x);
340 }
341 double Q(double x){
342     return _ratio * x;
343 }
344 //-------------------------------------------------------------------------
345 int main(){
346     //data_gen(); return 0;
347     //C(); return 0;
348     debug = 0;
349     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
350     if(debug) freopen("in.txt", "r", stdin);
351     //freopen("out.txt", "w", stdout);
352     int T = readint(), kase = 0;
353     while(T--){
354         scanf("%lld%lld%lld%lld", &D, &H, &B, &L);
355         double cnt = (double)B / D;
356         ll m = (ll)cnt;
357         maximize(m, 1);
358         while(B > m * D) ++m;
359         _ratio = (double)L / B * 2;
360         double l = 0, r = 1e60;
361         while(r - l > 1e-10){
362             double mid = l + (r - l) / 2;
363             double p0 = P(mid), q0 = Q(mid);
364             if(p0 > q0) r = mid;
365             else l = mid;
366         }
367         double ans = l;
368         //dbg2("ratio", _ratio);
369         //dbg2("ans", ans);
370         printf("Case %d:
", ++kase);
371         printf("%.2f
", H - ((double)B * ans) / 4. / (double)m);
372         if(T) putchar('
');
373     }
374     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
375     return 0;
376 }
code:

 17.Uva 11752 The Super Powers

关键词:STL

提示:注意数值转化和溢出

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 #define low_bit(x) ((x) & (-x))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const double double_inf = 1e30;
 41 const double eps = 1e-14;
 42 typedef unsigned long long ul;
 43 inline int readint(){
 44     int x;
 45     scanf("%d", &x);
 46     return x;
 47 }
 48 inline int readstr(char *s){
 49     scanf("%s", s);
 50     return strlen(s);
 51 }
 52 //Here goes 2d geometry templates
 53 struct Point{
 54     double x, y;
 55     Point(double x = 0, double y = 0) : x(x), y(y) {}
 56 };
 57 typedef Point Vector;
 58 Vector operator + (Vector A, Vector B){
 59     return Vector(A.x + B.x, A.y + B.y);
 60 }
 61 Vector operator - (Point A, Point B){
 62     return Vector(A.x - B.x, A.y - B.y);
 63 }
 64 Vector operator * (Vector A, double p){
 65     return Vector(A.x * p, A.y * p);
 66 }
 67 Vector operator / (Vector A, double p){
 68     return Vector(A.x / p, A.y / p);
 69 }
 70 bool operator < (const Point& a, const Point& b){
 71     return a.x < b.x || (a.x == b.x && a.y < b.y);
 72 }
 73 int dcmp(double x){
 74     if(abs(x) < eps) return 0;
 75     return x < 0 ? -1 : 1;
 76 }
 77 bool operator == (const Point& a, const Point& b){
 78     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 79 }
 80 double Dot(Vector A, Vector B){
 81     return A.x * B.x + A.y * B.y;
 82 }
 83 double Len(Vector A){
 84     return sqrt(Dot(A, A));
 85 }
 86 double Angle(Vector A, Vector B){
 87     return acos(Dot(A, B) / Len(A) / Len(B));
 88 }
 89 double Cross(Vector A, Vector B){
 90     return A.x * B.y - A.y * B.x;
 91 }
 92 double Area2(Point A, Point B, Point C){
 93     return Cross(B - A, C - A);
 94 }
 95 Vector Rotate(Vector A, double rad){
 96     //rotate counterclockwise
 97     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 98 }
 99 Vector Normal(Vector A){
100     double L = Len(A);
101     return Vector(-A.y / L, A.x / L);
102 }
103 void Normallize(Vector &A){
104     double L = Len(A);
105     A.x /= L, A.y /= L;
106 }
107 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
108     Vector u = P - Q;
109     double t = Cross(w, u) / Cross(v, w);
110     return P + v * t;
111 }
112 double DistanceToLine(Point P, Point A, Point B){
113     Vector v1 = B - A, v2 = P - A;
114     return abs(Cross(v1, v2)) / Len(v1);
115 }
116 double DistanceToSegment(Point P, Point A, Point B){
117     if(A == B) return Len(P - A);
118     Vector v1 = B - A, v2 = P - A, v3 = P - B;
119     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
120     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
121     else return abs(Cross(v1, v2)) / Len(v1);
122 }
123 Point GetLineProjection(Point P, Point A, Point B){
124     Vector v = B - A;
125     return A + v * (Dot(v, P - A) / Dot(v, v));
126 }
127 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
128     //Line1:(a1, a2) Line2:(b1,b2)
129     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
130            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
131     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
132 }
133 bool OnSegment(Point p, Point a1, Point a2){
134     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
135 }
136 Vector GetBisector(Vector v, Vector w){
137     Normallize(v), Normallize(w);
138     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
139 }
140 
141 bool OnLine(Point p, Point a1, Point a2){
142     Vector v1 = p - a1, v2 = a2 - a1;
143     double tem = Cross(v1, v2);
144     return dcmp(tem) == 0;
145 }
146 struct Line{
147     Point p;
148     Vector v;
149     Point point(double t){
150         return Point(p.x + t * v.x, p.y + t * v.y);
151     }
152     Line(Point p, Vector v) : p(p), v(v) {}
153 };
154 struct Circle{
155     Point c;
156     double r;
157     Circle(Point c, double r) : c(c), r(r) {}
158     Circle(int x, int y, int _r){
159         c = Point(x, y);
160         r = _r;
161     }
162     Point point(double a){
163         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
164     }
165 };
166 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
167     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
168     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
169     double delta = f * f - 4 * e * g;
170     if(dcmp(delta) < 0) return 0;
171     if(dcmp(delta) == 0){
172         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
173         return 1;
174     }
175     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
176     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
177     return 2;
178 }
179 double angle(Vector v){
180     return atan2(v.y, v.x);
181     //(-pi, pi]
182 }
183 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
184     double d = Len(C1.c - C2.c);
185     if(dcmp(d) == 0){
186         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
187         return 0; //two circles share identical center
188     }
189     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
190     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
191     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
192     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
193     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
194     sol.pb(p1);
195     if(p1 == p2) return 1;
196     sol.pb(p2);
197     return 2;
198 }
199 int GetPointCircleTangents(Point p, Circle C, Vector* v){
200     Vector u = C.c - p;
201     double dist = Len(u);
202     if(dist < C.r) return 0;//p is inside the circle, no tangents
203     else if(dcmp(dist - C.r) == 0){
204         // p is on the circles, one tangent only
205         v[0] = Rotate(u, PI / 2);
206         return 1;
207     }else{
208         double ang = asin(C.r / dist);
209         v[0] = Rotate(u, -ang);
210         v[1] = Rotate(u, +ang);
211         return 2;
212     }
213 }
214 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
215     //a[i] store point of tangency on Circle A of tangent i
216     //b[i] store point of tangency on Circle B of tangent i
217     //six conditions is in consideration
218     int cnt = 0;
219     if(A.r < B.r) { swap(A, B); swap(a, b); }
220     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
221     int rdiff = A.r - B.r;
222     int rsum = A.r + B.r;
223     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
224     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
225     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
226     if(d2 == rdiff * rdiff){ // internal tangency
227         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
228         return 1;
229     }
230     double ang = acos((A.r - B.r) / sqrt(d2));
231     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
232     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
233     if(d2 == rsum * rsum){
234         //one internal tangent
235         a[cnt] = A.point(base);
236         b[cnt++] = B.point(base + PI);
237     }else if(d2 > rsum * rsum){
238         //two internal tangents
239         double ang = acos((A.r + B.r) / sqrt(d2));
240         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
241         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
242     }
243     return cnt;
244 }
245 Point ReadPoint(){
246     double x, y;
247     scanf("%lf%lf", &x, &y);
248     return Point(x, y);
249 }
250 Circle ReadCircle(){
251     double x, y, r;
252     scanf("%lf%lf%lf", &x, &y, &r);
253     return Circle(x, y, r);
254 }
255 //Here goes 3d geometry templates
256 struct Point3{
257     double x, y, z;
258     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
259 };
260 typedef Point3 Vector3;
261 Vector3 operator + (Vector3 A, Vector3 B){
262     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
263 }
264 Vector3 operator - (Vector3 A, Vector3 B){
265     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
266 }
267 Vector3 operator * (Vector3 A, double p){
268     return Vector3(A.x * p, A.y * p, A.z * p);
269 }
270 Vector3 operator / (Vector3 A, double p){
271     return Vector3(A.x / p, A.y / p, A.z / p);
272 }
273 double Dot3(Vector3 A, Vector3 B){
274     return A.x * B.x + A.y * B.y + A.z * B.z;
275 }
276 double Len3(Vector3 A){
277     return sqrt(Dot3(A, A));
278 }
279 double Angle3(Vector3 A, Vector3 B){
280     return acos(Dot3(A, B) / Len3(A) / Len3(B));
281 }
282 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
283     return abs(Dot3(p - p0, n));
284 }
285 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
286     return p - n * Dot3(p - p0, n);
287 }
288 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
289     Vector3 v = p2 - p1;
290     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
291     return p1 + v * t;//if t in range [0, 1], intersection on segment
292 }
293 Vector3 Cross(Vector3 A, Vector3 B){
294     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
295 }
296 double Area3(Point3 A, Point3 B, Point3 C){
297     return Len3(Cross(B - A, C - A));
298 }
299 class cmpt{
300 public:
301     bool operator () (const int &x, const int &y) const{
302         return x > y;
303     }
304 };
305 
306 int Rand(int x, int o){
307     //if o set, return [1, x], else return [0, x - 1]
308     if(!x) return 0;
309     int tem = (int)((double)rand() / RAND_MAX * x) % x;
310     return o ? tem + 1 : tem;
311 }
312 ////////////////////////////////////////////////////////////////////////////////////
313 ////////////////////////////////////////////////////////////////////////////////////
314 void data_gen(){
315     srand(time(0));
316     freopen("in.txt", "w", stdout);
317     int times = 100;
318     printf("%d
", times);
319     while(times--){
320         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
321         int b = Rand(r, 1), d = Rand(r, 1);
322         int m = Rand(100, 1), n = Rand(m, 1);
323         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
324     }
325 }
326 
327 struct cmpx{
328     bool operator () (int x, int y) { return x > y; }
329 };
330 int debug = 1;
331 int dx[] = {-1, 1, 0, 0};
332 int dy[] = {0, 0, -1, 1};
333 const int mod = 1e9 + 7;
334 //-------------------------------------------------------------------------
335 const int maxn = 1e6 + 10;
336 const double limit = (double)(1ll << 62) * 4. - 1.;
337 int k;
338 int prime[1 << 16], pointer;
339 bool vis[(1 << 16) + 10];
340 ul power(ul tem, int times){
341     ul ans = 1.;
342     while(times){
343         if(times & 1) ans *= tem;
344         times >>= 1;
345         tem *= tem;
346     }
347     return ans;
348 }
349 bool is_prime(int x){
350     if(x == 2) return 1;
351     if(!(x & 1)) return 0;
352     int mid = (int)sqrt(x);
353     for(int i = 3; i <= mid; i += 2){
354         if(x % i == 0) return 0;
355     }
356     return 1;
357 }
358 //-------------------------------------------------------------------------
359 
360 int main(){
361     //data_gen(); return 0;
362     //C(); return 0;
363     debug = 0;
364     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
365     if(debug) freopen("in.txt", "r", stdin);
366     //freopen("in.txt", "w", stdout);
367     set<ul> S;
368     S.clear();
369     for(int i = 4; i < 64; i++){
370         if(is_prime(i)) continue;
371         double lim = exp(64. / i * log(2));
372         for(int j = 2; j < lim; j++){
373             ul tem = power(j, i);
374             S.insert(tem);
375         }
376     }
377     S.insert(1);
378     //dbg(S.size());
379     set<ul> :: iterator it;
380     it = S.begin();
381     while(it != S.end()){
382         printf("%llu
", *it);
383         ++it;
384     }
385     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
386     return 0;
387 }
code:

 18.LA 4060 The Bells are Ringing

关键词:随机素数分解算法,暴力

提示:可能存在别的解法?

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) cout << x << endl
 30 #define dbg2(x, y) cout << x << " " << y << endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 using namespace std;
 35 typedef long long ll;
 36 const int int_inf = 0x3f3f3f3f;
 37 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 38 const int INT_INF = (int)((1ll << 31) - 1);
 39 const double double_inf = 1e30;
 40 const double eps = 1e-14;
 41 typedef unsigned long long ul;
 42 inline int readint(){
 43     int x;
 44     scanf("%d", &x);
 45     return x;
 46 }
 47 inline int readstr(char *s){
 48     scanf("%s", s);
 49     return strlen(s);
 50 }
 51 //Here goes 2d geometry templates
 52 struct Point{
 53     double x, y;
 54     Point(double x = 0, double y = 0) : x(x), y(y) {}
 55 };
 56 typedef Point Vector;
 57 Vector operator + (Vector A, Vector B){
 58     return Vector(A.x + B.x, A.y + B.y);
 59 }
 60 Vector operator - (Point A, Point B){
 61     return Vector(A.x - B.x, A.y - B.y);
 62 }
 63 Vector operator * (Vector A, double p){
 64     return Vector(A.x * p, A.y * p);
 65 }
 66 Vector operator / (Vector A, double p){
 67     return Vector(A.x / p, A.y / p);
 68 }
 69 bool operator < (const Point& a, const Point& b){
 70     return a.x < b.x || (a.x == b.x && a.y < b.y);
 71 }
 72 int dcmp(double x){
 73     if(abs(x) < eps) return 0;
 74     return x < 0 ? -1 : 1;
 75 }
 76 bool operator == (const Point& a, const Point& b){
 77     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 78 }
 79 double Dot(Vector A, Vector B){
 80     return A.x * B.x + A.y * B.y;
 81 }
 82 double Len(Vector A){
 83     return sqrt(Dot(A, A));
 84 }
 85 double Angle(Vector A, Vector B){
 86     return acos(Dot(A, B) / Len(A) / Len(B));
 87 }
 88 double Cross(Vector A, Vector B){
 89     return A.x * B.y - A.y * B.x;
 90 }
 91 double Area2(Point A, Point B, Point C){
 92     return Cross(B - A, C - A);
 93 }
 94 Vector Rotate(Vector A, double rad){
 95     //rotate counterclockwise
 96     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 97 }
 98 Vector Normal(Vector A){
 99     double L = Len(A);
100     return Vector(-A.y / L, A.x / L);
101 }
102 void Normallize(Vector &A){
103     double L = Len(A);
104     A.x /= L, A.y /= L;
105 }
106 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
107     Vector u = P - Q;
108     double t = Cross(w, u) / Cross(v, w);
109     return P + v * t;
110 }
111 double DistanceToLine(Point P, Point A, Point B){
112     Vector v1 = B - A, v2 = P - A;
113     return abs(Cross(v1, v2)) / Len(v1);
114 }
115 double DistanceToSegment(Point P, Point A, Point B){
116     if(A == B) return Len(P - A);
117     Vector v1 = B - A, v2 = P - A, v3 = P - B;
118     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
119     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
120     else return abs(Cross(v1, v2)) / Len(v1);
121 }
122 Point GetLineProjection(Point P, Point A, Point B){
123     Vector v = B - A;
124     return A + v * (Dot(v, P - A) / Dot(v, v));
125 }
126 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
127     //Line1:(a1, a2) Line2:(b1,b2)
128     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
129            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
130     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
131 }
132 bool OnSegment(Point p, Point a1, Point a2){
133     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
134 }
135 Vector GetBisector(Vector v, Vector w){
136     Normallize(v), Normallize(w);
137     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
138 }
139 
140 bool OnLine(Point p, Point a1, Point a2){
141     Vector v1 = p - a1, v2 = a2 - a1;
142     double tem = Cross(v1, v2);
143     return dcmp(tem) == 0;
144 }
145 struct Line{
146     Point p;
147     Vector v;
148     Point point(double t){
149         return Point(p.x + t * v.x, p.y + t * v.y);
150     }
151     Line(Point p, Vector v) : p(p), v(v) {}
152 };
153 struct Circle{
154     Point c;
155     double r;
156     Circle(Point c, double r) : c(c), r(r) {}
157     Circle(int x, int y, int _r){
158         c = Point(x, y);
159         r = _r;
160     }
161     Point point(double a){
162         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
163     }
164 };
165 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
166     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
167     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
168     double delta = f * f - 4 * e * g;
169     if(dcmp(delta) < 0) return 0;
170     if(dcmp(delta) == 0){
171         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
172         return 1;
173     }
174     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
175     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
176     return 2;
177 }
178 double angle(Vector v){
179     return atan2(v.y, v.x);
180     //(-pi, pi]
181 }
182 int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
183     double d = Len(C1.c - C2.c);
184     if(dcmp(d) == 0){
185         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
186         return 0; //two circles share identical center
187     }
188     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
189     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
190     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
191     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
192     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
193     sol.pb(p1);
194     if(p1 == p2) return 1;
195     sol.pb(p2);
196     return 2;
197 }
198 int GetPointCircleTangents(Point p, Circle C, Vector* v){
199     Vector u = C.c - p;
200     double dist = Len(u);
201     if(dist < C.r) return 0;//p is inside the circle, no tangents
202     else if(dcmp(dist - C.r) == 0){
203         // p is on the circles, one tangent only
204         v[0] = Rotate(u, PI / 2);
205         return 1;
206     }else{
207         double ang = asin(C.r / dist);
208         v[0] = Rotate(u, -ang);
209         v[1] = Rotate(u, +ang);
210         return 2;
211     }
212 }
213 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
214     //a[i] store point of tangency on Circle A of tangent i
215     //b[i] store point of tangency on Circle B of tangent i
216     //six conditions is in consideration
217     int cnt = 0;
218     if(A.r < B.r) { swap(A, B); swap(a, b); }
219     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
220     int rdiff = A.r - B.r;
221     int rsum = A.r + B.r;
222     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
223     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
224     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
225     if(d2 == rdiff * rdiff){ // internal tangency
226         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
227         return 1;
228     }
229     double ang = acos((A.r - B.r) / sqrt(d2));
230     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
231     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
232     if(d2 == rsum * rsum){
233         //one internal tangent
234         a[cnt] = A.point(base);
235         b[cnt++] = B.point(base + PI);
236     }else if(d2 > rsum * rsum){
237         //two internal tangents
238         double ang = acos((A.r + B.r) / sqrt(d2));
239         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
240         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
241     }
242     return cnt;
243 }
244 Point ReadPoint(){
245     double x, y;
246     scanf("%lf%lf", &x, &y);
247     return Point(x, y);
248 }
249 Circle ReadCircle(){
250     double x, y, r;
251     scanf("%lf%lf%lf", &x, &y, &r);
252     return Circle(x, y, r);
253 }
254 //Here goes 3d geometry templates
255 struct Point3{
256     double x, y, z;
257     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
258 };
259 typedef Point3 Vector3;
260 Vector3 operator + (Vector3 A, Vector3 B){
261     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
262 }
263 Vector3 operator - (Vector3 A, Vector3 B){
264     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
265 }
266 Vector3 operator * (Vector3 A, double p){
267     return Vector3(A.x * p, A.y * p, A.z * p);
268 }
269 Vector3 operator / (Vector3 A, double p){
270     return Vector3(A.x / p, A.y / p, A.z / p);
271 }
272 double Dot3(Vector3 A, Vector3 B){
273     return A.x * B.x + A.y * B.y + A.z * B.z;
274 }
275 double Len3(Vector3 A){
276     return sqrt(Dot3(A, A));
277 }
278 double Angle3(Vector3 A, Vector3 B){
279     return acos(Dot3(A, B) / Len3(A) / Len3(B));
280 }
281 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
282     return abs(Dot3(p - p0, n));
283 }
284 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
285     return p - n * Dot3(p - p0, n);
286 }
287 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
288     Vector3 v = p2 - p1;
289     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
290     return p1 + v * t;//if t in range [0, 1], intersection on segment
291 }
292 Vector3 Cross(Vector3 A, Vector3 B){
293     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
294 }
295 double Area3(Point3 A, Point3 B, Point3 C){
296     return Len3(Cross(B - A, C - A));
297 }
298 class cmpt{
299 public:
300     bool operator () (const int &x, const int &y) const{
301         return x > y;
302     }
303 };
304 
305 int Rand(int x, int o){
306     //if o set, return [1, x], else return [0, x - 1]
307     if(!x) return 0;
308     int tem = (int)((double)rand() / RAND_MAX * x) % x;
309     return o ? tem + 1 : tem;
310 }
311 ////////////////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////////////////
313 void data_gen(){
314     srand(time(0));
315     freopen("in.txt", "w", stdout);
316     int times = 100;
317     printf("%d
", times);
318     while(times--){
319         int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
320         int b = Rand(r, 1), d = Rand(r, 1);
321         int m = Rand(100, 1), n = Rand(m, 1);
322         printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
323     }
324 }
325 
326 struct cmpx{
327     bool operator () (int x, int y) { return x > y; }
328 };
329 int debug = 1;
330 int dx[] = {0, 0, 1, 1};
331 int dy[] = {1, 0, 0, 1};
332 //-------------------------------------------------------------------------
333 const int maxn = 1e5 + 10;
334 ll fac[maxn];
335 int pointer;
336 ll p[100];
337 int k;
338 int cnt[100];
339 pair<ll, pll> ans[maxn];
340 int pt;
341 ll n;
342 int S = 29;
343 
344 ll multi(ll a, ll b, ll c){
345     a %= c, b %= c;
346     ll res = 0;
347     while(b){
348         if(b & 1) res += a, res %= c;
349         a <<= 1;
350         if(a >= c) a %= c;
351         b >>= 1;
352     }
353     return res;
354 }
355 ll power(ll x, ll n, ll mod){
356     if(n == 1) return x % mod;
357     x %= mod;
358     ll tmp = x;
359     ll res = 1;
360     while(n){
361         if(n & 1) res = multi(res, tmp, mod);
362         tmp = multi(tmp, tmp, mod);
363         n >>= 1;
364     }
365     return res;
366 }
367 bool check(ll a, ll n, ll x, ll t){
368     ll res = power(a, x, n);
369     ll last = res;
370     FOR(i, 1, t){
371         res = multi(res, res, n);
372         if(res == 1 && last != 1 && last != n - 1) return 1;
373         last = res;
374     }
375     if(res != 1) return 1;
376     return 0;
377 }
378 bool miller_rabin(ll n){
379     if(n < 2) return 0;
380     if(n == 2) return 1;
381     if((n & 1) == 0) return 0;
382     ll x = n - 1;
383     ll t = 0;
384     while((x & 1) == 0) x >>= 1, ++t;
385     FOR(i, 0, S - 1){
386         ll a = rand() % (n - 1) + 1;
387         if(check(a, n, x, t)) return 0;
388     }
389     return 1;
390 }
391 ll gcd(ll a, ll b){
392     if(a == 0) return 1;
393     if(a < 0) return gcd(-a, b);
394     while(b){
395         ll t = a % b;
396         a = b;
397         b = t;
398     }
399     return a;
400 }
401 ll pollard_rho(ll x, ll c){
402     ll i = 1, k = 2;
403     ll x0 = rand() % x;
404     ll y = x0;
405     while(true){
406         ++i;
407         x0 = (multi(x0, x0, x) + c) % x;
408         ll d = gcd(y - x0, x);
409         if(d != 1 && d != x) return d;
410         if(y == x0) return x;
411         if(i == k){
412             y = x0;
413             k += k;
414         }
415     }
416 }
417 void factorize(ll n){
418     if(miller_rabin(n)){
419         p[k++] = n;
420         return;
421     }
422     ll p = n;
423     while(p >= n) p = pollard_rho(p, rand() % (n - 1) + 1);
424     factorize(p);
425     factorize(n / p);
426 }
427 void dfs(int pos, ll cur){
428     if(pos == k){
429         fac[pointer++] = cur;
430         return;
431     }
432     ll tem = 1;
433     FOR(i, 0, cnt[pos]){
434         dfs(pos + 1, cur * tem);
435         tem *= p[pos];
436     }
437 }
438 void solve(){
439     srand(time(0));
440     pointer = k = pt = 0;
441     factorize(n);
442     //FOR(i, 0, k - 1) printf("%lld
", p[i]);
443     sort(p, p + k);
444     k = unique(p, p + k) - p;
445     ll tem = n;
446     clr(cnt, 0);
447     FOR(i, 0, k - 1) while(tem % p[i] == 0) ++cnt[i], tem /= p[i];
448     dfs(0, 1);
449     sort(fac, fac + pointer);
450     FOR(i, 0, pointer - 1){
451         int rear = i + 1;
452         while(rear < pointer && fac[rear] - fac[i] <= 25) ++rear;
453         FOR(j, i + 1, rear - 1) FOR(u, j + 1, rear - 1){
454             if(fac[u] > 1e6) continue;
455             ll tem = fac[i] / gcd(fac[i], fac[j]) * fac[j];
456             tem = tem / gcd(tem, fac[u]) * fac[u];
457             if(tem == n) ans[pt++] = mp(fac[i], mp(fac[j], fac[u]));
458         }
459     }
460 }
461 //-------------------------------------------------------------------------
462 int main(){
463     //data_gen(); return 0;
464     //C(); return 0;
465     debug = 0;
466     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
467     if(debug) freopen("in.txt", "r", stdin);
468     //freopen("in.txt", "w", stdout);
469     int kase = 0;
470     while(~scanf("%lld", &n) && n){
471         solve();
472         printf("Scenario %d:
", ++kase);
473         if(!pt) puts("Such bells don't exist");
474         else FOR(i, 0, pt - 1) printf("%lld %lld %lld
", ans[i].st, ans[i].nd.st, ans[i].nd.nd);
475         puts("");
476     }
477     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
478     return 0;
479 }
code:
原文地址:https://www.cnblogs.com/astoninfer/p/5670350.html