题解 | Moon Mooing-牛客假日团队赛14F题

Moon Mooing

https://ac.nowcoder.com/acm/contest/1086/F

题目描述

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.

输入描述:

* 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

输入
3 10
4 3 3
17 8 2
输出
65

解答

这题我一开始使用优先队列(STL中的priority_queue)来做,结果WA+TLE。其实本题只要用数组进行简单的模拟即可。当然也用到了队列的思想。具体见下面的代码:
#include <cstdio>
#include <iostream>
typedef unsigned long long ull; //注意本题要用unsigned long long。
const int N = 4000000; 
ull a[N+5]; //这里我们将a数组看成一个队列
inline ull mn(ull x, ull y) {
    return x < y ? x : y;
}
int main() {
    int c, n, a1, b1, d1, a2, b2, d2, i = 1, f1 = 1, f2 = 1;
    scanf("%d%d%d%d%d%d%d%d", &c, &n, &a1, &b1, &d1, &a2, &b2, &d2);
    a[i++] = c; //初始元素c入队
    while(i <= N) {
        ull x = mn(a1*a[f1]/d1+b1, a2*a[f2]/d2+b2); //取F1()和F2()中的较小值
        a[i++] = x; //将该较小值入队
        if(x == a1*a[f1]/d1+b1) f1++; //如果较小值来自F1(),则将F1()的指针f1+1。
        if(x == a2*a[f2]/d2+b2) f2++; //如果较小值来自F2(),则将F2()的指针f2+1
    } //重点理解while循环的内容。因为算出的F1()或F2()的数据单调递增,所以可以用这样的方式生成整个数列
    std::cout << a[n]; //最后输出即可。
    return 0;
}
//祝各位早日AC此题!



来源:x_faraway_x
全部评论

相关推荐

zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务