锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久综合综合久久97色,人人狠狠综合久久亚洲,久久精品夜色噜噜亚洲A∨ http://www.shnenglu.com/y346491470/category/17821.html涓烘ⅵ鎯寵屽鏂楋紒zh-cnTue, 07 Aug 2012 16:51:22 GMTTue, 07 Aug 2012 16:51:22 GMT60hdoj 4050 wolf5x - 鏈熸湜 + dphttp://www.shnenglu.com/y346491470/articles/185087.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesWed, 25 Jul 2012 15:46:00 GMThttp://www.shnenglu.com/y346491470/articles/185087.htmlhttp://www.shnenglu.com/y346491470/comments/185087.htmlhttp://www.shnenglu.com/y346491470/articles/185087.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/185087.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/185087.html璺蟲牸瀛愶紝宸﹀彸鑴氳疆嫻佽煩錛屼笉鑳藉線鍥炶煩錛屾瘡嬈¤煩鏈変釜[A , B]姝ラ暱鑼冨洿銆?/span>姣忎釜鏍煎瓙鍙兘鍑虹幇鍥涚鐘舵侊紝鍙彸鑴氳繘錛屽乏鑴氳繘錛屽弻鑴氳繘錛屼笉鍑嗚繘銆傛瘡縐嶇姸鎬侀兘鏈変竴瀹氭鐜囥傛瘡嬈″彧鑳借煩鍒版渶榪戝彲琛岀殑鏍煎瓙銆傚綋璺沖嚭浜唍涓牸瀛愭垨鑰呬笉鑳藉姩鏃舵湡鏈涙鏁般?br />
銆愰瑙c戯細鏈熸湜綾婚鐩紝鏃犵幆錛岀洿鎺p銆?br />              鍒╃敤鍏ㄦ湡鏈涘叕寮忥紝娉ㄦ剰鐘舵佹灇涓炬椂瑕佷箻涓婃潯浠舵鐜囥傜劧鍚庢寜甯歌鏈熸湜鍋氭硶鍗沖彲錛屽掔潃鎺ㄣ傜瓟妗堜負dp[0][3]銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 #include "set"
13 #include "utility"
14 using namespace std;
15 typedef pair<intint> pii;
16 #define pb push_back
17 #define mp make_pair
18 #define fi first
19 #define se second
20 #define sof(x) sizeof(x)
21 #define lc(x) (x << 1)
22 #define rc(x) (x << 1 | 1)
23 #define lowbit(x) (x & (-x))
24 #define ll long long
25 
26 double dp[4010][4];
27 double p[4010][4];
28 int n, a, b;
29 int main() {
30     int T;
31     scanf("%d", &T);
32     while(T--) {
33         scanf("%d%d%d", &n, &a, &b);
34         memset(p, 0, sizeof(p));
35         memset(dp, 0, sizeof(dp));
36         for(int i = 1; i <= n; i++)
37             scanf("%lf%lf%lf%lf", &p[i][0], &p[i][1], &p[i][2], &p[i][3]);
38         for(int i = n + 1; i <= n + a; i++) p[i][3] = 1.0;
39         double c = 1.0;
40         for(int i = n; i >= 0; i--) {
41             for(int j = 1; j < 4; j++) {
42                 double pp = 1.0;
43                 for(int k = a; k <= b; k++) {
44                     if(j == 1) {
45                         dp[i][1] += pp * ((dp[i+k][2] + c) * p[i+k][2] + (dp[i+k][3] + c) * p[i+k][3]);
46                         pp *= (p[i+k][0] + p[i+k][1]);
47                     } else if(j == 2) {
48                         dp[i][2] += pp * ((dp[i+k][1] + c) * p[i+k][1] + (dp[i+k][3] + c) * p[i+k][3]);
49                         pp *= (p[i+k][0] + p[i+k][2]);
50                     } else if(j == 3) {
51                         dp[i][3] += pp * ((dp[i+k][1] + c) * p[i+k][1] + (dp[i+k][2] + c) * p[i+k][2] + (dp[i+k][3] + c) * p[i+k][3]);
52                         pp *= (p[i+k][0]);
53                     }
54                 }
55             }
56         }
57         printf("%.6f\n", dp[0][3]);
58     }
59     return 0;
60 }
61 


y @ The Angry Teletubbies 2012-07-25 23:46 鍙戣〃璇勮
]]>
hdoj 4219 Randomization - 鏍慸p + 姒傜巼http://www.shnenglu.com/y346491470/articles/184744.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesMon, 23 Jul 2012 08:23:00 GMThttp://www.shnenglu.com/y346491470/articles/184744.htmlhttp://www.shnenglu.com/y346491470/comments/184744.htmlhttp://www.shnenglu.com/y346491470/articles/184744.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/184744.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/184744.html緇欏畾涓媯墊爲錛岀粰瀹氳竟鏉冪殑鍖洪棿[0,L]錛屾眰鏈緇堟爲鐨勬渶闀胯礬<=S鐨勬鐜囷紱 

銆愰瑙c戯細姝﹀ぇ鏍¤禌鐨勬爲dp錛屾斁浜嗗ソ涔咃紝鐪嬬潃棰樿В鍕夊己榪囦簡銆傚彧鍙互璇存垜鐨勬爲dp澶按浜嗭紝榪樺仠鐣欏湪鑳屽寘鐨勯樁孌點?br />              璁劇姸鎬乨p[i][j]琛ㄧず浠涓烘牴鐨勫瓙鏍戯紝鍏跺彾瀛愮粨鐐瑰埌i鐨勬渶闀胯窛紱諱負j涓旇瀛愭爲鏈闀胯礬寰勪笉瓚呰繃s鐨勬鐜囥?br />              瀵逛簬u鐨勪竴涓瓙鏍憊錛屽厛鏋氫婦u榪瀡鐨勯暱搴︼紝鐒跺悗鍐嶆妸涔嬪墠鐨勫瓙鏍戝拰v榪欐潯閾懼悎騫訛紝濂藉鏉傦紝涓嶄細琛ㄨ揪浜嗭紝鐩存帴鐮旂┒浠g爜鍚с?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 #define maxn 70
22 #define maxs 530
23 int n, l, s;
24 double dp[maxn][maxs];
25 double son[maxs], tmp[maxs];
26 double e;
27 vector<int> vec[maxn];
28 void dfs(int u, int fa) {
29     memset(dp[u], 0, sizeof(dp[u]));
30     dp[u][0] = 1.0;
31     int size = vec[u].size();
32     for(int i = 0; i < size; i++) {
33         int v = vec[u][i];
34         if(v == fa) continue;
35         dfs(v, u);
36         memset(son, 0, sizeof(son));
37         memset(tmp, 0, sizeof(tmp));
38         for(int j = 0; j <= l; j++)
39             for(int k = 0; k <= s; k++)
40                 if(j + k <= s)
41                     son[j+k] += e * dp[v][k];
42         for(int j = 0; j <= s; j++)
43             for(int k = 0; k <= s - j; k++)
44                 tmp[max(j, k)] += son[j] * dp[u][k];
45         for(int j = 0; j <= s; j++)
46             dp[u][j] = tmp[j];
47     }
48 }
49 
50 int main() {
51     int T, Case = 1;
52     scanf("%d", &T);
53     while(T--) {
54         scanf("%d%d%d", &n, &l, &s);
55         for(int i = 0; i <= n; i++) vec[i].clear();
56         e = 1.0 / (1.0 + l);
57         for(int i = 1; i < n; i++) {
58             int a, b;
59             scanf("%d%d", &a, &b);
60             vec[a].pb(b);
61             vec[b].pb(a);
62         }
63         dfs(1, -1);
64         double ans = 0.0;
65         for(int i = 0; i <= s; i++) ans += dp[1][i];
66         printf("Case %d: %.6f\n", Case++, ans);
67     }
68     return 0;
69 }
70 


