码迷,mamicode.com
首页 > 其他好文 > 详细

CodeChef November Challenge 2014

时间:2014-12-06 06:32:54      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

重点回忆下我觉得比较有意义的题目吧。水题就只贴代码了。

Distinct Characters Subsequence

水。

代码:

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <map>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <vector>
 9 #define pb push_back
10 #define mp make_pair
11 #define esp 1e-8
12 #define lson   l, m, rt<<1
13 #define rson   m+1, r, rt<<1|1
14 #define sz(x) ((int)((x).size()))
15 #define pb push_back
16 #define in freopen("solve_in.txt", "r", stdin);
17 #define out freopen("solve_out.txt", "w", stdout);
18 
19 #define bug(x) printf("Line : %u >>>>>>\n", (x));
20 #define inf 0x7f7f7f7f
21 using namespace std;
22 typedef long long LL;
23 typedef map<int, int> MPS;
24 typedef pair<int, int> PII;
25 
26 const int maxn = (int)1e5 + 100;
27 char s[maxn];
28 int vis[30];
29 
30 int main(){
31 
32     int n;
33     for(int i = scanf("%d", &n); i <= n; i++){
34         memset(vis, 0, sizeof vis);
35         scanf("%s", s);
36         int len = 0;
37         for(int i = 0; s[i]; i++) if(!vis[s[i]-a]){
38             vis[s[i]-a] = 1;
39             len++;
40         }
41         cout << len << endl;
42     }
43     return 0;
44 }
View Code

Let us construct palindrome

题意:给定一个字符串问能否删除一个字符之后剩下的字符连接起来构成回文串。

分析:可以从两端开始枚举,直到遇到第一对不相匹配的字符(如果完全匹配答案肯定是YES),然后考虑这对字符中去掉一个,两种情况下分别继续配对,看最后能不能形成回文串。

代码:

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <map>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <vector>
 9 #define pb push_back
10 #define mp make_pair
11 #define esp 1e-8
12 #define lson   l, m, rt<<1
13 #define rson   m+1, r, rt<<1|1
14 #define sz(x) ((int)((x).size()))
15 #define pb push_back
16 #define in freopen("solve_in.txt", "r", stdin);
17 #define out freopen("solve_out.txt", "w", stdout);
18 
19 #define bug(x) printf("Line : %u >>>>>>\n", (x));
20 #define inf 0x7f7f7f7f
21 using namespace std;
22 typedef long long LL;
23 typedef map<int, int> MPS;
24 typedef pair<int, int> PII;
25 
26 const int maxn = (int)1e5 + 100;
27 char s[maxn];
28 
29 
30 int main() {
31 
32     int T;
33     for(int t = scanf("%d", &T); t <= T; t++) {
34         scanf("%s", s);
35         int len = strlen(s);
36         int i = 0, j = len-1;
37         int ok = 1;
38         while(i < j) {
39             if(s[i] != s[j]) {
40                 ok = 0;
41                 break;
42             }
43             i++, j--;
44         }
45         if(ok) puts("YES");
46         else {
47             if(i == j-1) {
48                 puts("YES");
49                 continue;
50             } else {
51                 int ii = i, jj = j;
52                 if(s[i] == s[j-1]) {
53                     j--;
54                     ok = 1;
55                     while(i < j) {
56                         if(s[i] != s[j]) {
57                             ok = 0;
58                             break;
59                         }
60                         i++, j--;
61                     }
62                     if(ok) {
63                         puts("YES");
64                         continue;
65                     }
66                 }
67                 i = ii, j = jj;
68                 if(s[i+1] == s[j]) {
69                     i++;
70                     ok = 1;
71                     while(i < j) {
72                         if(s[i] != s[j]) {
73                             ok = 0;
74                             break;
75                         }
76                         i++, j--;
77                     }
78                     if(ok) {
79                         puts("YES");
80                         continue;
81                     }
82                 }
83                 puts("NO");
84             }
85         }
86     }
87     return 0;
88 }
View Code

Chef and Segment Game

题意:给定[0, X]的线段,然后首先切1次分成2等分,切2次将两份等分成4分,然后切4分,求第k次切下去时的x坐标。

分析:先考虑是位于2^i变2^i+1的等分过程中,然后再求是将2^i等分成2^i+1时第几次切下去。

代码:

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <map>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <vector>
 9 #define pb push_back
10 #define mp make_pair
11 #define esp 1e-8
12 #define lson   l, m, rt<<1
13 #define rson   m+1, r, rt<<1|1
14 #define sz(x) ((int)((x).size()))
15 #define pb push_back
16 #define in freopen("solve_in.txt", "r", stdin);
17 #define out freopen("solve_out.txt", "w", stdout);
18 
19 #define bug(x) printf("Line : %u >>>>>>\n", (x));
20 #define inf 0x7f7f7f7f
21 using namespace std;
22 typedef long long LL;
23 typedef map<int, int> MPS;
24 typedef pair<int, int> PII;
25 
26 const int maxn = 66;
27 int dig[maxn];
28 int getDig(LL x) {
29     int len = 0;
30     while(x) {
31         dig[len++] = x&1;
32         x >>= 1;
33     }
34     return len;
35 }
36 int main() {
37 
38     int T;
39     for(int t = scanf("%d", &T); t <= T; t++) {
40         double x;
41         LL k;
42         scanf("%lf%lld", &x, &k);
43         int len = getDig(k);
44         int ok = 1;
45         for(int i = 0; i < len; i++) if(dig[i] == 0) {
46                 ok = 0;
47                 break;
48             }
49         if(ok) {
50             printf("%.10f\n", x-1.0/pow(2.0,len)*x);
51         } else {
52             printf("%.10f\n", 1.0*x*((1.0*k-((1LL<<(len-1))-1.0))/pow(2.0, len-1)-1.0/pow(2.0, len)));
53         }
54     }
55     return 0;
56 }
View Code

Chef and Red Black Tree

题意:一棵无限深度的二叉树,每个结点一个颜色,(红或黑)树上相邻两点颜色不同,根节点颜色初始为黑色,编号为1,任意结点u的两个左右儿子编号为2*u, 2*u+1, 每次3种操作:

Qi x 翻转x的颜色;

Qr x y 询问x到y路径上红色结点的颜色;

Qb x y询问x到y路径上黑色结点的颜色。

分析:将x到y结点上相应结点个数变成求一个点到根节点上相应颜色的结点的个数,然后再减去重复路径上的。由于相邻结点颜色不同,因此,一个点到根节点上相应颜色结点个数就是l/2+1(如果该节点与根节点同色且l为奇数)

代码:

bubuko.com,布布扣
 1 #include <bits/stdc++.h>
 2 #define pb push_back
 3 #define mp make_pair
 4 #define esp 1e-8
 5 #define lowbit(x) ((x)&(-x))
 6 #define lson   l, m, rt<<1
 7 #define rson   m+1, r, rt<<1|1
 8 #define sz(x) ((int)((x).size()))
 9 #define pb push_back
10 #define in freopen("solve_in.txt", "r", stdin);
11 #define out freopen("solve_out.txt", "w", stdout);
12 
13 #define bug(x) printf("Line : %u >>>>>>\n", (x));
14 #define inf 0x0f0f7f7f
15 using namespace std;
16 typedef long long LL;
17 typedef pair<int, int> PII;
18 typedef map<int, LL> MPS;
19 int getDig(int x){
20     int len = 0;
21     while(x){
22         len++;
23         x >>= 1;
24     }
25     return len;
26 }
27 int main(){
28 
29     int q;
30     scanf("%d", &q);
31     char s[10];
32     int rt = 0;
33     while(q--){
34         scanf("%s", s);
35         if(s[1] == i){
36             rt ^= 1;
37         }
38         else {
39             int x, y, lx, ly;
40             scanf("%d%d", &x, &y);
41 //            cout << x << ‘ ‘ << y << endl;
42             lx = getDig(x);
43             ly = getDig(y);
44             int col = s[1] == r;
45             int ans = (lx+(col == rt))/2 + (ly+(col == rt))/2;
46             if(lx > ly){
47                 while(lx != ly) x >>= 1, lx--;
48             }else{
49                 while(lx != ly) y >>= 1, ly--;
50             }
51             while(x != y) x >>= 1, y >>= 1;
52             lx = getDig(x);
53             ans -= (lx + (col == rt))/2*2;
54             ans += (col == rt ? lx&1 : !(lx&1));
55             cout << ans << endl;
56         }
57 
58     }
59     return 0;
60 }
View Code

 

 

