CF-327 C.Magic Five---快速幂+组合数学

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 = |a|·k.

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

cf题目
题意:
给一个数n和一个数k,组成k个n(首位拼接)称为C
问通过删除C中的数有多少种方法使之后的C可被5整除

题解:取可以保留的最后一位,设位置为i,则此情况下有2^(i-1)种方法删除(i后面的全部删除),快速幂取每个可行位置提供的方法数,因为数据大小的缘故,还要考虑copyK次之后的幂,详见代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7, maxn = 100001;
int mod_pow(int a, int b)
{
    int ans = 1;
    while (b)
    {
        if (b & 1)ans = (ll)ans * a % mod;
        a = (ll)a * a % mod;
        b >>= 1;
    }
    return ans;
}
char s[maxn];
int k, f2[maxn];
int main()
{
    f2[0] = 1;
    //打表求幂
    for (int i = 1; i < maxn; i++)
    {
        f2[i] = f2[i - 1] << 1;
        if (f2[i] >= mod)f2[i] -= mod;
    }
    while (~scanf("%s%d", s, &k))
    {
        int ans = 0, len = strlen(s);
        for (int i = 0; i < len; i++)
            if (s[i] == '0' || s[i] == '5')
            {
                ans += f2[i];
                if (ans >= mod)ans -= mod;
            }
        int a = f2[len];
        ans = (ll)ans * (mod_pow(a, k) - 1) % mod * mod_pow(a - 1, mod - 2) % mod;
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值