HDU1005 Number Sequence

Stella981
• 阅读 428

HDU1005 Number Sequence
对于公式f[n]=A* f[n-1]+B* f[n-2],因为对于f[n-1]或者f[n-2] 的取值只有0、1、2、3、4、5、6这7个数,A、B又是固定的,所以只有7*7=49种可能值。
由该关系式得知每一项只与前两项发生关系,所以当再次连续出现1、1时,就找到了循环节mod
在输出答案时还要注意n%mod=0的情况。

//0ms
#include <bits/stdc++.h>
using namespace std;
int a[50],A,B,n,i,mod;
int main()
{
   
   
   
    while(cin>>A>>B>>n&&A&&B&&n)
    {
   
   
   
        a[1]=a[2]=1;
        for(i=3;i<=49;i++)
        {
   
   
   
            a[i]=(A*a[i-1]+B*a[i-2])%7;
            if(a[i]==1&&a[i-1]==1)break;
        }
        mod=i-2;n=n%mod;
        if(n==0) printf("%d\n",a[mod]);//注意n%mod=0的情况
        else printf("%d\n",a[n]);
    }
    return 0;
}

本文同步分享在 博客"nefu_ljw"(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
hdu2204 Eddy's爱好
原题:点击打开链接(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D2204)题意很明确了,即:给你一个正整数N,确定在1到N之间有多少个可以表示成M^K(K1)的数。本题目为容斥原
Stella981 Stella981
2年前
Python——Day1(笔记代码)
print('HelloWorld')"""n1input('请输入用户名:')print(n1)n2input('请输入密码:')print(n2)""""""n1"alex"n2"root"print(n1'\\t'n2)print(n2)""""""
Wesley13 Wesley13
2年前
F
Fstack简介Fstack粘合了dpdk、用户态协议栈和nginx、redis,弥补了dpdk没有协议栈的不足,并用nginx、redis提供了一个调用应用程序的接口。QuickStartgithub链接(https://www.oschina.net/action/GoToLink
Stella981 Stella981
2年前
Python递归函数、匿名函数、过滤函数
递归函数Python对递归的深度有限制,超过即会报错。所以一定一要注意跳出条件。斐波拉契数列一个数列,第一个数是1,第二个数也是1,从第三个数开始,每一个数是前两个数之和公式:f(1)1,f(2)1,f(3)f(1)f(2),...,f(n)f(n2)f(n1)
Stella981 Stella981
2年前
HDOJ1021题 Fibonacci Again 应用求模公式
ProblemDescriptionThereareanotherkindofFibonaccinumbers:F(0)7,F(1)11,F(n)F(n1)F(n2)(n2).InputInputconsistsofasequenceoflines,eachcontaini
Stella981 Stella981
2年前
Fortran文件读写
programeximplicitnonecharacter(len40)A(3000),B(3000),C(3000)!A异常、B已开挖、C需标记integeri,j,N1,N2,count!N1是10号文件行数,N2是11号文件行数,count是计数器open(unit10,
Easter79 Easter79
2年前
The Complete Guide To Rooting Any Android Phone
PhoneWhitsonGordon(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.lifehacker.com.au%2Fauthor%2Fwhitsongordon%2F)7April,20118:00AMShare(https://ww
Wesley13 Wesley13
2年前
Java大数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid1316(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D1316)题目描述:!(https://o
Stella981 Stella981
2年前
Hdu
Sequence(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D6395)
Stella981 Stella981
2年前
Jenkins 插件开发之旅:两天内从 idea 到发布(上篇)
本文首发于:Jenkins中文社区(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fjenkinszh.cn)!huashan(https://oscimg.oschina.net/oscnet/f499d5b4f76f20cf0bce2a00af236d10265.jpg)