Fombinatorial

题意:定义f(i) = 1^1*2^2*3^3......i^i,求g(r) = f(n)/(f(r)*f(n-r)) mod(m)保证g(r)结果为整数。

分析:

m不一定为质数,所以题目就不能直接利用逆元了,因为逆元的前提是两个数互质,这里f(r),f(n-r)不一定互质,所以我们关键就是在于把f(r)与m互质,将f(r)和f(n-r)中包含的m的质因子全部提取出来,剩下来的一定就互质了,利用逆元处理,然后乘上质因子部分。

代码:

bubuko.com,布布扣
  1 #include <bits/stdc++.h>
  2 #define pb push_back
  3 #define mp make_pair
  4 #define esp 1e-12
  5 #define lowbit(x) ((x)&(-x))
  6 #define lson   l, m, rt<<1
  7 #define rson   m+1, r, rt<<1|1
  8 #define sz(x) ((int)((x).size()))
  9 #define pb push_back
 10 #define pf(x) ((x)*(x))
 11 
 12 #define pi acos(-1.0)
 13 
 14 #define in freopen("solve_in.txt", "r", stdin);
 15 #define out freopen("solve_out.txt", "w", stdout);
 16 
 17 #define bug(x) printf("Line : %u >>>>>>\n", (x));
 18 #define inf 0x0f0f0f0f
 19 using namespace std;
 20 typedef long long LL;
 21 typedef pair<int, int> PII;
 22 const int maxn = (int)1e5 + 100;
 23 int N, M, Q;
 24 
 25 LL f[maxn], ppow[maxn];
 26 int phi;
 27 
 28 
 29 
 30 inline LL powmod(LL a, LL b, LL c) {
 31     LL res = 1;
 32     while(b) {
 33         if(b&1) res = res*a%c;
 34         a = a*a%c;
 35         b >>= 1;
 36     }
 37     return res;
 38 }
 39 
 40 inline LL getNum(int x, int p) {
 41     LL t = p;
 42     LL res = 0;
 43     while(t <= (LL)x) {
 44         int tmp = x/t;
 45         res += (LL)(tmp+1)*tmp/2*t;
 46         t *= p;
 47     }
 48     return res;
 49 }
 50 vector<int> primes;
 51 
 52 bool isPrime(int x) {
 53     for(int i = 0; i < sz(primes); i++)
 54         if(x%primes[i] == 0) return false;
 55     return true;
 56 }
 57 void init() {
 58     for(int i = 2; i < 100000; i++)
 59         if(isPrime(i)) primes.pb(i);
 60 }
 61 void getFac(int m, vector<int> &pfac) {
 62     pfac.clear();
 63     phi = m;
 64     for(int i = 0; i < sz(primes) && m/primes[i] >= primes[i]; i++) {
 65         if(m%primes[i] == 0) {
 66             phi -= phi/primes[i];
 67             pfac.pb(primes[i]);
 68             while(m%primes[i] == 0)
 69                 m /= primes[i];
 70         }
 71     }
 72     if(m != 1) {
 73         pfac.pb(m);
 74         phi -= phi/m;
 75     }
 76 }
 77 
 78 int main() {
 79 //    in
 80     cin.sync_with_stdio(0);
 81     cout.setf(ios::fixed, ios::floatfield);
 82     cout.precision(6);
 83     int T;
 84     init();
 85     vector<int> pfac;
 86 
 87     for(int t = scanf("%d", &T); t <= T; t++) {
 88         scanf("%d%d%d", &N, &M, &Q);
 89         pfac.clear();
 90         getFac(M, pfac);
 91         f[1] = 1%M;
 92 
 93         for(int i = 2; i <= N; i++) {
 94             int x = i;
 95             for(int j = 0; j < sz(pfac) && x >= pfac[j]; j++) {
 96                 if(x % pfac[j] == 0) {
 97                     while(x%pfac[j] == 0)
 98                         x /= pfac[j];
 99                 }
100             }
101             f[i] = f[i-1]*powmod(x, i, M)%M;
102         }
103         while(Q--) {
104             int r;
105             scanf("%d", &r);
106             if(M == 1)
107                 printf("0\n");
108             else {
109                 int res = f[N];
110                 res = 1LL*res*powmod(f[r], phi-1, M)%M*powmod(f[N-r], phi-1, M)%M;
111                 for(int i = 0; i < sz(pfac) && pfac[i] <= N; i++) {
112                     LL num = getNum(N, pfac[i])-getNum(r, pfac[i])-getNum(N-r, pfac[i]);
113                     res = 1LL*res*powmod(pfac[i], num, M)%M;
114                 }
115                 printf("%d\n", res);
116             }
117         }
118     }
119     return 0;
120 }
View Code

