zoukankan      html  css  js  c++  java
  • Hdoj 1757

    描述

    Lele now is thinking about a simple function f(x).
    If x < 10 f(x) = x.
    If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
    And ai(0<=i<=9) can only be 0 or 1 .
    Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

    输入

    The problem contains mutiple test cases.Please process to the end of file.
    In each case, there will be two lines.
    In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
    In the second line , there are ten integers represent a0 ~ a9.

    输出

    For each case, output f(k) % m in one line.

    样例输入

    10 9999
    1 1 1 1 1 1 1 1 1 1
    20 500
    1 0 1 0 1 0 1 0 1 0

    样例输出

    45
    104

    思路

    很基础的矩阵快速幂

    代码

    #include <cstdio>
    #include<cstring>
    #define ll long long
    #define maxn 10
    using namespace std;
    
    int k, mod;
    
    struct Mat
    {
    	ll f[maxn][maxn];
    	void cls(){memset(f, 0, sizeof(f));}//全部置为0 
    	Mat() {cls();}
    	friend Mat operator * (Mat a, Mat b)
    	{
    		Mat res;
    		for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++)
    			for(int k = 0; k < maxn; k++)
    				(res.f[i][j] += a.f[i][k] * b.f[k][j]) %= mod;
    		return res;
    	}
    };
    
    Mat quick_pow(Mat a)  
    {  
        Mat ans;
        for(int i = 0; i < maxn; i++) ans.f[i][i] = 1;
        int b = k;
        while(b != 0)  
        {
            if(b & 1) ans = ans * a;
            b >>= 1;
            a = a * a;
        }
        return ans;  
    }
    
    int main()
    {
    	Mat A, B;
    	for(int i = 0; i < 10; i++)
    		B.f[0][i] = 9 - i;
    	for(int i = 1; i < 10; i++) A.f[i-1][i] = 1;
    	while(~scanf("%d %d", &k, &mod))
    	{
    		Mat C;
    		for(int i = 0; i < 10; i++) scanf("%d", &A.f[i][0]);
    		if(k < 10) {printf("%d
    ", k % mod); continue;}
    		k -= 9;
    		C = quick_pow(A);
    		C = B * C;
    		printf("%d
    ", C.f[0][0]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    日常学习随笔-数组、单链表、双链表三种形式实现队列结构的基本操作(源码注释)
    代码重构之单元测试
    C# yield return 用法与解析
    MVC学习手册之数据注解与验证
    C#数字图像处理算法学习笔记(三)--图像几何变换
    关于变量名与类名同名问题
    C# 计时器
    C#入门--索引器
    C#入门--字段与属性
    var与dynamic
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8392106.html
Copyright © 2011-2022 走看看