题目链接:http://codeforces.com/gym/101061/problem/G
题面:
G. Repeat it
time limit per test2.0 s
memory limit per test64 MB
inputstandard input
outputstandard 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 N separated 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
inputCopy
3
4 31
8 1
123 123
outputCopy
31313131
11111111
388853804
分析:
整数n复制m遍相当于:
令n的位数为t,有
整
数
n
复
制
m
遍
等
价
于
:
原
式
=
n
∗
[
(
1
0
t
)
m
−
1
+
(
1
0
t
)
m
−
2
+
(
1
0
t
)
m
−
3
+
⋅
⋅
⋅
+
(
1
0
t
)
1
+
(
1
0
t
)
0
]
,
而
中
括
号
内
是
一
个
等
比
数
列
,
令
a
=
1
0
t
,
m
o
d
=
1
e
9
+
7
,
则
原
始
=
n
∗
(
a
m
−
1
+
a
m
−
2
+
a
m
−
3
+
⋅
⋅
⋅
+
a
1
+
a
0
=
n
∗
(
a
−
a
m
1
−
a
)
=
n
∗
(
a
m
−
a
a
−
1
)
,
则
原
始
%
m
o
d
=
n
%
m
o
d
∗
(
a
m
−
1
)
%
m
o
d
∗
(
(
a
−
1
)
模
m
o
d
的
逆
元
.
整数n复制m遍等价于:原式=n*[(10^t)^{m-1}+(10^t)^{m-2}+(10^t)^{m-3}+···+(10^t)^{1}+(10^t)^{0}],而中括号内是一个等比数列,令a=10^t ,mod=1e9+7,则原始=n*(a^{m-1}+a^{m-2}+a^{m-3}+···+a^{1}+a^{0}=n*(\frac{a-a^m}{1-a})=n*(\frac{a^m-a}{a-1}),则原始\%mod=n\%mod* (a^m-1)\%mod *( (a-1)模mod的逆元.
整数n复制m遍等价于:原式=n∗[(10t)m−1+(10t)m−2+(10t)m−3+⋅⋅⋅+(10t)1+(10t)0],而中括号内是一个等比数列,令a=10t,mod=1e9+7,则原始=n∗(am−1+am−2+am−3+⋅⋅⋅+a1+a0=n∗(1−aa−am)=n∗(a−1am−a),则原始%mod=n%mod∗(am−1)%mod∗((a−1)模mod的逆元.
a-1模mod的逆元可以利用扩展欧几里得等算法求得–》关于逆元求解博客
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+100;
const int mod=1000000007;
ll ni(ll a,ll b , ll& x,ll& y) //利用扩展欧几里得,x就是a关于b的逆元,
{
if(b==0)
{
x=1;
y=0;
return a;
}
ll t=ni(b,a%b,y,x);
y-=(a/b)*x;
return t;
}
ll mul(ll a,ll b) // a^b % mod
{
ll base=a;
ll sum=1;
while(b)
{
if(b&1)
sum=sum*base%mod;
base=base*base%mod;
b>>=1;
}
return sum;
}
int main()
{
// freopen("data.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
ll m, n;
int len;
ll sum;
cin>>t;
while(t--)
{
cin>>m>>n;
ll x=n;
len=0;
while(x)
{
x/=10;
len++;
}
ll a=mul(10,len);
if(a==(ll)1)
{
sum=n%mod*m%mod;
cout<<sum<<endl;
}
else
{
ll w, z;
ll gcd=ni(a-1,mod,w,z);
sum=n%mod*(mul(a,m)-1+mod)%mod*(w+mod)%mod;
cout<<sum<<endl;
}
}
return 0;
}