当然还可以用线段树预处理所有可能的答案啦,因为g(r) = n^n*(n-1)^(n-1)*.....(n-r+1)/(r^r*(r-1)^(r-1)*....2^2*1^1)。

每次处理完g(r)之后处理,下一个答案时,相当于乘以(n-r)^(n-r)/(r+1)^(r+1),我们对分子分母处理一下所含质因子的幂次,更新线段树。

代码:

bubuko.com,布布扣
  1 #include <bits/stdc++.h>
  2 #define pb push_back
  3 #define mp make_pair
  4 #define esp 1e-12
  5 #define lowbit(x) ((x)&(-x))
  6 #define lson   l, m, rt<<1
  7 #define rson   m+1, r, rt<<1|1
  8 
  9 #define sz(x) ((int)((x).size()))
 10 #define pb push_back
 11 #define pf(x) ((x)*(x))
 12 
 13 #define pi acos(-1.0)
 14 
 15 #define in freopen("solve_in.txt", "r", stdin);
 16 #define out freopen("solve_out.txt", "w", stdout);
 17 
 18 #define bug(x) printf("Line : %u >>>>>>\n", (x));
 19 #define inf 0x0f0f0f0f
 20 using namespace std;
 21 typedef long long LL;
 22 typedef pair<int, int> PII;
 23 const int maxn = (int)1e5 + 100;
 24 int cnt[maxn], pa[maxn], vis[maxn], id[maxn];
 25 int num;
 26 vector<int> primes;
 27 
 28 LL powmod(LL a, LL b, LL c) {
 29     LL res = 1;
 30     while(b > 0) {
 31         if(b&1) res = res*a%c;
 32         a = a*a%c;
 33         b >>= 1;
 34     }
 35     return res;
 36 }
 37 
 38 class SegmentTree {
 39 private:
 40     vector<LL>power;
 41     vector<int> tree;
 42     int mod;
 43 public:
 44     SegmentTree(int n, int m) {
 45         mod = m;
 46         n = lower_bound(primes.begin(), primes.end(), n)-primes.begin();
 47         n++;
 48         power.resize(n, 0);
 49         tree.resize(n*4, 1%m);
 50     }
 51     void update(int pos, LL c) {
 52         power[pos] += c;
 53         update(0, sz(power)-1, 1, pos, powmod(primes[pos], power[pos], mod));
 54     }
 55     void update(int l, int r, int rt, int pos, int c) {
 56         if(l == r) {
 57             tree[rt] = c;
 58             return;
 59         }
 60         int m = (l+r)>>1;
 61         if(pos <= m)
 62             update(lson, pos, c);
 63         else update(rson, pos, c);
 64         tree[rt] = 1LL*tree[rt<<1]*tree[rt<<1|1]%mod;
 65     }
 66     int val() {
 67         return tree[1];
 68     }
 69 };
 70 void pre() {
 71     memset(id, 0x0f, sizeof id);
 72     id[1] = maxn;
 73     cnt[1] = 0;
 74     pa[1] = 1;
 75     num = 0;
 76 
 77     for(int i = 2; i < maxn; i++) if(!vis[i]) {
 78             cnt[i] = 1;
 79             pa[i] = 1;
 80             id[i] = num;
 81             primes.pb(i);
 82             for(int j = i*2; j < maxn; j += i) {
 83                 if(vis[j]) continue;
 84                 vis[j] = 1;
 85                 id[j] = num;
 86 
 87                 if(id[j/i] == num) {
 88                     pa[j] = pa[j/i];
 89                     cnt[j] = cnt[j/i] + 1;
 90                 } else {
 91                     pa[j] = j/i;
 92                     cnt[j] = 1;
 93                 }
 94             }
 95             num++;
 96         }
 97 //        bug(pa[4])
 98 }
 99 int n, m, q;
100 int ans[maxn];
101 
102 int main() {
103 
104     int T;
105     pre();
106 //    bug(1)
107     for(int t = scanf("%d", &T); t <= T; t++) {
108         scanf("%d%d%d", &n, &m, &q);
109         SegmentTree tree(n ,m);
110         for(int i = 1; i <= n/2; i++) {
111             int p = n-i+1, q = i;
112 
113             while(p != 1 || q != 1) {
114                 int x = id[p];
115 //                 bug(i)
116                 int y = id[q];
117                 LL ct = 0;
118                 x = min(x, y);
119                 if(x == id[p]) {
120                     ct += 1LL*cnt[p]*(n-i+1);
121                     p = pa[p];
122                 }
123                 if(x == id[q]) {
124                     ct -= 1LL*cnt[q]*i;
125                     q = pa[q];
126                 }
127 //                cout << p << q << endl;
128                     tree.update(x, ct);
129             }
130             ans[i] = tree.val();
131         }
132         while(q--) {
133             int r;
134             scanf("%d", &r);
135             r = min(r, n-r);
136             printf("%d\n", ans[r]);
137         }
138     }
139     return 0;
140 }
View Code

 

Chef and Churu

题意:和区间询问有关,不过每次询问的一系列区间的和。简单来说就是这样,给定n个函数,每个函数询问Li到Ri的区间内数的和。有2中操作,操作1改变某个数的值,操作2询问函数m到n的和。

分析:询问一系列区间的和,可以分块处理,将函数分成L块, L = sqrt(n),每一块的大小也是L。预处理每块内的函数所包含的每个元素的次数,算出初始时该块内函数的和,每次更新数的值时,只需要针对每个块内该元素包含的次数进行更新。询问时,包含的整块函数值的和直接加到结果中,对于不完全包含的函数块,只能查询相应区间的和了,这里似乎只能用树状数组进行更新查询,线段树常数过大会TLE。

代码:

