zoukankan      html  css  js  c++  java
  • Hdoj 1005

    原题链接

    问题描述

    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

    思路

    暴力枚举,因为是除7,余数最多49个一组循环,枚举找到循环后,即可快速算出

    代码

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a[50] = {1, 1};
    	int x, y, n;
    	ios::sync_with_stdio(false);
    	while(cin >> x >> y >> n)
    	{
    		if(x * y * n == 0) break;
    		for(int i = 2; i < 49; i++)
    			a[i] = (x * a[i - 1] + y * a[i - 2]) % 7;
    		cout << a[(n - 1) % 49] << endl;
    	}
        return 0;
    }
    
  • 相关阅读:
    洛谷P3513 [POI2011]KON-Conspiracy
    柱状图 三分法+树状数组
    CF习题集三
    CF习题集二
    CF习题集一
    单调队列总结
    SP688 SAM
    lemon使用方法
    洛谷 P2403 [SDOI2010]所驼门王的宝藏 题解
    字符串学习笔记二
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8289827.html
Copyright © 2011-2022 走看看