打卡信奥刷题(1295)用C++实现信奥 P2942 [USACO09MAR] Moon Mooing G

P2942 [USACO09MAR] Moon Mooing G

题目描述

A full moon casts some sort of spell on the cows and, like their cousins the wolves and coyotes, they bay at the moon – mooing instead of howling, of course.

Each ‘moo’ lasts a certain amount of time. A short ‘moo’ might last time 1; a longer one might last time 24 or even 1,000,000,000 or longer (cows can really moo when they want to). No ‘moo’ will last more than or equal to 2^63.

It should come as no surprise that the cows have a pattern to their moos. Bessie will choose an integer c (1 <= c <= 100) that is the initial length of a moo.

After Bessie moos for length c, the cows calculate times for

subsequent moos. They apply two formulae to each moo time to yield even more moo times. The two formulae are:

f1(c)=a1*c/d1+b1 (integer divide, of course) and 
f2(c)=a2*c/d2+b2. 
They then successively use the two new times created by evaluating f1(c) and f2(c) to create even more mooing times. They keep a sorted list of all the possible mooing times (discarding duplicates). 
They are allowed to moo a total of N times (1 <= N <= 4,000,000). Please determine the length of the longest moo before they must quit. 
The constants in the formulae have these constraints: 1 <= d1 < a1; d1 < a1 <= 20; 0 <= b1 <= 20; 1 <= d2 < a2; d2 < a2 <= 20; 0 <= b2 <= 20. 
Consider an example where c=3 and N=10. The constants are: 
a1=4    b1=3     d1=3 
a2=17   b2=8     d2=2 

The first mooing time is 3, given by the value of c. The total list of mooing times is:

1. c=3             ->  3       6. f2(3)=17*3/2+8  -> 33 
2. f1(3)=4*3/3+3   ->  7       7. f1(28)=4*28/3+3 -> 40 
3. f1(7)=4*7/3+3   -> 12       8. f1(33)=4*33/3+3 -> 47 
4. f1(12)=4*12/3+3 -> 19       9. f1(40)=4*40/3+3 -> 56 
5. f1(19)=4*19/3+3 -> 28      10. f1(47)=4*47/3+3 -> 65 
The tenth time is 65, which would be the proper answer for this set of inputs. 

Partial feedback will be provided on the first 50 submissions.
MEMORY LIMIT: 64MB

满月的时候,和狼一样,牛们也在月光下叫,他们从不嚎叫,而是哞叫。

每次哞叫都有一个时长,可能是 111 秒,可能是 10910^9109 秒或更久,牛们真的非常能叫.当然,没有哞叫时长会超过或等于 2632^{63}263

牛们的哞叫可以找到规律,这并不奇怪。贝茜会选择一个整数 c(c≤100)c(c\le100)c(c100) 来作为初始时长之后,牛们根据两条公式确定更多的时长:

f1(c)=⌊a1c/d1⌋+b1f_1(c)=\lfloor a_1c/d_1\rfloor+b_1f1(c)=a1c/d1+b1

f2(c)=⌊a2c/d2⌋+b2f_2(c)=\lfloor a_2c/d_2\rfloor+b_2f2(c)=a2c/d2+b2

牛们用这两条公式不断地迭代、计算,算得大量的时长.然后她们将这些时长排序,剔除重复的时长,最后取前 N(1<N<4000000)N(1<N< 4000000)N(1<N<4000000) 个整数为她们 NNN 次哞叫的时长.请你计算,第 NNN 次哞叫的时长是多少。公式中的常量均为整数,满足下列关系:

1≤d1<a1≤201 \le d_1 < a_1 \le 201d1<a120; 0≤b1≤200\le b_1 \le 200b120;

1≤d2<a2≤201 \le d_2 < a_2 \le 201d2<a220; 0≤b2≤200\le b_2\le 200b220

输入格式

* Line 1: Two space-separated integers: c and N

* Line 2: Three space-separated integers: a1, b1, and d1

* Line 3: Three space-separated integers: a2, b2, and d2

输出格式

* Line 1: A single line which contains a single integer which is the length of the Nth moo

输入输出样例 #1

输入 #1

3 10 
4 3 3 
17 8 2

输出 #1

65

C++实现

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, c;
int timing[5000010];
int f1, f2, ans1, ans2;
int a1, a2, b1, b2, d1, d2;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> c >> n;
	cin >> a1 >> b1 >> d1 >> a2 >> b2 >> d2;
	memset(timing, 0, sizeof timing);
	timing[1] = c;
	ans1 = ans2 = 1;
	int i = 1;
	while(i != n) {
		f1 = a1 * timing[ans1] / d1 + b1;
		f2 = a2 * timing[ans2] / d2 + b2;
		if(f1 < f2) timing[++i] = f1, ans1++;
		else if(f1 > f2) timing[++i] = f2, ans2++;
		else timing[++i] = f1, ans1++, ans2++;
	}
	sort(timing + 1, timing + n + 1);
	cout << timing[n] << '\n';
	return 0;
}

在这里插入图片描述

后续

接下来我会不断用C++来实现信奥比赛中的算法题、GESP考级编程题实现、白名单赛事考题实现,记录日常的编程生活、比赛心得,感兴趣的请关注,我后续将继续分享相关内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值