Prob_Solve 板


LINE

※ 引述《RockLee (Now of all times)》之銘言: : 題目敘述網址: : http://code.google.com/codejam/contest/544101/dashboard#s=p2 : 雖然小弟的 algorithm 也能在限定時間內跑完 small set 及 large set, : 但是看了前幾名的 algorithm, : 似乎都有用到 (sqrt(5.0) + 1.0) / 2.0, : 有高手可以解釋一下這個神秘的數字是什麼意思嗎? : [第一名解答 in C++, <_.._>] : #include <iostream> : #include <sstream> : #include <string> : #include <vector> : #include <deque> : #include <queue> : #include <set> : #include <map> : #include <algorithm> : #include <functional> : #include <utility> : #include <cmath> : #include <cstdlib> : #include <ctime> : #include <cstdio> : using namespace std; : #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++) : #define foreach(c,itr) for(__typeof((c).begin()) : itr=(c).begin();itr!=(c).end();itr++) : template <class T> inline string itos(T n) {return : (n)<0?"-"+itos(-(n)):(n)<10?(string)("")+(char)('0'+(n)):itos((n)/10)+itos((n)%10);} : typedef long long ll; : int case_number; : #define printg case_number++, printf("Case #%d: ",case_number), printf : #define gout case_number++, printf("Case #%d: ",case_number), cout : double golden = (sqrt(5.0) + 1.0) / 2.0; : ll main2(int A1, int A2, int B1, int B2){ // A * golden < B : ll ans = 0; : for(int A=A1;A<=A2;A++){ : int x = (int)(A * golden) + 1; : x = max(x,B1); : if(x <= B2) ans += B2 - x + 1; : } : return ans; : } : int main(void){ : int number_of_test_cases,i; : scanf("%d",&number_of_test_cases); : REP(i,number_of_test_cases){ : int A1,A2,B1,B2; : scanf("%d%d%d%d",&A1,&A2,&B1,&B2); : ll ans = main2(A1,A2,B1,B2) + main2(B1,B2,A1,A2); : gout << ans << endl; : } : return 0; : } : [小弟的解答 in Java, 或許還有可以改進的空間, 互相漏氣求進步囉…] : import java.io.*; : class GCJ_2010_1A_C : { : private static final int MAX_NUMBER = 1000000; : private static int[] loseBegin = new int[MAX_NUMBER + 1]; : private static int[] loseEnd = new int[MAX_NUMBER + 1]; : public static void calculateLoseRange() : { : loseBegin[1] = 1; : loseEnd[1] = 1; : loseBegin[2] = 2; : loseEnd[2] = 3; : for (int i = 3; i <= MAX_NUMBER; i++) : { : int begin = loseBegin[i - 1]; : while (loseEnd[begin] < i) : { : begin++; : } : loseBegin[i] = begin; : loseEnd[i] = begin + i - 1; : } : } : public static void main(String[] args) : { : try : { : BufferedReader input = new BufferedReader(new FileReader(args[0])); : BufferedWriter output = new BufferedWriter(new FileWriter(args[1])); : calculateLoseRange(); : String line = input.readLine(); : int T = Integer.parseInt(line); : for (int i = 1; i <= T; i++) : { : line = input.readLine(); : int A1 = Integer.parseInt(line.substring(0, line.indexOf(' '))); : line = line.substring(line.indexOf(' ') + 1); : int A2 = Integer.parseInt(line.substring(0, line.indexOf(' '))); : line = line.substring(line.indexOf(' ') + 1); : int B1 = Integer.parseInt(line.substring(0, line.indexOf(' '))); : line = line.substring(line.indexOf(' ') + 1); : int B2 = Integer.parseInt(line); : long count = 0; : for (int j = A1; j <= A2; j++) : { : int lose = Math.max(0, Math.min(B2, loseEnd[j]) - Math.max(B1, : loseBegin[j]) + 1); : int win = (B2 - B1 + 1) - lose; : count += win; : } : String result = "Case #" + i + ": " + count; : System.out.println(result); // : output.write(result); : output.newLine(); : } : input.close(); : output.close(); : } : catch (Exception e) : { : e.printStackTrace(); : } : } : } phi = (sqrt(5) + 1) / 2 prove that (x, y) such that x <= y <= floor(x * phi) is a losing position and (x, y) such that y > floor(x * phi) is a winning position 1. (x, y) is a winning position iff (y, x) is a winning position 2. (x, x) is a losing position for all x > 0 3. if (x, y) is a losing position, (x, y + k * x) and (x + k * y, y) is a winning position (k > 0) 4. if (x, y) is a losing position, (X, Y) = (x, y - k * x) or (x - k * y, y) must satisfy one of, a) X <= 0 or Y <= 0 b) (X, Y) is a winning position for x = 1, floor(x * phi) = 1 and (1, 1) is a losing position (1, k), k > 1 is a winning position HOLDS if the hypothesis holds for x <= X, for x = X + 1 (x, x) is a losing position (by 2) for x < y <= floor(x * phi) we have x < y < phi * x, since phi \notin Q => x * (phi - 1) > y - x > 0 and y - 2*x < 0 consider (y - x) * phi, (y - x) * phi < x * (phi - 1) * phi = x, and y - x <= X => ((y - x), x) is a winnig position => (x, y) is a losing position (by 4) for floor(x * phi) < y for z < x, by induction, (z, x) is a losing position iff x <= floor(z * phi) smallest z happens when x = floor(z * phi) => z * phi - 1 < x <= z * phi => z - (1 / phi) < x / phi <= z => ceil(x / phi) <= z <= ceil(x / phi) => z = ceil(x / phi) (x, z) is a losing position if ceil(x / phi) <= z <= floor(x * phi) x * phi < floor(x * phi) + 1 < x * phi + 1 x / phi <= ceil(x / phi) < x / phi + 1 => - (x / phi + 1) < -ceil(x / phi) < - x / phi => x * (phi - 1 / phi) - 1 < floor(x * phi) - ceil(x / phi) + 1 < x * (phi - 1 / phi) + 1 and phi - 1 / phi = 1 x - 1 < floor(x * phi) - ceil(x / phi) + 1 < x + 1 \therefore floor(x * phi) - ceil(x / phi) + 1 = x \therefore for any y > floor(x * phi), \exists k \in N s.t. (x, y - k * x) is a losing position. hypothesis also holds for x = X + 1 by induction, hypothesis holds for all x \in N --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.4.195
1F:→ RockLee:感謝 S 大詳細的回覆, 版上果然高手眾多 <_.._> 02/20 15:48







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP