遇到公式题快去推啊,别傻愣 数学推公式 codechef Cracking the Code

本文探讨了如何通过迭代和递归公式计算数值序列的特定值。针对不同情况,提供了两种计算方法:一种适用于给定索引范围内的直接计算,另一种则通过推导额外的递归公式来处理特定条件下的计算需求。最终,文章阐述了如何利用这些方法来计算序列的最终值,并解释了如何计算特定值的二次项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://s3.amazonaws.com/codechef_shared/download/translated/SEPT15/mandarin/CODECRCK.pdf

PROBLEM LINK:

Practice
Contest

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Math

PROBLEM:

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.

EXPLANATION:

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  ik .
    In this case, the given recurrence can be used directly. I.e.,
    an+1=x(an+bn)xy(anbn)
    bn+1=x(anbn)+xy(an+bn)

  • When  k<i .
    In this case, the following recurrence can be derived and used.
    an1=an+bny(anbn)2x+2xy2
    bn1=anbn+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(anbn)
bn+1=x(anbn)+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+1bn+1=2xbn2xyan

Now, we use our original recurrence to calculate  an+2  and  bn+2 .
an+2=x(2xan+2xybn)xy(2xbn2xyan)=an(2x2+2x2y2)
bn+2=x(2xbn2xyan)+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  ki=2 c=24 ; when  ki=4 c=28 , and so on. Thus,  c=22(ki) .
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)  algorithm in all the programming languages.
Net complexity:  O(logN) .

SAMPLE SOLUTIONS:

Author
Tester
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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值