G. Repeat it

G. Repeat it

time limit per test
2.0 s
memory limit per test
64 MB
input
standard input
output
standard output

Jad has bought a new computer, a really weird computer!!

Every time he copies some number from the screen and pastes it, the computer pastes it many times instead of once!

Jad tested his computer many times, and now he knows how many times the computer will paste each copied number.

For example, In case the new computer repeated each copied number 4 times. When Jad copies the number 31 and pastes it, the number that appears on the screen would be 31313131.

Given N the number that Jad copied, and M the number of times the new computer is pasting each copied number. you have to print the number that will appear on the screen.

Since the number might be very big, you are asked to print it modulo 1000000007.

Input

The first line of the input consists of a single integer t, the number of test cases. Each test case consists of two numbers M and Nseparated by a single space:

(1 ≤ M ≤ 109) is the number of times the new computer pasted the number N.

(0 ≤ N ≤ 109) is the number Jad had copied.

Output

For each test case print one line, the number that will appear on the screen modulo 1000000007.

Example
input
Copy
3
4 31
8 1
123 123
output
Copy
31313131
11111111
388853804

题意:给定m和n,要求m个n数字形式拼接起来的值对1000000007取模


#include <iostream>
#include<string.h>
#include<stdio.h>
#define ll long long
using namespace std;
const ll mod = 1000000007;
ll n,t,k,temp,len;
struct mat//定义矩阵结构体
{
  ll m[2][2];
  mat()
  {
    memset(m, 0, sizeof(m));
  }
};
mat mul(mat &A, mat &B)
{
  mat C;
  for (int i = 0; i < 2; i++)
  {
    for (int j = 0; j < 2; j++)
    {
      for (int k = 0; k < 2; k++) 
      {
        C.m[i][j] = (C.m[i][j] + A.m[i][k] * B.m[k][j]) % mod;
      }
    }
  }
  return C;
}
mat pow(mat A, ll n)
{
  mat B;
  for(int i=0;i<2;i++)//初始化方阵
    B.m[i][i]=0;

  //初始被乘矩阵的初值
    B.m[0][0]=k;
    B.m[1][0]=1;
    
  while (n)
  {
    if (n & 1)
      B = mul(A, B);//注意这里,矩阵的左乘和右乘是不一样的,对应的系数矩阵也不一样
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}
int main()
{
  cin>>t;
  while (t--)
  {
    cin>>n>>k;
    k=k%mod;
    len=1,temp=k;
    while(temp)
    {
        temp=temp/10;
        len=len*10;;
    }
    mat A;//矩阵A是系数矩阵(转移矩阵)
    A.m[0][0]=len%mod;//记得对len取模,wa了无数次
    A.m[0][1]=k;
    A.m[1][1]=1; 
    if(n==1)
    { 
      printf("%lld
",k);
    }
  
    else
    {
      mat B = pow(A, n-1);
      printf("%lld
",B.m[0][0]%mod);
    }
  }
  return 0;
}


 
原文地址:https://www.cnblogs.com/-citywall123/p/11228675.html