10167 - Birthday Cake

Problem G. Birthday Cake 

Background

Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100.

There are 2N (N is a integer, 1<=N<=50) cherries on the cake. Mother wants to cut the cake into two halves with a knife (of course a beeline). The twins would like to be treated fairly, that means, the shape of the two halves must be the same (that means the beeline must go through the center of the cake) , and each half must have N cherrie(s). Can you help her?

Note: the coordinate of a cherry (x , y) are two integers. You must give the line as form two integers A,B(stands for Ax+By=0), each number in the range [-500,500]. Cherries are not allowed lying on the beeline. For each dataset there is at least one solution.

Input

The input file contains several scenarios. Each of them consists of 2 parts: The first part consists of a line with a number N, the second part consists of 2N lines, each line has two number, meaning (x,y) .There is only one space between two border numbers. The input file is ended with N=0.

Output

For each scenario, print a line containing two numbers A and B. There should be a space between them. If there are many solutions, you can only print one of them.

Sample Input

2
-20 20
-30 20
-10 -50
10 -5
0

Sample Output

0 1
分析 :
遍历 A,B的时候由于A,B的范围是对称的可以只遍历一半范围。(A,B同号,A,B异号)。
代码 :
#include 
#define N 100
typedef struct point point;
struct point{
    int x;
    int y;
};
point cherry[N];
int judge(int a, int b, int n)
{
    int i, j, t, cnt1, cnt2;
    cnt1 = cnt2 = 0;
    for(i = 0; i < n; i++){
        t = cherry[i].x * a + cherry[i].y * b;
        if(t > 0)
            cnt1++;
        else if(t < 0)
            cnt2++;
        else
            break;
    }
    return (cnt1 == cnt2 && cnt1 + cnt2 == n);
}
int main()
{
    int n, i, j, f;
    while(scanf("%d", &n) != EOF && n != 0){
        n *= 2;
        for(i = 0; i < n; i++)
            scanf("%d %d", &cherry[i].x, &cherry[i].y);
        f = 0;
        for(i = -500; i <= 500; i++){
            for(j = -500; j <= 0; j++){
                f = judge(i, j, n);
                if(f == 1)
                    break;
            }
            if(f == 1)
                break;
        }
        printf("%d %d\n", i, j);
    }
    return 0;
}
### 字符串哈希处理方法 对于字符串 `'birthday cake'` 的哈希处理,通常采用的是基于多项式的哈希函数。该方法能够有效地计算并比较不同子串的内容。 #### 基本概念 字符串哈希是一种将字符串映射到固定大小数值的技术,常用于快速查找和模式匹配等问题中。通过预先计算各个前缀的哈希值,在后续操作里仅需简单的数学运算即可得出任意子串的哈希结果[^3]。 #### 多项式哈希函数定义 设有一个字符串 `s=s_1s_2...s_n` ,其字符集为小写字母,则可以通过如下方式构建哈希值 H(s): \[H(s)=\sum_{i=1}^{n}s_i \times p^{(n-i)} \% mod\] 这里 \(p\) 是一个选定的基础底数,\(mod\) 则是为了防止溢出而设定的大质数。选择合适的参数可以使冲突的概率降到最低程度[^4]。 #### 实际应用中的优化技巧 为了避免重复计算以及提高效率,实际编程实现时会利用滚动哈希的思想提前准备好所有可能需要用到的信息: - **预处理**:先求得整个字符串 s 的总哈希值,并记录下每一个位置 i 对应之前部分(即从起始至当前位置)形成的连续片段 hash[i]=hash(s[0..i])。 - **查询区间[l,r]** :当需要获取某一段特定范围内的子串哈希值时,可通过已知的整体数据迅速推导出来: \[\text{Hash}(l, r)=(\text{Hash}[r]-\text{Hash}[l-1]\times P^{r-l+1}) \% MOD\] 此处需要注意边界情况处理,比如 l==0 或者其他特殊情况下的调整[^5]。 #### Python代码示例 下面是一段Python代码来展示如何具体地对给定字符串执行上述过程: ```python def str_hash(text): base = 131 # 可选基数 mod = int(1e9 + 7) # 模数 h = [0] * (len(text)+1) power_base = [1]*(len(text)+1) for i in range(len(text)): h[i+1] = (h[i]*base + ord(text[i])) % mod power_base[i+1] = (power_base[i]*base)%mod def get(l, r): return ((h[r+1] - h[l]*power_base[r-l+1]%mod)+mod)%mod return h,power_base,get # 测试用法 text="birthday cake" hash_table,_,get=str_hash(text) print(f"The hash value of substring 'bir': {get(0,2)}") # 输出 "bir" 的哈希值 ``` 此程序实现了基本的功能——创建了一个可以高效检索任何指定区间的哈希值得工具。这有助于解决像生日蛋糕问题这样的复杂挑战,其中涉及到大量字符串之间的对比分析工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值