codeforces #327C Magic Five 矩阵快速幂加费马小定理加逆元

17 篇文章 0 订阅

C. Magic Five
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.

Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.

Look at the input part of the statement, s is given in a special form.

Input

In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |ak.

Output

Print a single integer — the required number of ways modulo 1000000007 (109 + 7).

Sample test(s)
input
1256
1
output
4
input
13990
2
output
528
input
555
2
output
63
Note

In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.

In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.

In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.


题意:告诉一个串,以及这个串的个数K,将这K个串连接起来,然后可以删除其中一些数字,但是不能全部删除,使得这个串表示的数能被5整除,可以存在包含前导零的情况,05 和 5是两个不同的数。问总共能有多少这种数。

思路:能被5整除,那么要么是0 要么是5结尾,所以对于只有一个串的时候每次都找0 5结尾的数,它前面的可以选或者不选就是总共2^i种可能。当有多个串时,第2,3,4,。。。k个串中可能性就是第一个串中对应位置的 i+strlen(str), 第一个串中符合条件的2^i的和为tmp,那么k个串中符合条件的总和就是  tmp*(1+2^len+2^(2len)+ 2^(3len)....+2^(klen)),这是个等比数列求和问题,可以化成(1-2^(len*k))/ (1-2^(len)) %mod  假设 a=(1-2^(len*k))b=(1-2^(len))  由于a很大,所以这个时候就要用到逆元来求(a/b)%mod 

乘法逆元定义:
满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元。

为什么要有乘法逆元呢?
当我们要求(a/b) mod p的值,且a很大,无法直接求得a/b的值时,我们就要用到乘法逆元。
我们可以通过求b关于p的乘法逆元k,将a乘上k再模p,即(a*k) mod p。其结果与(a/b) mod p等价。

证:(其实很简单。。。)
根据b*k≡1 (mod p)有b*k=p*x+1。
k=(p*x+1)/b。
把k代入(a*k) mod p,得:
(a*(p*x+1)/b) mod p
=((a*p*x)/b+a/b) mod p
=[((a*p*x)/b) mod p +(a/b)] mod p
=[(p*(a*x)/b) mod p +(a/b)] mod p
//p*[(a*x)/b] mod p=0
所以原式等于:(a/b) mod p

那么现在就要求这样一个K ,根据已知的两个试子 b*k≡1 (mod p) 和 b^(p-1)≡1 (mod p) (费马小定理)变换下可得

b*k*t1=p*x+1;

b^(p-1)*t2=p*y+1;

两个式子相减,b*k*t1 - b^(p-1)*t2 =p*(x-y)  -----> b*kb^(p-1) mod(p) -------->kb^(p-2)


#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int mod = 1000000007;
typedef __int64 ll;

char str[100009];


ll pow_(ll n,ll m)
{
    ll ans=1;
    while(m>0)
    {
        if(m&1)
            ans=(ans*n)%mod;

            n=(n*n)%mod;
            m>>=1;
    }
    return ans;
}

ll solve(ll len,ll k)
{
    ll b=pow_(2,len);
    ll a=pow_(b,k);
    a=(1-a+mod)%mod;
    b=(1-b+mod)%mod;

    return a*pow_(b,mod-2)%mod;
}

int main()
{
    ll k;
    while(~scanf("%s %I64d",str,&k))
    {
        int len=strlen(str);

        ll tmp=0;

        for(int i=0;i<len;i++)
        {
            if(str[i]=='0' || str[i]=='5')
                tmp=(tmp+pow_(2,i))%mod;
        }
        printf("%I64d\n",tmp*solve(len,k)%mod);
    }

    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值