zoukankan      html  css  js  c++  java
  • NYOJ Number SequenceProblem F

    Number Sequence
    时间限制:1000 ms | 内存限制:65535 KB
    难度:2
    描述
    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).
    输入
    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.
    输出
    For each test case, print the value of f(n) on a single line.
    样例输入
    1 1 3
    1 2 10
    0 0 0

    样例输出
    2
    5来源
    ZJCPC2004
    上传者
    zinber

    冰这个贱男出的题,当时他的数据比较水哈,暴力即可过;但是比赛后,他有重新出了数据,重判……给哥哥判下来了!

    这题说不上是难题,但是出的很好,需要思维啊!

    题解:如题,f[i]一定是0-6中的某个数,那么f[i-1]与f[i-2]的所有可能组合也只有49种,也就是说,任意两个0-6之间的数的组合超过49组后,必须出现重复的!!出现重复的话那就会循环下去了!所以不用再去算了!

    View Code
     
    #include <stdio.h>
    int f[52];
    int main()
    {
    int i,j,a,b,n;
    f[1]=f[2]=1;
    while(scanf("%d %d %d",&a,&b,&n),n)
    {
    for(i=3;i<52;i++) f[i]=(a*f[i-1]+b*f[i-2])%7;
    if(n<52) printf("%d\n",f[n]);
    else
    {
    for(i=1;i<50;i++)
    for(j=i+1;j<51;j++)
    if(f[i]==f[j] && f[i+1]==f[j+1]) goto here;
    here: printf("%d\n",f[(n-i)%(j-i)+i]);
    }
    }
    return 0;
    }



  • 相关阅读:
    Github基础(一)
    python3中编码和解码
    sql面试题(一)
    sql查询(三)--having子句求众数、中位数
    sql查询
    numpy中ndarray数据结构简介
    qt信号和槽
    logtag查看log
    AT45DB161D 概述
    C中的auto、static、register、extern、const和volitate
  • 原文地址:https://www.cnblogs.com/fornever/p/2405912.html
Copyright © 2011-2022 走看看