GOJ1150(矩阵快速幂)

sum

Time Limit: 1000ms

Problem Description:

 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和。

Input:

测试数据有多组,以文件结尾。每行输入a,n(1<=a<=10^9,1=<n<=10^18)。

Output:

由于结果可能比较大,所以请输出答案mod 1000000007。

Sample Input:

3 2

Sample Output:

36

分析:数列a,aa,aaa...符合公式f[n]=pow(10,len)+a(len为a的位数);则g[n]=g[n-1]+f[n];由上面两个公式可构造矩阵:                                |1,0,0||g[n],f[n],1|=|g[n-1],f[n-1],0|*|p,p,0|(其中p=pow(len,10))                                |a,a,1|

递推得|g[n],f[n],1|=|g[1],f[1],0,|*ans^(n-1);ans为构造矩阵f[1]=g[1]=a;

所以最终答案为res=a*ans.m[0][0]+a*ans.m[0][1]+ans.m[0][2]

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
#define mod 1000000007
#define N 100010
using namespace std;
struct matrix
{
    LL m[3][3];
}ans;
matrix mult(matrix a,matrix b)
{
    matrix c;
    memset(c.m,0,sizeof(c.m));
    for(int i=0;i<3;i++)
    for(int k=0;k<3;k++)
    {
        if(a.m[i][k]==0)continue;
        for(int j=0;j<3;j++)
        {
            if(b.m[k][j]==0)continue;
            c.m[i][j]+=(a.m[i][k]%mod)*(b.m[k][j]%mod)%mod;
            c.m[i][j]%=mod;
        }
    }
    return c;
}
matrix quickmod(matrix x,LL n)
{
    matrix temp;
    memset(temp.m,0,sizeof(temp.m));
    for(int i=0;i<3;i++)temp.m[i][i]=1;
    while(n)
    {
        if(n&1)temp=mult(temp,x);
        x=mult(x,x);
        n>>=1;
    }
    return temp;
}
LL fact(LL x)
{
    LL res=1;
    for(int i=1;i<=x;i++)res*=10;
    return res;
}
int main()
{
    LL n,a;
    while(scanf("%lld%lld",&a,&n)!=EOF)
    {
        LL temp=a,len=0;
        while(temp)
        {
            len++;
            temp/=10;
        }
        LL p=fact(len);
        ans.m[0][0]=1;ans.m[0][1]=0;ans.m[0][2]=0;
        ans.m[1][0]=p;ans.m[1][1]=p;ans.m[1][2]=0;
        ans.m[2][0]=a;ans.m[2][1]=a;ans.m[2][2]=1;
        ans=quickmod(ans,n-1);
        LL res=ans.m[0][0]*a%mod+ans.m[1][0]*a%mod+ans.m[2][0];
        printf("%lld\n",res%mod);
    }
}

				
时间: 2024-08-30 11:25:27

GOJ1150(矩阵快速幂)的相关文章

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: 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); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...

2017省夏令营Day7 【快速幂,筛法,矩阵快速幂,线段树】

题解:首先,我们可以得到一个规律:经过2次变换后,a和b的值都分别乘2了,所以只要用快速幂就能过啦,但是,要特判n为0的情况. 代码如下: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define Mod 1000000007 5 using namespace std; 6 long long a,b,n,ans1,ans2; 7 long long power(long long x)

HDU 5950 Recursive sequence 矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=5950 一开始以为i^4不能矩阵快速幂,但是结论是可以得,那么要怎么递推呢? 矩阵快速幂的思路都是一样的,matrix_a * matrix_b ^ n 其中,想要维护什么,就在matrix_a写,比如现在是F[n - 1], F[n - 2],我想要递推到下一项,那么就 会变成F[n], F[n - 1],这个时候,你就要寻找一下F[n]和F[n - 1]有什么关系. i^4也一样,想要从i^4 递推到 (i