y @ The Angry Teletubbies 2012-07-23 16:23 鍙戣〃璇勮
]]>
zoj 3582 Back to the Past - 鏈熸湜 + dphttp://www.shnenglu.com/y346491470/articles/171996.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesThu, 19 Apr 2012 08:20:00 GMThttp://www.shnenglu.com/y346491470/articles/171996.htmlhttp://www.shnenglu.com/y346491470/comments/171996.htmlhttp://www.shnenglu.com/y346491470/articles/171996.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/171996.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/171996.html
銆愰瑙c戯細璁綿p[i][j]琛ㄧず宸﹁竟鏈塱涓礊浜紝鍙寵竟鏈塲涓礊浜鐩爣鐘舵佺殑鏈熸湜銆?br />               鐒跺悗鎵鏈塪p[ii][jj](ii >= i && jj >= j)閮芥槸dp[i][j]鐨勫悗緇х姸鎬侊紝鐢辨湡鏈涜綆楀叕寮忓彲浠ョ畻鍑篸p[i][j]鐨勬湡鏈涖?br />               榪欓噷杞Щ浼氭湁涓幆錛岀Щ欏瑰嵆鍙鐞嗘帀銆?br />               瑕佸厛棰勫鐞嗙粍鍚堟暟銆乸鐨勫箓鍜?1-p)鐨勫箓銆?br />               鏈鍚庣瓟妗堝嵆涓篸p[0][0].
               鐢ㄨ蹇嗗寲鎼滅儲鏉ュ啓闈炲父鏂逛究銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 #define maxn 110
22 double dp[55][55];
23 double p1[maxn], p2[maxn];
24 double C[55][55];
25 int n, m;
26 double p;
27 bool visit[55][55];
28 void init() {
29     for(int i = 0; i < 51; i++) C[i][0] = C[i][i] = 1;
30     for(int i = 2; i < 51; i++)
31         for(int j = 1; j < i; j++)
32             C[i][j] = C[i-1][j-1] + C[i-1][j];
33 }
34 
35 double dfs(int a, int b) {
36     double &tmp = dp[a][b];
37     if(visit[a][b]) return tmp;
38     visit[a][b] = true;
39     if(a >= m && b >= m) return tmp = 0.0;
40     tmp = 1.0;
41     for(int i = 0; i <= n - a; i++) {
42         for(int j = 0; j <= n - b; j++) {
43             if(i == 0 && j == 0) continue;
44             tmp += (dfs(a + i, b + j)) * p1[i+j] * p2[2 * n - a - b - i - j] * C[n-a][i] * C[n-b][j];
45         }
46     }
47     return tmp /= (1 - p2[2 * n - a - b]);
48 }
49 
50 int main() {
51     init();
52     while(~scanf("%d%d%lf", &n, &m, &p)) {
53         if(!n && !m) break;
54         p1[0] = p2[0] = 1.0;
55         for(int i = 1; i < maxn; i++) p1[i] = p1[i-1] * p;
56         for(int i = 1; i < maxn; i++) p2[i] = p2[i-1] * (1 - p);
57         memset(visit, falsesizeof(visit));
58         printf("%.6f\n", dfs(0, 0));
59     }
60     return 0;
61 }
62 


y @ The Angry Teletubbies 2012-04-19 16:20 鍙戣〃璇勮
]]>
uva 5811 Cards - 鏈熸湜 + dphttp://www.shnenglu.com/y346491470/articles/171977.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesThu, 19 Apr 2012 06:25:00 GMThttp://www.shnenglu.com/y346491470/articles/171977.htmlhttp://www.shnenglu.com/y346491470/comments/171977.htmlhttp://www.shnenglu.com/y346491470/articles/171977.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/171977.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/171977.html
銆愰瑙c戯細鏈熸湜dp錛屽鏋滀笉鑰冭檻joker灝卞彉寰楅潪甯哥畝鍗曘備絾鐜板湪joker鍙互鍙樻垚浠繪剰鑺辮壊錛屾墍浠ュ鏋滃綋鍓嶇炕鍒癹oker闇瑕佹灇涓捐繖涓猨oker鍙樻垚浠涔堣姳鑹詫紝鍙栨渶灝忕殑鏈熸湜鍗沖彲銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 int aa, bb, cc, dd;
22 double dp[16][16][16][16][5][5];
23 bool visit[16][16][16][16][5][5];
24 
25 double dfs(int a, int b, int c, int d, int big, int small) {
26     double &tmp = dp[a][b][c][d][big][small];
27     if(visit[a][b][c][d][big][small]) return tmp;
28     visit[a][b][c][d][big][small] = true;
29     int now[] = {a, b, c, d};
30     if(big != 4) now[big]++;
31     if(small != 4) now[small]++;
32     if(now[0] >= aa && now[1] >= bb && now[2] >= cc && now[3] >= dd) return tmp = 0.0;
33     tmp = 1.0;
34     double sum = now[0] + now[1] + now[2] + now[3];
35     if(a < 13) tmp += dfs(a + 1, b, c, d, big, small) * (13.0 - a) / (54.0 - sum);
36     if(b < 13) tmp += dfs(a, b + 1, c, d, big, small) * (13.0 - b) / (54.0 - sum);
37     if(c < 13) tmp += dfs(a, b, c + 1, d, big, small) * (13.0 - c) / (54.0 - sum);
38     if(d < 13) tmp += dfs(a, b, c, d + 1, big, small) * (13.0 - d) / (54.0 - sum);
39     if(big == 4) {
40         double tmp1 = 1e100;
41         tmp1 = min(tmp1, dfs(a, b, c, d, 0, small) / (54.0 - sum));
42         tmp1 = min(tmp1, dfs(a, b, c, d, 1, small) / (54.0 - sum));
43         tmp1 = min(tmp1, dfs(a, b, c, d, 2, small) / (54.0 - sum));
44         tmp1 = min(tmp1, dfs(a, b, c, d, 3, small) / (54.0 - sum));
45         tmp += tmp1;
46     }
47     if(small == 4) {
48         double tmp1 = 1e100;
49         tmp1 = min(tmp1, dfs(a, b, c, d, big, 0) / (54.0 - sum));
50         tmp1 = min(tmp1, dfs(a, b, c, d, big, 1) / (54.0 - sum));
51         tmp1 = min(tmp1, dfs(a, b, c, d, big, 2) / (54.0 - sum));
52         tmp1 = min(tmp1, dfs(a, b, c, d, big, 3) / (54.0 - sum));
53         tmp += tmp1;
54     }
55     return tmp;
56 }
57 
58 int main() {
59     int T, Case = 1;
60     cin >> T;
61     while(T--) {
62         cin >> aa >> bb >> cc >> dd;
63         int cnt = 0;
64         cnt += max(aa - 13, 0);
65         cnt += max(bb - 13, 0);
66         cnt += max(cc - 13, 0);
67         cnt += max(dd - 13, 0);
68         printf("Case %d: ", Case++);
69         if(cnt > 2) printf("-1.000\n");
70         else {
71             memset(visit, falsesizeof(visit));
72             printf("%.3f\n", dfs(0, 0, 0, 0, 4, 4));
73         }
74     }
75     return 0;
76 }
77 


