G. Repeat it(逆元的运用)

题目链接: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的逆元. nm=n[(10t)m1+(10t)m2+(10t)m3++(10t)1+(10t)0],a=10tmod=1e9+7,=n(am1+am2+am3++a1+a0=n(1aaam)=n(a1ama),%mod=n%mod(am1)%mod((a1)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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值