Code Monkey home page Code Monkey logo

aoapc-bac2nd's People

Contributors

aoapc-book avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aoapc-bac2nd's Issues

The code of ch10 / UVa12169.cpp gets WA

In line 17

if(x[i+1] != (a*x[i] + b) % M) { ok = false; break; }

should be

if(i+1 <= T*2 && x[i+1] != (a*x[i] + b) % M) { ok = false; break; }

And in line 27

for(int i = 2; i <= T*2; i++) cout << x[i] << "\n";

should be

for(int i = 2; i <= T*2; i+=2) cout << x[i] << "\n";

9.2.3 有问题

page264页的dp函数,没有s=0时的出口函数,所以把ans设为0和(1<<30)都应该是一样的,都无法区分出出无法到达和已到达重点

A problem with ch6/UVa11853.cpp

Although the code can get AC, I think the >= should be > in line 48. > means starting from a circle that intersects the upper border, while >= allows to start from a tangent circle.

      if(y[i] + r[i] >= W && dfs(i)) { ok = false; break; } // 从上往下dfs

A simple test case below will make the code get WA. That is, you can go along the upper border, but the code will give "IMPOSSIBLE".

2
500 500 500
500 0 100

第二版第二章正文表述有误

在提示2-3下面的那段正文中,原文表述为“变量i定义在循环语句中,因此i在循环体内不可见
这段正文指的代码是
for(int i=1;i<=n;i++) printf("%d\n",i;
循环体内i应当是可见的

第一章習題答案

我已經用手寫寫出答案,書本:打下好基礎程式設計與演算法競賽--入門經典(繁體書,)但因沒時間上機慢慢測驗是否正確,其他章節是否也請作者給完整的解答,以方便驗證是否正確,還有官方放的友情連結網友給的習題答案已經失效,可否也請修復一下呢? 目前請作者請先給第一章解答,謝謝!

例题8-12,UVa12627的一部分代码有问题

虽然代码中没有用到g(k, i)这个函数,但其计算方式出现错误
if (i >= k2) return g(k - 1, i - k2) + count(k - 1);这是源代码中的那个式子
但实际应为if (i >= k2) return g(k - 1, i - k2) * 2 + count(k - 1);
仔细想象一下并不困难,左下角一个整体的方块,加上上面两块残缺的区域
这道题用的f(k, b) - f(k, a - 1)来表示的答案
同理答案也等于g(k, l - a + 1) - g(k, l - b), l is 1 << k

Errata 《入门经典》第二版 P156 例6-8 Uva548

由于Google Code挂了现在不知道在哪里开issues... Po在这里试试作者能不能看到
Correct me if I were wrong/if我买到了盗版= =
【分析】第一句话应该是“后序遍历的最后一个字符就是根”,而不是第一个字符。

例题7-13 UVa1374猜想

书中有一个猜想,每次总是使用”刚刚得到“的那个数。
这个猜想可以尝试用反证法证明,不知道是否正确。
假设目标数X最少需要d次乘除法求出,那么在解答树上,X位于深度d,则根据猜想假设,X一定由X(d-1)求出: X = X(d-1) +/- Xk,0 <= k <= d-1。
现在给出反例,X无法由Xd-1得到,但可以由另外路径中两个结点Xi, Xj得到,即X = Xi +/- Xj,0 <= i <= j < d-1,那么在另一条路径,0--i--j,X = X = Xi +/- Xj,那X可以在第j+1层就被求出,即只需要j+1 次乘除法就可以求出,与假设最少需要d(>j+1)次乘除法相悖,因此反例不存在。

勘误

P124代码下面那行有误,这里是以1e8为一“位”,而不是10。举例:若要表示90901234432156788765,则s={9090,12344321,56788765}

Considerable Optimization in Page 193, Chapter 7 (ch7/queen2.cpp)

if (!vis[0][i] && !vis[1][cur+i] && !vis[2][cur-i+n]

can be optimized to

if (!vis[0][i] && !vis[1][cur+i-n] && !vis[2][cur-i+n]

in prevention in memory abuse.
e.g. cur+i maybe is bigger than the limit for N Queen Problem. Maybe subtract an n on that expression.

例题7-8 (UVa 10603) 正确性

书中提及”上述算法非常直观,正确性却不是显然的“,请问为什么无法严格证明它是正确的?

算法使用priority queue,可以保证每次都pop出dist最小的node u,而每次把u的子节点u2 push进priority queue的时候,u2.dist = u.dist + amount,所以可以严格保证u2.dist > u.dist

所以当u被pop出,u.v中三个杯子水量的dist (ans[u.v[i]]),如果不是-1 (初始值,没被填充过),则一定是可达到的最小值,因为u.dist是当前所有node中dist的最小值,而在尚未push进priority queue的state node u2,所有的u2.dist都严格大于u.dist

此外在update_ans()函数中,
if (ans[d] < 0 || u.dist < ans[d]) ans[d] = u.dist也可以改成if (ans[d] < 0) ans[d] = u.dist,因为被priority queue弹出,第一次出现的水量d,他的dist就是可以达到的最小值,因为之后所有弹出的node的dist都大于当前的dist

10305

小dfs函数里面的else if字句应该把!dfs(v)放进判断条件,如果满足直接返回0吧。因为这个函数判断的应该是一条路上是否存在有向环,如果我错了希望有指出

10213題的code會TLE

n<2^31, O(n) complexity會超時

參考http://www.algorithmist.com/index.php/UVa_10213 可用closed form計算 但答案太大要用大數解 (不確定是否有更好解法)
(內部交點可用c(n,4)計算 (任四點構成一交點,每一交點多一塊),邊數可用c(n,2)計算 (每多一條邊多一塊) 共 c(n,4)+c(n,2)+1 塊)

參考代碼:

import java.io.*;
import java.util.*;
import java.math.*;
 
class Main
{
  public static void main (String args[])  
  {
    Scanner cin=new Scanner(System.in);
    int t,n;
    t=cin.nextInt();
    for(int i=0;i<t;i++){
      n=cin.nextInt();
      BigInteger ans;
      ans=BigInteger.valueOf(n)
      .multiply(BigInteger.valueOf(n-1))
      .multiply(BigInteger.valueOf(n-2))
      .multiply(BigInteger.valueOf(n-3))
      .divide(BigInteger.valueOf(24))
      .add(
           BigInteger.valueOf(n)
           .multiply(BigInteger.valueOf(n-1))
           .divide(BigInteger.valueOf(2))
           )
      .add(BigInteger.valueOf(1));
      System.out.println (ans);
    }
  }
}

第二章:韩信点兵

#include<stdio.h>
int main()
{
int a[40],b[40],c[40],i;
int kase=0;
int x=0;
while(scanf("%d %d %d",&a[x],&b[x],&c[x])==3)
{
x++;
if(scanf("%d %d %d",&a[x],&b[x],&c[x])==3)
x++;
else
break;
}

int j=0;
for(;j<x;j++)
{	int print=1;//是否有解 
	for(i=10;i<=100;i++)
	{
		if(i%3==a[j]&&i%5==b[j]&&i%7==c[j])
		{
			printf("case %d:%d\n",++kase,i);
			print=0;
			break; 
		}
	}
	if(print) printf("case %d:No answer.\n",++kase);
	
}
return 0;

}

Page.169 euler代码是不是有问题

169页的示例代码如下:

void euler(int u){
    for (int v=0; v<n; v++) if (g[u][v] && !vis[u][v]) {
        vis[u][v] = vis[v][u] =1;
        euler(v);
        prinf("...");
    }
}

这个代码貌似只有遍历的功能吧?

例题9-15 s1和s2的计算可简化

范例代码中,为了计算s1(只有一个老师能授课的课程集合)和s2(至少有两个老师能授课的课程集合),作者引入了s0。但其实不需要s0。
对于雇佣第i名老师,下一轮次的dp改为:
dp(i+1, s1^st[i], s2|(s1&st[i])即可。

程序3-7 回文词

2是镜像,但您的程序回答
22 -- is a regular palindrome.


22 should is a regular palindrome.

第5章 字符串 部分有问题

5.1.3 字符串 104页 提示5-5上方
原文是:

例如,C++的cin/cout可以直接读写string类型,却不能读写字符数组;string类型还可以像整数那样“相加”……

而在下面那份“输出每行所有整数之和”的代码中,有这样几行代码:

while(getline(cin,line)) {
  int sum = 0, x;
  stringstream ss(line);
  while(ss >> x) sum += x;
  cout << sum << "\n";
}

注意这里:

  cout << sum << "\n";

"\n"本身就是一个“只有一个字符”的字符数组,而能写到cout里。这不与上面“不能读写字符数组”矛盾吗?仔细查一查iostream的资料,可以发现它们可以读写char*或者 char[]
建议作者改一下。。。

关于egypt的问题

刘老师,您好!请问第7章egypt代码里的better函数是不是没有必要?我的理解是一旦找到解,dfs就会终止,搜索的顺序保证了是字典序最小?

Is there a mistake of ch9 / UVa1220.cpp ?

In line 56

if(d[0][1] > d[0][0] && f[1][0]) unique = true;

should be

if(d[0][1] > d[0][0] && f[0][1]) unique = true;

Although the code is Accepted, I think it will meet problem in some cases.

Is there a mistake of ch9 / UVa1218.cpp ?

In line 44 and 45

        if(d[u][0] > INF) d[u][0] = INF; // avoid overflow!
        if(d[u][1] > INF) d[u][1] = INF;

and line 51

        d[u][2] = min(d[u][2], d[u][1] - d[v][2] + d[v][0]); // neither u or father is server

If d[u][1] and d[v][2] are set to INF. d[u][1] - d[v][2] will be a zero, it’s obviously meaningless.

Although it is accepted, it will make mistakes in some test cases. Like

7
1 5
4 1
3 5
2 3
6 5
7 2
-1

the correct ans is 3, but the code get 1.

If we change it to this

                if(d[v][0] > INF) d[v][0] = INF;
                if(d[v][1] > INF) d[v][1] = INF * 2; // avoid INF - INF = 0

It will be all right.

紫书P305 UVa242 笔误?

不知道是不是题目更新了,总之“邮票组合中邮票的张数应最多”应改为“...应最少(由于这样可以节省印刷费用)”

UVa514 Revision

// UVa514 Rails
// Rujia Liu

include

include

using namespace std;
const int MAXN = 1000 + 10;

int n, target[MAXN];

int main() {
scanf("%d", &n);
while(n) {
stack s;
int A = 1, B = 1;
scanf("%d",&target[1]);
if(!target[1]){
scanf("%d",&n);
continue;
}
for(int i = 2; i <= n; i++)
scanf("%d", &target[i]);
int ok = 1;
while(B <= n) {
if(A == target[B]){ A++; B++; }
else if(!s.empty() && s.top() == target[B]){ s.pop(); B++; }
else if(A <= n) s.push(A++);
else { ok = 0; break; }
}
printf("%s\n", ok ? "Yes" : "No");

}
return 0;
}

例题9-18 跳舞机 UVa10618翻译有误P291

书上关于消耗能量的翻译,其中第三四行,书上原话是“如果这个脚上个时间单位移动到相邻箭头,则消耗5单位能量”,指的移动是上个时间单位里的移动,但是原题中的意思是“上个单位用了这个脚,现在这个时间单位的移动相邻,则消耗5能量”。

在程序中是按照原题的意思的,只是翻译有误。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.