y @ The Angry Teletubbies 2012-04-19 14:25 鍙戣〃璇勮
]]>
poj 2635 The Embarrassed Cryptographer - 綰挎х瓫绱犳暟 + 鍒嗗壊鏁板瓧姹傛āhttp://www.shnenglu.com/y346491470/articles/168388.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesTue, 20 Mar 2012 05:50:00 GMThttp://www.shnenglu.com/y346491470/articles/168388.htmlhttp://www.shnenglu.com/y346491470/comments/168388.htmlhttp://www.shnenglu.com/y346491470/articles/168388.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/168388.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/168388.html100)鍜孡(L<=106)錛岄棶k鐨勬渶灝忚川鍥犳暟鏄惁灝忎簬L銆?br />
銆愰瑙c戯細鏄劇劧鎴戜滑鍙叧蹇冭川鍥犳暟錛屾墍浠ュ厛瀵筁姹傚嚭瀹冪殑鎵鏈夎川鍥犳暟銆?br />               鐒跺悗灝辨槸鍒ゆ柇榪欎簺璐ㄥ洜鏁版槸鍚﹁兘澶熸暣闄錛屽鏋滅敤鍗佽繘鍒墮珮綺懼害鍘繪眰妯′細TLE銆?br />               鑰冭檻鍒拌繘鍒惰秺澶э紝榪愮畻鐨勬鏁板氨瓚婂皯銆傚洜姝ゆ垜浠彲浠ユ洿楂樼殑榪涘埗銆?br />               鎴戞祴璇曚簡涓ょ榪涘埗錛?000榪涘埗鏃?00+MS錛?0000000000榪涘埗鏃?00+MS銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 #define maxn 110
22 #define maxl 1000050
23 char s[maxn];
24 int l, cnt;
25 int prime[100000], pn;
26 ll val[maxn];
27 
28 void getprime() {
29     bool isprime[maxl];
30     memset(isprime, truesizeof(isprime));
31     pn = 0;
32     for(ll i = 2; i < maxl; i++) {
33         if(isprime[i]) {
34             prime[pn++] = i;
35             for(ll j = i * i; j < maxl; j += i) isprime[j] = false;
36         }
37     }
38 }
39 
40 void work() {
41     int len = strlen(s);
42     ll res = 0;
43     cnt = 0;
44     for(int i = 0; i < len % 10; i++) {
45         res *= 10;
46         res += s[i] - '0';
47     }
48     val[cnt++] = res;
49     for(int i = len % 10; i < len; i += 10) {
50         res = 0;
51         for(int j = i; j < i + 10; j++) {
52             res *= 10;
53             res += s[j] - '0';
54         }
55         val[cnt++] = res;
56     }
57 }
58 
59 int main() {
60     getprime();
61     while(~scanf("%s%d", s, &l)) {
62         if(s[0] == '0' && l == 0) break;
63         work();
64         bool flag = true;
65         int len = (strlen(s) + 9) / 10;
66         for(int i = 0; i < pn && prime[i] < l; i++) {
67             ll res = 0;
68             for(int j = 0; j < cnt; j++) {
69                 res = res * 10000000000LL + val[j];
70                 res %= prime[i];
71             }
72             if(res == 0) {
73                 printf("BAD %d\n", prime[i]);
74                 flag = false;
75                 break;
76             }
77         }
78         if(flag) printf("GOOD\n");
79     }
80     return 0;
81 }
82 


y @ The Angry Teletubbies 2012-03-20 13:50 鍙戣〃璇勮
]]>
poj 2689 Prime Distance - 綰挎х瓫绱犳暟 + 姹傚ぇ鍖洪棿绱犳暟http://www.shnenglu.com/y346491470/articles/166591.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesSun, 26 Feb 2012 15:48:00 GMThttp://www.shnenglu.com/y346491470/articles/166591.htmlhttp://www.shnenglu.com/y346491470/comments/166591.htmlhttp://www.shnenglu.com/y346491470/articles/166591.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/166591.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/166591.html2,147,483,647)

銆愰瑙c戯細榪欓濂界粡鍏革紝褰撳勾鏂扮敓璧涗篃鍑轟簡榪欓錛屼笉榪囧仛涓嶅嚭錛岀洿鍒頒粖澶╃粓浜庣粰鎴戝仛鍑烘潵浜嗐?br />               榪欓闇瑕佺煡閬撲竴涓畾鐞嗭紝灝辨槸瀵逛簬姣忎竴涓悎鏁癮錛屽叾蹇呭畾瀛樺湪灝忎簬絳変簬鏍瑰彿a鐨勮川鍥犲瓙銆?br />               鎵浠ュ浜庝竴涓暟a鎴戜滑鍙渶瑕佹鏌ヤ粬鏈夋病鏈夊皬浜庣瓑浜庢牴鍙穉鐨勮川鍥犲瓙鍗沖彲銆?br />               鑰冭檻鍒版牴鍙?,147,483,647澶х害鏄?7000錛屾墍浠ユ垜浠厛絳涢夊嚭47000浠ュ唴鐨勭礌鏁幫紝澶х害5000涓?br />               鐒跺悗鍐嶇敤榪?000涓礌鏁板幓絳涢夌粰瀹氬尯闂村唴鐨勭礌鏁般?br />               鏈鍚庢灇涓句竴嬈℃眰絳旀鍗沖彲銆?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define lc(x) (x << 1)
15 #define rc(x) (x << 1 | 1)
16 #define lowbit(x) (x & (-x))
17 #define ll long long
18 #define MAX 1000050
19 ll prime[5500], rec[MAX];
20 int tot, cnt;
21 bool isprime[MAX];
22 ll l, r;
23 void init() {
24     tot = 0;
25     memset(isprime, truesizeof(isprime));
26     prime[tot++] = 2;
27     for(int i = 3; i < 50000; i += 2) {
28         if(isprime[i]) {
29             prime[tot++] = i;
30             if(i * i < 50000) {
31                 for(int j = i + i; j < 50000; j += i) isprime[j] = false;
32             }
33         } 
34     }
35 }
36 
37 int main() {
38     init();
39     while(~scanf("%lld%lld", &l, &r)) {
40         if(l < 2) l = 2;
41         cnt = 0;
42         memset(isprime, truesizeof(isprime));
43         for(int i = 0; i < tot && prime[i] * prime[i] <= r; i++) {
44             ll tmp = l / prime[i] * prime[i];
45             if(tmp < l) tmp += prime[i];
46             if(tmp == prime[i]) tmp += prime[i];
47             for(ll j = tmp; j <= r; j += prime[i]) isprime[j-l] = false;
48         }
49         for(int i = 0; i <= r - l; i++) 
50             if(isprime[i]) rec[cnt++] = i + l;
51 
52         if(cnt < 2) printf("There are no adjacent primes.\n");
53         else {
54             int idx1 = 1, idx2 = 1;
55             for(int i = 1; i < cnt; i++) {
56                 if(rec[i] - rec[i-1] < rec[idx1] - rec[idx1-1]) idx1 = i;
57                 if(rec[i] - rec[i-1] > rec[idx2] - rec[idx2-1]) idx2 = i;
58             }
59             printf("%lld,%lld are closest, %lld,%lld are most distant.\n", rec[idx1-1], rec[idx1], rec[idx2-1], rec[idx2]);
60         }
61     }
62     return 0;
63 }
64 


y @ The Angry Teletubbies 2012-02-26 23:48 鍙戣〃璇勮
]]>
hdoj 3944 DP錛?- 姹傚ぇ緇勫悎鏁?+ Lucas瀹氱悊http://www.shnenglu.com/y346491470/articles/164856.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesThu, 02 Feb 2012 14:42:00 GMThttp://www.shnenglu.com/y346491470/articles/164856.htmlhttp://www.shnenglu.com/y346491470/comments/164856.htmlhttp://www.shnenglu.com/y346491470/articles/164856.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/164856.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/164856.html
銆愰瑙c戯細涓寮濮嬩互涓烘槸涓猟p錛屽彂鐜皀,k閮藉緢澶э紝鑲畾鏄暟瀛﹂浜嗐?br />               寰堝鏄撳彂鐜版潹杈変笁瑙掑叿鏈夊縐版э紝鎵浠ユ垜浠彧闇瑕佺爺絀秌 <= n / 2鐨勬儏鍐點?br />               鍒╃敤璐績鎬濇兂錛屾瘡嬈¢兘寰宸︿笂鏂硅漿縐葷洿鍒板幓鍒扮0鍒楋紝鐒跺悗鏈鍚庡叏閮ㄥ線涓婅漿縐葷洿鍒版渶涓婃柟銆?br />               瀹規槗寰楀嚭鍏紡錛?br />                     ans = C(n, k) + C(n-1, k-1) + …… + C(n-k+1, 1) + C(n-k, 0) + n - k
                           鎶?C(n-k, 0) 鎹㈡垚 C(n-k+1, 0)錛岀敱鍏紡 C(n, k) = C(n-1, k) + C(n-1, k-1) 鍙互榪涜鍖栫畝
               鍖栫畝鍚庡緱錛?br />                     ans = C(n+1, k) + n - k

                     鎺ㄥ嚭絳旀鎴戝緢寮蹇冿紝浠ヤ負鎺ヤ笅鏉ュ氨娌′簡錛屽彲鏄垜鍙戠幇n寰堝ぇ錛屾櫘閫氭眰緇勫悎鏁頒細TLE錛岄間簬鏃犲錛屾垜鎼滀簡涓涓嬶紝鍙戠幇浜哃ucas瀹氱悊銆?br />                     鑷充簬浠涔堟槸Lucas瀹氱悊錛屽ぇ瀹惰嚜宸辯櫨搴﹀惂銆?br />                     鐢ㄤ簡瀹氱悊鍒氬紑濮嬭繕鏄疶LE錛屽洜涓篶ase楂樿揪10W錛岃繕瑕佹墦琛ㄨ涓嬫ā姣忎釜绱犳暟鐨勯樁涔橈紝鏈鍚庣粓浜庤繃浜嗐?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "vector"
 6 #include "queue"
 7 #include "cmath"
 8 #include "string"
 9 #include "cctype"
10 #include "map"
11 using namespace std;
12 #define pb push_back
13 #define lc(x) (x << 1)
14 #define rc(x) (x << 1 | 1)
15 #define lowbit(x) (x & (-x))
16 #define ll long long
17 #define maxp 10050
18 ll n, k, p;
19 ll fac[maxp][1500];
20 int prime[maxp], hash[maxp];
21 bool visit[maxp];
22 
23 void init() {
24     memset(visit, falsesizeof(visit));
25     int tot = 0;
26     prime[tot] = 2, hash[2] = tot, tot++;
27     for(int i = 3; i < maxp; i += 2) {
28         if(!visit[i]) {
29             prime[tot] = i, hash[i] = tot, tot++;
30             if(i * i < maxp)
31                 for(int j = 2 * i; j < maxp; j += i) visit[j] = true;
32         }
33     }
34     for(int i = 0; i < tot; i++) {
35         fac[0][i] = 1;
36         for(int j = 1; j < maxp; j++) {
37             fac[j][i] = (fac[j-1][i] * j) % prime[i];
38         }
39     }
40 }
41 
42 ll quickpower(ll x, ll n) {
43     ll res = 1;
44     x %= p;
45     while(n) {
46         if(n & 1) res = res * x % p;
47         x = x * x % p;
48         n >>= 1;
49     }
50     return res;
51 }
52 
53 ll C(ll n, ll k, ll p) {
54     ll res = 1;
55     if(k > n) return 0;
56     res = fac[n][hash[p]] * quickpower(fac[n-k][hash[p]] * fac[k][hash[p]], p - 2) % p;
57     return res;
58 }
59 
60 ll Lucas(ll n, ll k, ll p) {
61     if(k == 0) return 1;
62     return C(n % p, k % p, p) * Lucas(n / p, k / p, p) % p;
63 }
64 
65 int main() {
66     int Case = 1;
67     init();
68     while(~scanf("%lld%lld%lld", &n, &k, &p)) {
69         if(k > n / 2) k = n - k;
70         ll ans = (Lucas(n+1, k, p) + n - k) % p;
71         printf("Case #%d: %lld\n", Case++, ans);
72     }
73     return 0;
74 }
75 


y @ The Angry Teletubbies 2012-02-02 22:42 鍙戣〃璇勮
]]>
hdoj 2243 鑰冪爺璺尗鑼斺斿崟璇嶆儏緇?- ac鑷姩鏈?+ 鐭╅樀蹇熷箓 + 蹇熷箓 + 浜屽垎http://www.shnenglu.com/y346491470/articles/158561.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesMon, 17 Oct 2011 09:24:00 GMThttp://www.shnenglu.com/y346491470/articles/158561.htmlhttp://www.shnenglu.com/y346491470/comments/158561.htmlhttp://www.shnenglu.com/y346491470/articles/158561.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/158561.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/158561.html闃呰鍏ㄦ枃