bubuko.com,布布扣
  1 #include <bits/stdc++.h>
  2 #define pb push_back
  3 #define mp make_pair
  4 #define esp 1e-12
  5 #define lowbit(x) ((x)&(-x))
  6 #define lson   l, m, rt<<1
  7 #define rson   m+1, r, rt<<1|1
  8 #define sz(x) ((int)((x).size()))
  9 #define pb push_back
 10 #define pf(x) ((x)*(x))
 11 
 12 #define pi acos(-1.0)
 13 
 14 #define in freopen("solve_in.txt", "r", stdin);
 15 #define out freopen("solve_out.txt", "w", stdout);
 16 
 17 #define bug(x) printf("Line : %u >>>>>>\n", (x));
 18 #define inf 0x0f0f0f0f
 19 using namespace std;
 20 typedef unsigned long long LL;
 21 typedef pair<int, int> PII;
 22 const int maxn = (int)1e5 + 100;
 23 const int maxm = 350;
 24 
 25 PII func[maxn];
 26 int a[maxn], cnt[maxm][maxn], num[maxm][maxn];
 27 LL ans[maxm], sum[maxn];
 28 
 29 int L, n, q;
 30 
 31 
 32 void add(int x, int c) {
 33     while(x <= n) {
 34         sum[x] += c;
 35         x += lowbit(x);
 36     }
 37 }
 38 void update(int x, int c){
 39     add(x, c-a[x]);
 40     a[x] = c;
 41 }
 42 LL query(int x) {
 43     LL res = 0;
 44     while(x > 0) {
 45         res += sum[x];
 46         x -= lowbit(x);
 47     }
 48     return res;
 49 }
 50 LL query(int l, int r){
 51     return query(r)-query(l-1);
 52 }
 53 void build(){
 54     for(int i = 1; i <= n; i++){
 55         add(i, a[i]);
 56     }
 57 }
 58 void pre() {
 59     build();
 60     for(int i = 0; i < L; i++) {
 61         for(int j = 1; j <= n; j++) {
 62             num[i][j] = num[i][j-1] + cnt[i][j];
 63             ans[i] += 1ULL*num[i][j]*a[j];
 64         }
 65     }
 66 }
 67 int main() {
 68 
 69     scanf("%d", &n);
 70     L = (int)sqrt(n) + 1;
 71     for(int i = 1; i <= n; i++)
 72         scanf("%d", a+i);
 73     for(int i = 0; i < n; i++) {
 74         int l, r;
 75         scanf("%d%d", &l, &r);
 76         func[i] = PII(l, r);
 77         cnt[i/L][l]++;
 78         cnt[i/L][r+1]--;
 79     }
 80     pre();
 81     scanf("%d", &q);
 82     while(q--) {
 83         int type;
 84         int l, r;
 85         scanf("%d%d%d", &type, &l, &r);
 86         if(type == 1) {
 87             for(int i = 0; i < L; i++) {
 88                 ans[i] += 1ULL*num[i][l]*(r-a[l]);
 89             }
 90             update(l, r);
 91         } else {
 92             l--, r--;
 93             int x = l/L;
 94             int y = r/L;
 95             LL res = 0;
 96             if(x == y && y-x+1 != L) {
 97                 while(l <= r) {
 98                     res += query(func[l].first, func[l].second);
 99                     l++;
100                 }
101             } else {
102                 int ll = (l%L ? x+1 : x);
103                 int rr = ((r+1)%L ? y-1 : y);
104                 for(int i = ll; i <= rr; i++)
105                     res += ans[i];
106                 while(l%L) {
107                     res += query(func[l].first, func[l].second);
108                     l++;
109                 }
110                 while((r+1)%L) {
111                     res += query(func[r].first, func[r].second);
112                     r--;
113                 }
114             }
115             printf("%llu\n", res);
116         }
117     }
118     return 0;
119 }
View Code

 

 

题意:

Sereja and Permutations

题意:

一个排列p,每次去掉其中一个元素,然后将剩下的数中,所有大于去掉元素的数减1,这样可以得到一个n-1的排列,进行n次可以得到q[1],q[2],........q[n].

现在任意顺序给定n个排列表示q[i],求原来排列p。

分析:显然是一个简单问题的逆问题,但似乎也不是很难做诶,考虑任意给定的一个q,由于不知道他是去掉哪个数后得到的,也不知道去掉的数的位置,所以枚举共有n^2种情况,然后检测得到的p‘是不是满足条件的,也就是做一遍原来的操作,能否得到对应的q,由于q[i]可能有重复,所以要记录得到的个数,最后要求不同的q的个数对应相等。

代码:

bubuko.com,布布扣
  1 #include <bits/stdc++.h>
  2 #define pb push_back
  3 #define mp make_pair
  4 #define esp 1e-12
  5 #define lowbit(x) ((x)&(-x))
  6 #define lson   l, m, rt<<1
  7 #define rson   m+1, r, rt<<1|1
  8 
  9 #define sz(x) ((int)((x).size()))
 10 #define pb push_back
 11 #define pf(x) ((x)*(x))
 12 
 13 #define pi acos(-1.0)
 14 
 15 #define in freopen("solve_in.txt", "r", stdin);
 16 #define out freopen("solve_out.txt", "w", stdout);
 17 
 18 #define bug(x) printf("Line : %u >>>>>>\n", (x));
 19 #define inf 0x0f0f0f0f
 20 using namespace std;
 21 typedef long long LL;
 22 typedef unsigned long long ULL;
 23 typedef pair<int, int> PII;
 24 typedef map<ULL, int> MPS;
 25 
 26 const int maxn = 333;
 27 const int B = 1000000007;
 28 
 29 int a[maxn][maxn];
 30 int num[maxn], ct[maxn];
 31 
 32 MPS mps;
 33 int cnt;
 34 
 35 int id(ULL x) {
 36     return mps[x];
 37 }
 38 int ID(ULL x) {
 39     if(mps.count(x) == false) {
 40         mps[x] = cnt++;
 41     }
 42     num[mps[x]]++;
 43     return mps[x];
 44 }
 45 int main() {
 46 
 47     int T;
 48     for(int t = scanf("%d", &T); t <= T; t++) {
 49         int n;
 50         scanf("%d", &n);
 51         cnt = 0;
 52         memset(num, 0, sizeof num);
 53         mps.clear();
 54         for(int i = 1; i <= n; i++) {
 55             ULL Hash = 0;
 56             for(int j = 1; j < n; j++) {
 57                 scanf("%d", &a[i][j]);
 58                 Hash = Hash*B + a[i][j];
 59             }
 60             ID(Hash);
 61         }
 62         int ok = 1;
 63 
 64         for(int i = 1; i <= n; i++) {
 65             for(int j = 1; j <= n; j++) {
 66                 vector<int> vec;
 67                 for(int k = 1; k < n; k++) {
 68                     int x = a[1][k];
 69                     if(x >= i) x++;
 70                     if(j == k)
 71                         vec.pb(i);
 72                     vec.pb(x);
 73                 }
 74                 if(j == n) vec.pb(i);
 75                 ok = 1;
 76                 memset(ct, 0, sizeof ct);
 77                 for(int k = 0; k < n; k++) {
 78                     ULL ha = 0;
 79                     for(int kk = 0; kk < n; kk++) {
 80                         if(kk == k) continue;
 81                         int x = vec[kk];
 82                         if(x > vec[k]) x--;
 83                         ha = ha*B + x;
 84                     }
 85                     if(mps.count(ha) == false) {
 86                         ok = 0;
 87                         break;
 88                     }
 89                     ct[id(ha)]++;
 90                 }
 91                 if(!ok) continue;
 92                 for(int k = 0; k < cnt; k++) {
 93                     if(num[k] != ct[k]) {
 94                         ok = 0;
 95                         break;
 96                     }
 97                 }
 98                 if(ok) {
 99                     for(int k = 0; k < sz(vec); k++)
100                         printf("%d%c", vec[k], k == sz(vec)-1 ? \n :  );
101                     break;
102                 }
103             }
104             if(ok) break;
105         }
106     }
107     return 0;
108 }
View Code

Chef and Words

题意:给定一个字符串,然后是一个矩阵表示字符i变成j的概率是a[i][j]最后一些字符串。每次每个字符都会变化一次,k次后,由原始字符串变成其中一个目标字符串的概率.

分析:先预处理一下i经过k步变成j的概率pi,j,考虑原始串变成目标串的概率,就是各个位置的字符i经过k次后变成目标串对应位置字符j的概率pi, j的乘积,而最终答案就是变成各个不同目标串的概率的和。

