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灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP