遇到公式题快去推啊,别傻愣 数学推公式 codechef Cracking the Code
2015-09-21 23:29
399 查看
https://s3.amazonaws.com/codechef_shared/download/translated/SEPT15/mandarin/CODECRCK.pdf
Practice
Contest
Easy-Medium
Math
Given two sequences a and b and
a recurrence to calculate ai and bi,
we need to calculate a certain value c when
a pair of corresponding terms of a and b are
given.
Subtask 1
For subtask 1, we can simply iterate from i to k (as
the case may be) and use the recurrences to calculate ak and bk.
Now, there are two cases:
When i≤k.
In this case, the given recurrence can be used directly. I.e.,
an+1=x(an+bn)−xy(an−bn)
bn+1=x(an−bn)+xy(an+bn)
When k<i.
In this case, the following recurrence can be derived and used.
an−1=an+bn−y(an−bn)2x+2xy2
bn−1=an−bn+y(an+bn)2x+2xy2
The last step is to calculate 2s.
The calculation must be done in floating type to ensure that the answer doesn't overflow. The in-built exponentiation function in most of the programming languages can do this job easily.
Subtask 2
Let us look at the original recurrences given to us, i.e.,
an+1=x(an+bn)−xy(an−bn)
bn+1=x(an−bn)+xy(an+bn)
What do they tell us? Basically, we are given an+1 and bn+1 in
terms of an and bn.
Let us go a step further and try to calculate an+2 and bn+2 in
terms of an and bn.
For doing this, we first note the following terms:
an+1+bn+1=2xan+2xybn
an+1−bn+1=2xbn−2xyan
Now, we use our original recurrence to calculate an+2 and bn+2.
an+2=x(2xan+2xybn)−xy(2xbn−2xyan)=an(2x2+2x2y2)
bn+2=x(2xbn−2xyan)+xy(2xan+2xybn)=bn(2x2+2x2y2)
Since, x=2√ and y=3√,
thus, (2x2+2x2y2)=16.
This leads us to a very important observation:
an+2=16an
bn+2=16bn
It tells us that if i and k are
both even or both odd, then ak+bk=c(ai+bi),
where c is
a constant. When k−i=2, c=24;
when k−i=4, c=28,
and so on. Thus, c=22(k−i).
When i and k are
of different parities, i.e., one is even and the other is odd, we can first calculate ai+1 and bi+1 and
then calculate ak and bk.
The last part is to calculate 2s,
which has already been explained in subtask 1.
All the operations take constant time except the use of exponentiation function. The in-built exponentiation function is implimented as O(logN) algorithm
in all the programming languages.
Net complexity: O(logN).
Author
Tester
Editorialist
PROBLEM LINK:
PracticeContest
DIFFICULTY:
Easy-Medium
PREREQUISITES:
Math
PROBLEM:
Given two sequences a and b anda recurrence to calculate ai and bi,
we need to calculate a certain value c when
a pair of corresponding terms of a and b are
given.
EXPLANATION:
Subtask 1For subtask 1, we can simply iterate from i to k (as
the case may be) and use the recurrences to calculate ak and bk.
Now, there are two cases:
When i≤k.
In this case, the given recurrence can be used directly. I.e.,
an+1=x(an+bn)−xy(an−bn)
bn+1=x(an−bn)+xy(an+bn)
When k<i.
In this case, the following recurrence can be derived and used.
an−1=an+bn−y(an−bn)2x+2xy2
bn−1=an−bn+y(an+bn)2x+2xy2
The last step is to calculate 2s.
The calculation must be done in floating type to ensure that the answer doesn't overflow. The in-built exponentiation function in most of the programming languages can do this job easily.
Subtask 2
Let us look at the original recurrences given to us, i.e.,
an+1=x(an+bn)−xy(an−bn)
bn+1=x(an−bn)+xy(an+bn)
What do they tell us? Basically, we are given an+1 and bn+1 in
terms of an and bn.
Let us go a step further and try to calculate an+2 and bn+2 in
terms of an and bn.
For doing this, we first note the following terms:
an+1+bn+1=2xan+2xybn
an+1−bn+1=2xbn−2xyan
Now, we use our original recurrence to calculate an+2 and bn+2.
an+2=x(2xan+2xybn)−xy(2xbn−2xyan)=an(2x2+2x2y2)
bn+2=x(2xbn−2xyan)+xy(2xan+2xybn)=bn(2x2+2x2y2)
Since, x=2√ and y=3√,
thus, (2x2+2x2y2)=16.
This leads us to a very important observation:
an+2=16an
bn+2=16bn
It tells us that if i and k are
both even or both odd, then ak+bk=c(ai+bi),
where c is
a constant. When k−i=2, c=24;
when k−i=4, c=28,
and so on. Thus, c=22(k−i).
When i and k are
of different parities, i.e., one is even and the other is odd, we can first calculate ai+1 and bi+1 and
then calculate ak and bk.
The last part is to calculate 2s,
which has already been explained in subtask 1.
COMPLEXITY:
All the operations take constant time except the use of exponentiation function. The in-built exponentiation function is implimented as O(logN) algorithmin all the programming languages.
Net complexity: O(logN).
SAMPLE SOLUTIONS:
AuthorTester
Editorialist
#include<bits/stdc++.h> using namespace std; int main() { int i, k, s; scanf("%d%d%d", &i, &k, &s); int ai, bi; scanf("%d%d", &ai, &bi); double a = ai, b = bi; if ((i&1) != (k&1)) { i++; a = sqrt(2.0) * ((ai + bi) - sqrt(3.0) * (ai - bi)); b = sqrt(2.0) * ((ai - bi) + sqrt(3.0) * (ai + bi)); } double ans = (a + b) * pow(2.0, 2*(k-i) - s); printf("%.10f\n", ans); }
相关文章推荐
- 【转载】MSXML应用总结 开发篇(下)
- 【转载】MSXML应用总结 开发篇(上)
- iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager(三)
- java多线程
- 2015.9.21笔记
- 谓词的经典用法——数组专用过滤器
- 变量与全局变量
- Python: ubuntu 下安装nltk以及载入 nltk-data
- iOS中KVO和NSNotification简单示范
- 企业进行敏捷变革失败最主要的原因: 有这样的最资深员工
- mysql 过滤查询(以字母开头,以数字开头,非数字开头,非字母开头)
- HIVE 应用集锦摘录
- IOS开发中针对UIImageView的几种常用手势
- mysql select相关基础知识
- Android获取手机及路由器的Mac地址和IP地址
- Github ignore file
- javascript图片库
- Fatal error: Call to undefined functionmb_internal_encoding() ,mac 安装php mbstring扩展
- 使用Slidmenu
- match和index返回多个匹配项