每周编程题:SPOJ NR2 (15/3/2014)
时间:2014-03-30
来源:互联网
今个周末,我搵到呢题畀大家:
http://www.spoj.com/problems/NR2/
因为输入数量好大,所以好有可能只得 C、C++ 或 Pascal 可以喺限时内做到。
Java 可能都得,但系我谂须要快速嘅输入方式。请参考以下连结:
http://stackoverflow.com/questions/14195351/reduce-program-runtime-in-java-making-a-code-faster
如果你识,请你暂时唔好贴上你嘅程式码,又或讲解点样做得到,以便其他人可以尝试。
过咗一星期后,就请随便。
作者: fitcat07 发布时间: 2014-03-30

作者: form5 发布时间: 2014-03-30
有贴码的冲动


增加难度:
尝试加快速度,最快者系 0.46s,我就 0.51s,排第三。
作者: fitcat07 发布时间: 2014-03-30

增加难度:
尝试加快速度,最快者系 0.46s,我就 0.51s,排第三。

Spoj fail to compile my C# code, their compiler might be too old to handle System.Numerics.BigInteger, I guess
作者: form5 发布时间: 2014-03-30

Spoj fail to compile my C# code, their compiler might be too old to handle System.Numerics.BigInteger, I guess

作者: fitcat07 发布时间: 2014-03-30
之前有网友提议,周末搵条较浅嘅编程题目,等大家一齐玩。
今个周末,我搵到呢题畀大家:
http://www.spoj.com/problems/NR2/
因为输入数量好大,所以好有可能只得 C、C++ 或 Pascal 可以喺限时内 ...
这题目似乎不易 (至少对我来说

K = (a1 ^ a2) | (a1 ^ a3) .....
另外,comments 里面所说的: "here input isn't terminated by a '\n', it's quite unusual",对 用普通的方法 handle input (I mean, use fgets() or scanf()) 有没有影响?
[ 本帖最后由 assembly.jc 於 2014-3-16 07:55 AM 编辑 ]
作者: assembly.jc 发布时间: 2014-03-30
这题目似乎不易 (至少对我来说

作者: fitcat07 发布时间: 2014-03-30
全中。呢题系考大家对位元逻辑运算嘅能力。并只针对其中两种:互斥或同或。呢两种逻辑运算有乜嘢特性呢?完全冇影响。佢咁讲系因为使用快捷输入方式时,可能要特别处理呢种情况。
就算系大数字都好
Xor和or操作都好容易
唔似bit shift、数字加法、乘法咁要同时操作一列data
作者: Susan﹏汪汪 发布时间: 2014-03-30
全中。呢题系考大家对位元逻辑运算嘅能力。并只针对其中两种:互斥或同或。呢两种逻辑运算有乜嘢特性呢?完全冇影响。佢咁讲系因为使用快捷输入方式时,可能要特别处理呢种情况。
睇返一些简单 Boolean algebra theorems, 尝试简化题目的算式,可惜失败了,看来用纯数的方法是解决不了的,但也是预料之内,毕竟这不是数学问题。现在从 bit pattern 方向去想,因为是 combination 的关系,有很多以下的 pattern
(a1 ^ a2) | (a2 ^ a3)
如果限 ai 的值为 0<= ai <= 3,那么若 a1=1, a2=2, (a1 ^ a2) = 3, in binary is 11, 已经不需要理会 (a2 ^ a3)了...只是初步的构思。
p.s 我完全不知答案,以上说的不算是提示吧,仍在黑暗中摸索答案


作者: assembly.jc 发布时间: 2014-03-30
进一步简化,设每个 ai 只可以 0 或 1,你能给出答案吗?
作者: fitcat07 发布时间: 2014-03-30
result = (x | y | z) ^ ( x & y &z)
作者: form5 发布时间: 2014-03-30
given x, y, z
result = (x | y | z) ^ ( x & y &z)
作者: Susan﹏汪汪 发布时间: 2014-03-30

就算开,都整个更直观嘛...

作者: fitcat07 发布时间: 2014-03-30

作者: form5 发布时间: 2014-03-30
given x, y, z
result = (x | y | z) ^ ( x & y &z)
That's great

[ 本帖最后由 assembly.jc 於 2014-3-18 05:06 AM 编辑 ]
作者: assembly.jc 发布时间: 2014-03-30
都整个更直观嘛...

That's enough, same truth table as (a1 ^ a2) | (a2 ^ a3). But Is it the optimized solution? need further simplification?
I mean that, via the formula provided by form5, we can combine two pair of number (say (a1, a2), (a2, a3)) into one expression. let we use (a1a2a3) to denote (a1 | a2 | a3) ^ (a1 & a2 & a3). Thus, the whole equation can be expressed as
(a1a2a3) | (a2a3a4) | ...
Does need to find a way to combine (a1a2a3) | (a2a3a4) ?
[ 本帖最后由 assembly.jc 於 2014-3-18 05:26 AM 编辑 ]
作者: assembly.jc 发布时间: 2014-03-30
For the time being, forget about that. Try to answer the question raised on my previous post (repeat here):
Suppose all ai are either 0 or 1, can we simplify the formula in this case?
作者: fitcat07 发布时间: 2014-03-30
I think you might misunderstood the solution given by form5.
For the time being, forget about that. Try to answer the question raised on my previous post (repeat here):
Suppose all ai are either ...
Ok, let us go back to the previous post, if all ai are either 0 or 1, then
(a1 ^ a2) | (a2 ^ a3) = 0 if and only if a1 = a2 = a3, otherwise, (a1 ^ a2) | (a2 ^ a3) = 1.
So, instead of binary operations, we just need to check whether all admin number ai are equal?
Besides, I can't figure out a simpler form of (a1 ^ a2) | (a2 ^ a3) after examining it's truth table

[ 本帖最后由 assembly.jc 於 2014-3-19 08:18 AM 编辑 ]
作者: assembly.jc 发布时间: 2014-03-30
(a1 ^ a2) | (a2 ^ a3) = 0 if and only if a1 = a2 = a3, otherwise, (a1 ^ a2) | (a2 ^ a3) = 1.


Then, the truth table for A and B are:
a1 a2 a3 a1^a2
0 0 - 0
0 1 - 1
1 0 - 1
1 1 - 0
B:
a1 a2 a3 a2^a3
- 0 0 0
- 0 1 1
- 1 0 1
- 1 1 0
where - means don't care.
a1 a2 a3 A|B
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0
[ 本帖最后由 fitcat07 於 2014-3-19 12:12 PM 编辑 ]
作者: fitcat07 发布时间: 2014-03-30
作者: fitcat07 发布时间: 2014-03-30
Now, you know the answer is 0 when all ai are equal and 1 otherwise. Then, what if ai now consists of 2 bits, ie, ai can be 00, 01, 10 or 11 in binary form? Finally, in general, what if ai consists of ...
Thanks a lot! I got it! and now I know why from5's solution is optimized


[ 本帖最后由 assembly.jc 於 2014-3-20 07:03 AM 编辑 ]
作者: assembly.jc 发布时间: 2014-03-30
开始时,我用了大量时间希望利用 boolean algebra 去简化给定的算式,但「死做烂做」都是徒劳无功。因为方向错误了,结果反而愈走愈远。后来经 fitcat 的指点后回到正确的方向上,答案一下子就出来了。
这令我体会到,所谓难的问题,不是因为它极其复杂,而是因为很难发现能找到答题的方向。这令我想起费马大定理的证明故事,证明该定理的教授花了八年的时间去完成证明,而发现证明的方向是二个日本教授,但在这个方向发现之前,已花了三百多年的时间尝试不同的方向了。
作者: assembly.jc 发布时间: 2014-03-30
在解答这道题目的过程中,我又上了一课:方向比方法更重要

虽然我不断强调自学嘅重要性,上学系有一个好重要嘅原因:就系有人可以指出你嘅错误,并提供正确方向。
另外,多帮助人,亦可以令自己进步。

熟能生巧系老生常谈,亦系至理。每学一样嘢,都必须经过失败。只要唔放弃、够虚心(此点极重要),自己想法就会慢慢改变。
想法改变,就会改变(坏)习惯。习惯改变,性格就会改变。性格改变,命运随之改变。

作者: fitcat07 发布时间: 2014-03-30
作者: a8d7e8 发布时间: 2014-03-30
绝对赞同!

虽然我不断强调自学嘅重要性,上学系有一个好重要嘅原因:就系有人可以指出你嘅错误,并提供正确方向。
另外,多帮助人,亦可以令自己进步。

熟能生巧系 ...
你说得对,虚心对学习很重要。

另外,想问一问,今个星期会不会继续出题目?是否因为上一题太浅,所以吸引不到更多高手参与呢?
作者: assembly.jc 发布时间: 2014-03-30
另外,想问一问,今个星期会不会继续出题目?是否因为上一题太浅,所以吸引不到更多高手参与呢?
有 form5 同汪汪,喺呢版嚟讲,都唔少㗎啦。

只要引起讨论,我谂更多高手会参与...
作者: fitcat07 发布时间: 2014-03-30
只要有人参与,识唔识都唔紧要,我都会继续出题。
有 form5 同汪汪,喺呢版嚟讲,都唔少㗎啦。

只要引起讨论,我谂更多高手会参与...

作者: Susan﹏汪汪 发布时间: 2014-03-30
每次都出两题唔同难度吧


作者: fitcat07 发布时间: 2014-03-30
一於出三题。一题较易,一题较难,另一题我唔识解,等高人教一教我。

横掂难解的话都可能用好多时间
作者: Susan﹏汪汪 发布时间: 2014-03-30
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28