y @ The Angry Teletubbies 2011-10-17 17:24 鍙戣〃璇勮
]]>
poj 2778 DNA Sequence - ac鑷姩鏈?+ 鐭╅樀蹇熷箓 + 杞寲涓烘眰L姝ラ暱鐨勬柟妗堟暟http://www.shnenglu.com/y346491470/articles/158559.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesMon, 17 Oct 2011 09:04:00 GMThttp://www.shnenglu.com/y346491470/articles/158559.htmlhttp://www.shnenglu.com/y346491470/comments/158559.htmlhttp://www.shnenglu.com/y346491470/articles/158559.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/158559.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/158559.html
銆愰瑙c戯細鐢╝c鑷姩鏈烘瀯閫犱竴涓狣FA錛堟棦鏈夌‘瀹氭х殑鐘舵佽漿縐誨浘錛夛紝鐒跺悗鐢ㄧ煩闃靛揩閫熷箓姹傝В銆?br />
銆愪唬鐮併戯細
  1 #include "iostream"
  2 #include "cstdio"
  3 #include "cstring"
  4 using namespace std;
  5 #define ll long long
  6 const ll MOD = 100000;
  7 const int kind = 4;
  8 const int maxn = 500;
  9 #define MAX 105
 10 int root, tot;
 11 int n, m;
 12 int que[maxn], head, tail;
 13 bool visit[maxn];
 14 struct Node {
 15     int child[kind];
 16     int fail;
 17     int end;
 18     void init() {
 19         memset(child, 0sizeof(child));
 20         fail = -1, end = 0;
 21     }
 22 } T[maxn];
 23 
 24 void init() {
 25     root = tot = 0;
 26     T[root].init();
 27 }
 28 
 29 int hash(char ch) {
 30     if(ch == 'A'return 0;
 31     else if(ch == 'C'return 1;
 32     else if(ch == 'G'return 2;
 33     else return 3;
 34 }
 35 
 36 void insert(char *s) {//鎻掑叆鍗曡瘝
 37     int p = root, index;
 38     while (*s) {
 39         index = hash(*s);
 40         if (!T[p].child[index]) {
 41             T[++tot].init();
 42             T[p].child[index] = tot;
 43         }
 44         p = T[p].child[index];
 45         s++;
 46     }
 47     T[p].end = 1;
 48 }
 49 
 50 void build_ac_auto() {
 51     head = tail = 0;
 52     que[tail++= root;
 53     while (head < tail) {
 54         int u = que[head++];
 55         for (int i = 0; i < kind; i++) {
 56             if (T[u].child[i]) {
 57                 int son = T[u].child[i];
 58                 int p = T[u].fail;
 59                 if (u == root) T[son].fail = root;
 60                 else {
 61                     T[son].fail = T[p].child[i];
 62                     T[son].end |= T[T[son].fail].end;
 63                 }
 64                 que[tail++= son;
 65             } else {//trie鍥撅紝璁懼畾铏氭嫙鑺傜偣
 66                 int p = T[u].fail;
 67                 if (u == root) T[u].child[i] = root;
 68                 else T[u].child[i] = T[p].child[i];
 69             }
 70         }
 71     }
 72 }
 73 
 74 struct Mat {
 75     ll val[MAX][MAX];
 76     void unit() {
 77         zero();
 78         for(int i = 0; i < MAX; i++) val[i][i] = 1;
 79     }
 80     void zero() {
 81         memset(val, 0sizeof(val));
 82     }
 83 }x;
 84 
 85 Mat operator *(const Mat &a, const Mat &b) {
 86     Mat tmp;
 87     tmp.zero();
 88     for(int k = 0; k <= tot; k++) {
 89         for(int i = 0; i <= tot; i++) {
 90             if(a.val[i][k])
 91                 for(int j = 0; j <= tot; j++) {
 92                     tmp.val[i][j] += a.val[i][k] * b.val[k][j];
 93                     if(tmp.val[i][j] >= MOD) tmp.val[i][j] %= MOD;
 94                 }
 95         }
 96     }
 97     return tmp;
 98 }
 99 
100 Mat operator ^(Mat x, int n) {
101     Mat tmp;
102     tmp.unit();
103     while(n) {
104         if(n & 1) tmp = tmp * x;
105         x = x * x;
106         n >>= 1;
107     }
108     return tmp;
109 }
110 
111 void dfs(int u) {
112     visit[u] = true;
113     for(int i = 0; i < kind; i++) {
114         int v = T[u].child[i];
115         if(!T[v].end) {
116             x.val[u][v]++;
117             if(!visit[v]) dfs(v);
118         }
119     }
120 }
121 
122 int main() {
123     char s[15];
124     while(~scanf("%d%d"&m, &n)) {
125         init();
126         for(int i = 0; i < m; i++) {
127             scanf("%s", s);
128             insert(s);
129         }
130         build_ac_auto();
131         x.zero();
132         memset(visit, falsesizeof(visit));
133         dfs(root);
134         Mat ans = x ^ n;
135         ll res = 0;
136         for(int i = 0; i <= tot; i++)
137             res += ans.val[0][i];
138         printf("%lld\n", res % MOD);
139     }
140     return 0;
141 }




y @ The Angry Teletubbies 2011-10-17 17:04 鍙戣〃璇勮
]]>
zoj 2671 Cryptography - 鐭╅樀涔樻硶 + 綰挎鏍戞煡璇㈡濇兂http://www.shnenglu.com/y346491470/articles/157455.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesMon, 03 Oct 2011 17:31:00 GMThttp://www.shnenglu.com/y346491470/articles/157455.htmlhttp://www.shnenglu.com/y346491470/comments/157455.htmlhttp://www.shnenglu.com/y346491470/articles/157455.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/157455.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/157455.htmlPi,j=Ai × Ai+1 × ... × Aj 銆?br />
銆愰瑙c戯細鏅氱殑鐭╅樀涔樻硶錛岃瀵熷埌1<=n,m<=30,000錛屽鏋滅敤鏈寸礌鐨勬柟娉曟椂闂村鏉傚害楂樿揪O(nm)錛屾樉鐒舵槸涓嶅彲浠ユ帴鍙楃殑銆?br />              榪欓噷鍙互鐢ㄧ嚎孌墊爲鐨勬煡璇㈡濇兂錛屾妸涓棿鏈夌敤鐨勫間繚瀛樹笅鏉ワ紝浠ュ悗灝卞彲浠ョ敤O(1)鐨勫緱鍒般?br />
銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 using namespace std;
 5 #define maxn 32768
 6 int r, n, m;
 7 struct Mat {
 8     int val[2][2];
 9     void zero() {
10         memset(val, 0sizeof(val));
11     }
12 }x[(maxn<<1)+1];
13 
14 int LC(int i) {return i << 1;}
15 int RC(int i) {return LC(i)^1;}
16 
17 Mat operator *(const Mat &a, const Mat &b) {
18     Mat tmp;
19     tmp.zero();
20     for(int k = 0; k < 2; k++) {
21         for(int i = 0; i < 2; i++) {
22             if(a.val[i][k]) {
23                 for(int j = 0; j < 2; j++) {
24                     tmp.val[i][j] += a.val[i][k] * b.val[k][j];
25                     if(tmp.val[i][j] >= r) tmp.val[i][j] %= r;
26                 }
27             }
28         }
29     }
30     return tmp;
31 }
32 
33 Mat query(int p, int left, int right, int l, int r) {//綰挎鏍戠殑鏌ヨ鎬濇兂
34     if(left == l && right == r) return x[p];
35     int mid = (left + right) >> 1;
36     if(r <= mid) return query(LC(p), left, mid, l, r);
37     else if(l > mid) return query(RC(p), mid + 1, right, l, r);
38     else return query(LC(p), left, mid, l, mid) * query(RC(p), mid + 1, right, mid + 1, r);
39 }
40 
41 int main() {
42     bool flag = false;
43     while(~scanf("%d%d%d"&r, &n, &m)) {
44         int st = 1;
45         while(st < n) st <<= 1;
46         for(int i = 0; i < n; i++
47             for(int j = 0; j < 2; j++)
48                 for(int k = 0; k < 2; k++)
49                     scanf("%d"&x[st+i].val[j][k]);
50         for(int i = st - 1; i; i--) x[i] = x[LC(i)] * x[RC(i)];
51         while(m--) {
52             if(flag) printf("\n");
53             else flag = true;
54             int le, ri;
55             scanf("%d%d"&le, &ri);
56             Mat ans = query(11, st, le, ri);
57             printf("%d %d\n%d %d\n", ans.val[0][0], ans.val[0][1], ans.val[1][0], ans.val[1][1]);
58         }
59     }
60     return 0;
61 }



y @ The Angry Teletubbies 2011-10-04 01:31 鍙戣〃璇勮
]]>
poj 3735 Training little cats - 鐭╅樀蹇熷箓http://www.shnenglu.com/y346491470/articles/157284.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesSat, 01 Oct 2011 04:00:00 GMThttp://www.shnenglu.com/y346491470/articles/157284.htmlhttp://www.shnenglu.com/y346491470/comments/157284.htmlhttp://www.shnenglu.com/y346491470/articles/157284.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/157284.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/157284.html               1. g i 緇檌鍙尗鍜竴棰楄姳鐢熺背
               2. e i 璁╃i鍙尗鍜悆鎺夊畠鎷ユ湁鐨勬墍鏈夎姳鐢熺背
               3. s i j 灝嗙尗鍜猧涓庣尗鍜猨鐨勬嫢鏈夌殑鑺辯敓綾充氦鎹?

               鐜板皢涓婅堪涓緇勬搷浣滃仛m嬈″悗錛岄棶姣忓彧鐚挭鏈夊灝戦鑺辯敓錛?/p>
銆愰瑙c戯細m杈懼埌10^9錛屾樉鐒朵笉鑳界洿鎺ョ畻銆?br />              鍥犱負k涓搷浣滅粰鍑轟箣鍚庡氨鏄浐瀹氱殑錛屾墍浠ユ兂鍒扮敤鐭╅樀錛岀煩闃靛揩閫熷箓鍙互鎶婃椂闂村鏉傚害闄嶅埌O錛坙ogm錛夈傞棶棰樿漿鍖栦負濡備綍鏋勯犺漿緗煩闃碉紵
              璇翠笅鎴戠殑鎬濊礬錛岃瀵熶互涓婁笁縐嶆搷浣滐紝鍙戠幇絎簩錛屼笁縐嶆搷浣滄瘮杈冨鏄撳鐞嗭紝閲嶇偣钀藉湪絎竴縐嶆搷浣滀笂銆?br />              鏈変竴涓緢濂界殑鍔炴硶灝辨槸娣誨姞涓涓緟鍔╋紝浣垮垵濮嬬煩闃靛彉涓轟竴涓猲+1鍏冪粍錛岀紪鍙蜂負0鍒皀錛屼笅闈互3涓尗涓轟緥錛?br />              瀹氫箟鍒濆鐭╅樀A = [1 0 0 0]錛?鍙峰厓绱犲浐瀹氫負1錛?~n鍒嗗埆涓哄搴旂殑鐚墍鎷ユ湁鐨勮姳鐢熸暟銆?br />              瀵逛簬絎竴縐嶆搷浣済 i錛屾垜浠湪鍗曚綅鐭╅樀鍩虹涓婁嬌Mat[0][i]鍙樹負1錛屼緥濡俫 1錛?br />              1 1 0 0
              0 1 0 0
              0 0 1 0
              0 0 0 1錛屾樉鐒禰1 0 0 0]*Mat = [1 1 0 0]
              瀵逛簬絎簩縐嶆搷浣渆 i錛屾垜浠湪鍗曚綅鐭╅樀鍩虹浣縈at[i][i] = 0,渚嬪e 2錛?br />              1 0 0 0
              0 1 0 0
              0 0 0 0
              0 0 0 1, 鏄劇劧[1 2 3 4]*Mat = [1 2 0 4]
              瀵逛簬絎笁縐嶆搷浣渟 i j錛屾垜浠湪鍗曚綅鐭╅樀鍩虹涓婁嬌絎琲鍒椾笌絎琷浜掓崲錛屼緥濡俿 1 2錛?br />              1 0 0 0
              0 0 0 1
              0 0 1 0
              0 1 0 0錛屾樉鐒禰1 2 0 4]*Mat = [1 4 0 2]
              鐜板湪錛屽浜庢瘡涓涓搷浣滄垜浠兘鍙互寰楀埌涓涓漿緗煩闃碉紝鎶妅涓搷浣滅殑鐭╅樀鐩鎬箻鎴戜滑鍙互寰楀埌涓涓柊鐨勮漿緗煩闃礣銆?br />              A * T 琛ㄧず鎴戜滑緇忚繃涓緇勬搷浣滐紝綾諱技鎴戜滑鍙互寰楀埌緇忚繃m緇勬搷浣滅殑鐭╅樀涓?A * T ^ m,鏈緇堢煩闃電殑[0][1~n]鍗充負絳旀銆?br />
              涓婅堪鐨勫仛娉曟瘮杈冪洿瑙傦紝浣嗘槸瀹炵幇榪囦簬楹葷儲錛屽洜涓鴻鏋勯爇涓笉鍚岀煩闃點?br />
              鏈夋病鏈夊埆鐨勬柟娉曞彲浠ョ洿鎺ユ瀯閫犺漿緗煩闃礣錛熺瓟妗堟槸鑲畾鐨勩?br />              鎴戜滑榪樻槸浠ュ崟浣嶇煩闃典負鍩虹錛?br />              瀵逛簬絎竴縐嶆搷浣済 i錛屾垜浠嬌Mat[0][i] = Mat[0][i] + 1錛?br />              瀵逛簬絎簩縐嶆搷浣渆 i錛屾垜浠嬌鐭╅樀鐨勭i鍒楁竻闆訛紱
              瀵逛簬絎笁縐嶆搷浣渟 i j錛屾垜浠嬌絎琲鍒椾笌絎琷鍒椾簰鎹€?br />              榪欐牱瀹炵幇鐨勮瘽錛屾垜浠緇堝湪澶勭悊涓涓煩闃碉紝鍏嶅幓鏋勯爇涓煩闃電殑楹葷儲銆?br />
              鑷蟲錛屾瀯閫犺漿緗煩闃礣灝卞畬鎴愪簡錛屾帴涓嬫潵鍙渶鐢ㄧ煩闃靛揩閫熷箓姹傚嚭 A * T ^ m鍗沖彲錛岃繕鏈変竴涓敞鎰忕殑鍦版柟錛岃棰橀渶瑕佺敤鍒發ong long銆?br />              鍏蜂綋瀹炵幇鍙互鐪嬩笅闈㈢殑浠g爜銆?br />
銆愪唬鐮併戯細

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 using namespace std;
 5 #define ll long long
 6 #define maxn 105
 7 int n, m, k;
 8 struct Mat {
 9     ll val[maxn][maxn];
10     void zero() {
11         memset(val, 0sizeof(val));
12     }
13     void unit() {
14         zero();
15         for(int i = 0; i < maxn; i++) val[i][i] = 1;
16     }
17 }A, T;//A = 鍒濆鐭╅樀 ,T = 杞疆鐭╅樀
18 
19 Mat operator *(const Mat &a, const Mat &b) {
20     Mat tmp;
21     tmp.zero();
22     for(int k = 0; k <= n; k++) {
23         for(int i = 0; i <= n; i++) {
24             if(a.val[i][k])
25                 for(int j = 0; j <= n; j++)
26                     tmp.val[i][j] += a.val[i][k] * b.val[k][j];
27         }
28     }
29     return tmp;
30 }
31 
32 Mat operator ^(Mat x, int n) {
33     Mat tmp;
34     tmp.unit();
35     while(n) {
36         if(n & 1) tmp = tmp * x;
37         x = x * x;
38         n >>= 1;
39     }
40     return tmp;
41 }
42 
43 void init() {
44     A.zero();
45     A.val[0][0= 1;
46     T.unit();
47 }
48 
49 int main() {
50     char s[5];
51     int a, b;
52     while(~scanf("%d%d%d"&n, &m, &k)) {
53         if(!&& !&& !k) break;
54         init();
55         for(int i = 0; i < k; i++) {
56             scanf("%s", s);
57             if(s[0== 'g') {
58                 scanf("%d"&a);
59                 T.val[0][a]++;
60             } else if(s[0== 'e') {
61                 scanf("%d"&a);
62                 for(int i = 0; i <= n; i++) T.val[i][a] = 0;
63             } else {
64                 scanf("%d%d"&a, &b);
65                 for(int i = 0; i <= n; i++) swap(T.val[i][a], T.val[i][b]);
66             }
67         }
68         Mat ans = A * (T ^ m);
69         for(int i = 1; i <= n; i++) printf("%lld ", ans.val[0][i]);
70         printf("\n");
71     }
72     return 0;
73 }



y @ The Angry Teletubbies 2011-10-01 12:00 鍙戣〃璇勮
]]>
poj 3233 Matrix Power Series - 鐭╅樀蹇熷箓 + 浜屽垎http://www.shnenglu.com/y346491470/articles/156781.htmly @ The Angry Teletubbiesy @ The Angry TeletubbiesSun, 25 Sep 2011 14:42:00 GMThttp://www.shnenglu.com/y346491470/articles/156781.htmlhttp://www.shnenglu.com/y346491470/comments/156781.htmlhttp://www.shnenglu.com/y346491470/articles/156781.html#Feedback0http://www.shnenglu.com/y346491470/comments/commentRss/156781.htmlhttp://www.shnenglu.com/y346491470/services/trackbacks/156781.htmlS = A + A2 + A3 + … + Ak

銆愰瑙c戯細浠ヤ笅鏄痬atrix67鐨勯瑙o細

榪欓亾棰樹袱嬈′簩鍒嗭紝鐩稿綋緇忓吀銆傞鍏堟垜浠煡閬擄紝A^i鍙互浜屽垎姹傚嚭銆傜劧鍚庢垜浠渶瑕佸鏁翠釜棰樼洰鐨勬暟鎹妯榪涜浜屽垎銆傛瘮濡傦紝褰搆=6鏃訛紝鏈夛細
A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3)
搴旂敤榪欎釜寮忓瓙鍚庯紝瑙勬āk鍑忓皬浜嗕竴鍗娿傛垜浠簩鍒嗘眰鍑篈^3鍚庡啀閫掑綊鍦拌綆桝 + A^2 + A^3錛屽嵆鍙緱鍒板師闂鐨勭瓟妗堛?/p>


銆愪唬鐮併戯細
 1 #include "iostream"
 2 #include "cstring"
 3 #include "cstdio"
 4 using namespace std;
 5 #define maxn 35
 6 int n, k, m;
 7 struct Mat {
 8     int val[maxn][maxn];
 9     void unit() {//鍗曚綅鐭╅樀
10         for(int i = 0; i < maxn; i++) val[i][i] = 1;
11     }
12     void zero() {//闆剁煩闃?/span>
13         memset(val, 0sizeof(val));
14     }
15 }x;
16 
17 Mat operator *(const Mat &a, const Mat &b) {//鐭╅樀涔樻硶
18     Mat tmp;
19     tmp.zero();
20     for(int k = 1; k <= n; k++) {
21         for(int i = 1; i <= n; i++)
22             if(a.val[i][k])
23                 for(int j = 1; j <= n; j++) {
24                     tmp.val[i][j] += a.val[i][k] * b.val[k][j];
25                     if(tmp.val[i][j] >= m) tmp.val[i][j] %= m;
26                 }
27     }
28     return tmp;
29 }
30 
31 Mat operator ^(Mat x, int n) {//鐭╅樀蹇熷箓
32     Mat tmp;
33     tmp.zero();
34     tmp.unit();
35     while(n) {
36         if(n & 1) tmp = tmp * x;
37         x = x * x;
38         n >>= 1;
39     }
40     return tmp;
41 }
42 
43 Mat operator +(const Mat &a, const Mat &b) {//鐭╅樀鍔犳硶
44     Mat tmp;
45     for(int i = 1; i <= n; i++)
46         for(int j = 1; j <= n; j++)
47             tmp.val[i][j] = (a.val[i][j] + b.val[i][j]) % m;
48     return tmp;
49 }
50 
51 Mat sum(int k) {
52     if(k == 1return x;
53     else {
54         Mat tmp = sum(k >> 1);
55         if(k & 1) {
56             Mat tmp2 = x ^ ((k >> 1+ 1);
57             return tmp + tmp2 + tmp * tmp2;
58         } else {
59             Mat tmp2 = x ^ (k >> 1);
60             return tmp + tmp * tmp2;
61         }
62     }
63 }
64 
65 int main() {
66     while(~scanf("%d%d%d"&n, &k, &m)) {
67         for(int i = 1; i <= n; i++) {
68             for(int j = 1; j <= n; j++) {
69                 scanf("%d"&x.val[i][j]);
70                 x.val[i][j] %= m;
71             }
72         }
73         Mat ans = sum(k);
74         for(int i = 1; i <= n; i++) {
75             for(int j = 1; j < n; j++
76                 printf("%d ", ans.val[i][j]);
77             printf("%d\n", ans.val[i][n]);
78         }
79         break;
80     }
81     return 0;
82 }




y @ The Angry Teletubbies 2011-09-25 22:42 鍙戣〃璇勮
]]>
poj 3070 Fibonacci - 鐭╅樀蹇熷箓姹侳ibonacci鏁?/title><link>http://www.shnenglu.com/y346491470/articles/156779.html</link><dc:creator>y @ The Angry Teletubbies</dc:creator><author>y @ The Angry Teletubbies</author><pubDate>Sun, 25 Sep 2011 14:25:00 GMT</pubDate><guid>http://www.shnenglu.com/y346491470/articles/156779.html</guid><wfw:comment>http://www.shnenglu.com/y346491470/comments/156779.html</wfw:comment><comments>http://www.shnenglu.com/y346491470/articles/156779.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/y346491470/comments/commentRss/156779.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/y346491470/services/trackbacks/156779.html</trackback:ping><description><![CDATA[銆愰鎰忋戯細姹侳ibonacci鏁板垪涓殑絎琻欏圭殑鏈鍚?涓暟瀛楋紝涓嶅寘鍚墠瀵?銆?br /><br />銆愰瑙c戯細Fibonacci婊¤凍浠ヤ笅鍏紡錛?br />                               <img align="middle" src="http://poj.org/images/3070_1.png" alt="" /><br />               鏋勯犵煩闃碉紝鍒╃敤鐭╅樀蹇熷箓姹傝В銆?br /><br />銆愪唬鐮併戯細 <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080"> 1</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">iostream</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 2</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">cstdio</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 3</span> <span style="color: #000000">#include </span><span style="color: #000000">"</span><span style="color: #000000">cstring</span><span style="color: #000000">"</span><span style="color: #000000"><br /></span><span style="color: #008080"> 4</span> <span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000"> </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> std;<br /></span><span style="color: #008080"> 5</span> <span style="color: #000000"></span><span style="color: #0000ff">#define</span><span style="color: #000000"> MAX 3</span><span style="color: #000000"><br /></span><span style="color: #008080"> 6</span> <span style="color: #000000"></span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> MOD </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">10000</span><span style="color: #000000">;<br /></span><span style="color: #008080"> 7</span> <span style="color: #000000"></span><span style="color: #0000ff">struct</span><span style="color: #000000"> Mat {<br /></span><span style="color: #008080"> 8</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> val[MAX][MAX];<br /></span><span style="color: #008080"> 9</span> <span style="color: #000000">    </span><span style="color: #0000ff">void</span><span style="color: #000000"> unit() {<br /></span><span style="color: #008080">10</span> <span style="color: #000000">        </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> MAX; i</span><span style="color: #000000">++</span><span style="color: #000000">) val[i][i] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">11</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">12</span> <span style="color: #000000">    </span><span style="color: #0000ff">void</span><span style="color: #000000"> zero() {<br /></span><span style="color: #008080">13</span> <span style="color: #000000">        memset(val, </span><span style="color: #000000">0</span><span style="color: #000000">, </span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(val));<br /></span><span style="color: #008080">14</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">15</span> <span style="color: #000000">};<br /></span><span style="color: #008080">16</span> <span style="color: #000000"><br /></span><span style="color: #008080">17</span> <span style="color: #000000">Mat </span><span style="color: #0000ff">operator</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">(</span><span style="color: #0000ff">const</span><span style="color: #000000"> Mat </span><span style="color: #000000">&</span><span style="color: #000000">a, </span><span style="color: #0000ff">const</span><span style="color: #000000"> Mat </span><span style="color: #000000">&</span><span style="color: #000000">b) {<br /></span><span style="color: #008080">18</span> <span style="color: #000000">    Mat tmp;<br /></span><span style="color: #008080">19</span> <span style="color: #000000">    tmp.zero();<br /></span><span style="color: #008080">20</span> <span style="color: #000000">    </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> k </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">; k </span><span style="color: #000000"><</span><span style="color: #000000"> MAX; k</span><span style="color: #000000">++</span><span style="color: #000000">) {<br /></span><span style="color: #008080">21</span> <span style="color: #000000">        </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> MAX; i</span><span style="color: #000000">++</span><span style="color: #000000">) {<br /></span><span style="color: #008080">22</span> <span style="color: #000000">            </span><span style="color: #0000ff">if</span><span style="color: #000000">(a.val[i][k])<br /></span><span style="color: #008080">23</span> <span style="color: #000000">                </span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000"> j </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">; j </span><span style="color: #000000"><</span><span style="color: #000000"> MAX; j</span><span style="color: #000000">++</span><span style="color: #000000">) {<br /></span><span style="color: #008080">24</span> <span style="color: #000000">                    tmp.val[i][j] </span><span style="color: #000000">+=</span><span style="color: #000000"> a.val[i][k] </span><span style="color: #000000">*</span><span style="color: #000000"> b.val[k][j];<br /></span><span style="color: #008080">25</span> <span style="color: #000000">                    </span><span style="color: #0000ff">if</span><span style="color: #000000">(tmp.val[i][j] </span><span style="color: #000000">>=</span><span style="color: #000000"> MOD) tmp.val[i][j] </span><span style="color: #000000">%=</span><span style="color: #000000"> MOD;<br /></span><span style="color: #008080">26</span> <span style="color: #000000">                }<br /></span><span style="color: #008080">27</span> <span style="color: #000000">        }<br /></span><span style="color: #008080">28</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">29</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> tmp;<br /></span><span style="color: #008080">30</span> <span style="color: #000000">}<br /></span><span style="color: #008080">31</span> <span style="color: #000000"><br /></span><span style="color: #008080">32</span> <span style="color: #000000">Mat </span><span style="color: #0000ff">operator</span><span style="color: #000000"> </span><span style="color: #000000">^</span><span style="color: #000000">(Mat x, </span><span style="color: #0000ff">int</span><span style="color: #000000"> n) {<br /></span><span style="color: #008080">33</span> <span style="color: #000000">    Mat tmp;<br /></span><span style="color: #008080">34</span> <span style="color: #000000">    tmp.zero(), tmp.unit();<br /></span><span style="color: #008080">35</span> <span style="color: #000000">    </span><span style="color: #0000ff">while</span><span style="color: #000000">(n) {<br /></span><span style="color: #008080">36</span> <span style="color: #000000">        </span><span style="color: #0000ff">if</span><span style="color: #000000">(n </span><span style="color: #000000">&</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) tmp </span><span style="color: #000000">=</span><span style="color: #000000"> tmp </span><span style="color: #000000">*</span><span style="color: #000000"> x;<br /></span><span style="color: #008080">37</span> <span style="color: #000000">        x </span><span style="color: #000000">=</span><span style="color: #000000"> x </span><span style="color: #000000">*</span><span style="color: #000000"> x;<br /></span><span style="color: #008080">38</span> <span style="color: #000000">        n </span><span style="color: #000000">>>=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">39</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">40</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> tmp;<br /></span><span style="color: #008080">41</span> <span style="color: #000000">}<br /></span><span style="color: #008080">42</span> <span style="color: #000000"><br /></span><span style="color: #008080">43</span> <span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000"> main() {<br /></span><span style="color: #008080">44</span> <span style="color: #000000">    Mat x;<br /></span><span style="color: #008080">45</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> n;<br /></span><span style="color: #008080">46</span> <span style="color: #000000">    x.val[</span><span style="color: #000000">1</span><span style="color: #000000">][</span><span style="color: #000000">1</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> x.val[</span><span style="color: #000000">1</span><span style="color: #000000">][</span><span style="color: #000000">2</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> x.val[</span><span style="color: #000000">2</span><span style="color: #000000">][</span><span style="color: #000000">1</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">, x.val[</span><span style="color: #000000">2</span><span style="color: #000000">][</span><span style="color: #000000">2</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">47</span> <span style="color: #000000">    </span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">~</span><span style="color: #000000">scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">n)) {<br /></span><span style="color: #008080">48</span> <span style="color: #000000">        </span><span style="color: #0000ff">if</span><span style="color: #000000">(n </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">) </span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">49</span> <span style="color: #000000">        Mat ans </span><span style="color: #000000">=</span><span style="color: #000000"> x </span><span style="color: #000000">^</span><span style="color: #000000"> n;<br /></span><span style="color: #008080">50</span> <span style="color: #000000">        printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">, ans.val[</span><span style="color: #000000">1</span><span style="color: #000000">][</span><span style="color: #000000">2</span><span style="color: #000000">]);<br /></span><span style="color: #008080">51</span> <span style="color: #000000">    }<br /></span><span style="color: #008080">52</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">53</span> <span style="color: #000000">}<br /></span><span style="color: #008080">54</span> <span style="color: #000000"></span></div><br /><img src ="http://www.shnenglu.com/y346491470/aggbug/156779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/y346491470/" target="_blank">y @ The Angry Teletubbies</a> 2011-09-25 22:25 <a href="http://www.shnenglu.com/y346491470/articles/156779.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p> <a href="http://www.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.pocoho.cn" target="_blank">国产69精品久久久久9999</a>| <a href="http://www.frsmb.cn" target="_blank">久久久青草青青国产亚洲免观</a>| <a href="http://www.ogus.cn" target="_blank">午夜精品久久久久成人</a>| <a href="http://www.grayhound.cn" target="_blank">超级碰久久免费公开视频</a>| <a href="http://www.shiyana.cn" target="_blank">久久久亚洲欧洲日产国码aⅴ </a>| <a href="http://www.ekqt.cn" target="_blank">九九精品99久久久香蕉</a>| <a href="http://www.sanya99job.cn" target="_blank">一本久久免费视频</a>| <a href="http://www.iqyyh.cn" target="_blank">伊人久久大香线蕉成人</a>| <a href="http://www.dgjiajun.net.cn" target="_blank">色婷婷综合久久久久中文字幕 </a>| <a href="http://www.zgpojie.cn" target="_blank">色综合久久久久综合99</a>| <a href="http://www.liangz.cn" target="_blank">久久综合九色欧美综合狠狠 </a>| <a href="http://www.daemontools.org.cn" target="_blank">久久精品一区二区三区不卡</a>| <a href="http://www.24quba.com.cn" target="_blank">久久精品国产色蜜蜜麻豆</a>| <a href="http://www.bvvnm.com.cn" target="_blank">久久婷婷人人澡人人爽人人爱</a>| <a href="http://www.syiz.cn" target="_blank">伊人伊成久久人综合网777</a>| <a href="http://www.chuangwote.com.cn" target="_blank">日韩AV毛片精品久久久</a>| <a href="http://www.tmnv.cn" target="_blank">久久笫一福利免费导航</a>| <a href="http://www.panxl.cn" target="_blank">97精品依人久久久大香线蕉97</a>| <a href="http://www.vnkp.cn" target="_blank">99精品国产99久久久久久97 </a>| <a href="http://www.goldquote.cn" target="_blank">99久久免费只有精品国产</a>| <a href="http://www.loreng.cn" target="_blank">国产成人精品久久亚洲高清不卡 </a>| <a href="http://www.phatmedia.cn" target="_blank">91精品国产高清久久久久久91</a>| <a href="http://www.kmaxbm.cn" target="_blank">国产A级毛片久久久精品毛片</a>| <a href="http://www.haiyunfu.cn" target="_blank">青青热久久国产久精品 </a>| <a href="http://www.iandu.cn" target="_blank">99久久成人18免费网站</a>| <a href="http://www.qinyoyo.cn" target="_blank">精品久久久久久国产免费了</a>| <a href="http://www.mhlz4f.cn" target="_blank">久久一区二区三区99</a>| <a href="http://www.52shadu.cn" target="_blank">亚洲午夜无码AV毛片久久</a>| <a href="http://www.inconfont.cn" target="_blank">无码伊人66久久大杳蕉网站谷歌</a>| <a href="http://www.nyvgcd.cn" target="_blank">久久久久亚洲av无码专区 </a>| <a href="http://www.yunxinggroup.com.cn" target="_blank">亚洲国产成人久久精品影视</a>| <a href="http://www.crolab.cn" target="_blank">久久99精品久久久久久齐齐</a>| <a href="http://www.u33g.cn" target="_blank">久久伊人五月丁香狠狠色</a>| <a href="http://www.6dyx.cn" target="_blank">成人久久精品一区二区三区</a>| <a href="http://www.shoucunshiyuan.cn" target="_blank">99精品久久久久久久婷婷</a>| <a href="http://www.uyqr.cn" target="_blank">久久99热这里只频精品6</a>| <a href="http://www.javareport.cn" target="_blank">久久久久AV综合网成人 </a>| <a href="http://www.bjhaoruida.com.cn" target="_blank">日韩影院久久</a>| <a href="http://www.baotou8.cn" target="_blank">777米奇久久最新地址</a>| <a href="http://www.njw2008.cn" target="_blank">欧美午夜A∨大片久久</a>| <a href="http://www.bdfu-edu.cn" target="_blank">久久久一本精品99久久精品88</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>