bubuko.com,布布扣
  1 #include <bits/stdc++.h>
  2 #define pb push_back
  3 #define mp make_pair
  4 #define esp 1e-8
  5 #define lson   l, m, rt<<1
  6 #define rson   m+1, r, rt<<1|1
  7 #define sz(x) ((int)((x).size()))
  8 #define pb push_back
  9 #define in freopen("solve_in.txt", "r", stdin);
 10 #define out freopen("solve_out.txt", "w", stdout);
 11 
 12 #define bug(x) printf("Line : %u >>>>>>\n", (x));
 13 #define inf 0x7f7f7f7f
 14 using namespace std;
 15 typedef long long LL;
 16 typedef pair<int, int> PII;
 17 typedef map<string, int> MPS;
 18 typedef long double LD;
 19 MPS mps;
 20 const int maxn = 30;
 21 
 22 
 23 struct Matrix {
 24     int n, m;
 25     double a[maxn][maxn];
 26     Matrix() {}
 27     Matrix(int n, int m):n(n), m(m) {
 28         memset(a, 0, sizeof a);
 29     }
 30 };
 31 double p[maxn][maxn];
 32 vector<string> word;
 33 int n, k;
 34 char str[10];
 35 char tmp[10];
 36 int cnt;
 37 Matrix operator * (Matrix &a, Matrix &b) {
 38     Matrix c;
 39     c.n = a.n;
 40     c.m = b.m;
 41     for(int i = 0; i < c.n; i++) for(int j = 0; j < c.m; j++) {
 42             c.a[i][j] = 0.0;
 43             for(int k = 0; k < a.m; k++)
 44                 c.a[i][j] += a.a[i][k]*b.a[k][j];
 45 //                cout << c.a[i][j] << endl;
 46 //            if(fabs(c.a[i][j]) < 1e-8)
 47 //                c.a[i][j] = 0.0;
 48         }
 49     return c;
 50 }
 51 bool idx(string str) {
 52     if(mps.count(str) == false) {
 53         mps[str] = ++cnt;
 54         return true;
 55     }
 56     return false;
 57 }
 58 int ID(char ch) {
 59     return ch - a;
 60 }
 61 
 62 int main() {
 63 
 64     int T;
 65     Matrix E0(26, 26);
 66     for(int i = 0; i < maxn; i++)
 67         for(int j = 0; j < maxn; j++) E0.a[i][j] = i == j ? 1.0 : 0.0;
 68 
 69     for(int t = scanf("%d", &T); t <= T; t++) {
 70         mps.clear();
 71         word.clear();
 72         cnt = 0;
 73         scanf("%d%d", &n, &k);
 74         scanf("%s", str);
 75         int len = strlen(str);
 76         for(int i = 0; i < 26; i++) for(int j = 0; j < 26; j++)
 77                 scanf("%lf", &p[i][j]);
 78 //                cin >> p[i][j];
 79 
 80         for(int i = 0; i < n; i++) {
 81             scanf("%s", tmp);
 82             if(strlen(tmp) != len) continue;
 83             if(idx(tmp)) {
 84                 word.pb(tmp);
 85             }
 86         }
 87         Matrix res = E0, c(26, 26);
 88 
 89         for(int i = 0; i < 26; i++) for(int j = 0; j < 26; j++)
 90                 c.a[i][j] = p[j][i];
 91 //                cout << k << endl;
 92         while(k) {
 93             if(k&1) res = res*c;
 94             c = c*c;
 95             k >>= 1;
 96         }
 97 //        for(int i = 0; i < 26; i++) for(int j = 0; j < 26; j++)
 98 //        memset(clap, 0, sizeof clap);
 99 //        for(int i = 0; i < 26; i++)
100 //            for(int j = 0; j < 26; j++)
101 //            clap[i] += res.a[j][i];
102         double ans = .0;
103         for(int i = 0; i < sz(word); i++) {
104 //            cout << word[i] << endl;
105             double tt = 1.0;
106             for(int j = 0; j < len; j++) {
107                 int u = ID(str[j]);
108                 int v = ID(word[i][j]);
109 //                cout << res.a[v][u] << endl;
110                 tt *= res.a[v][u];
111             }
112             ans += tt;
113         }
114 
115         printf("%.10f\n", ans);
116     }
117     return 0;
118 }
View Code

 

CodeChef November Challenge 2014

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/rootial/p/4147746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!