HDOJ 1005 Number Sequence
发布日期:2021-06-29 13:29:52
浏览次数:3
分类:技术文章
本文共 1084 字,大约阅读时间需要 3 分钟。
Problem Description
A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.Output
For each test case, print the value of f(n) on a single line.Sample Input
1 1 3 1 2 10 0 0 0Sample Output
2 5 发现很多同学都是以1,1为重复头,按照最多循环次数48来做的 我也参考了一些答案,发现: 1,不能以1,1 作为重复头; 2,自己先找周期。#include#include using namespace std;int f[100000005];int main(){ int a,b,n,i,j; f[1]=1;f[2]=1; while(scanf("%d%d%d",&a,&b,&n)) { int s=0;//记录周期 if(a==0&&b==0&&n==0) break; for(i=3;i<=n;i++) { f[i]=(a*f[i-1]+b*f[i-2])%7; for(j=2;j >"< < 0) break; } if(s>0){ f[n]=f[(n-j)%s+j]; //cout<<"f["< <<"]:="<<"f["<<(n-j)%s+j<<"] "<
转载地址:https://chenhx.blog.csdn.net/article/details/48473931 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月13日 03时53分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用 STM32 通用 Bootloader ,让 OTA 更加 Easy
2019-04-29
Cache 的基本概念与工作原理
2019-04-29
Android开发技巧!高级Android晋升之View渲染机制,Android篇
2019-04-29
Android程序员必会!Android面试必刷的200道真题,附答案
2019-04-29
Android面试回忆录:掌握这些Android开发热门前沿知识,完整PDF
2019-04-29
Android技术篇!Android面试吃透这一篇就没有拿不到的offer!面试必问
2019-04-29
Android开发快速学习!字节跳动历年校招Android面试真题解析,已拿offer
2019-04-29
Android开发指南!2021中级Android开发面试解答,完整版开放下载
2019-04-29
Android开发指南!带你全面解析Android框架体系架构view篇,已拿offer
2019-04-29
Android开发指南!金三银四我带你去BAT面试现场,跳槽薪资翻倍
2019-04-29
Android开发经验谈:来看看移动端小程序技术的前世今生!一线互联网公司面经总结
2019-04-29