题解:CF327C Magic Five

· · 题解

5的倍数的个数

题面

题目传送门

题目描述:

一个长度为 n 的数字串 s ,有多少种方法可以使得删除若干个数字后剩下的数字串构成的数字能被 5 整除。可以有前导零,但删完后的数字串不能为空。

输入格式:

第一行输入一个数字串 a

第二行输入一个整数 k

s = aaaa \cdots a ( k\ 个\ a )

输出格式:

输出一个整数,对 10 ^ 9 + 7 取模

数据范围:

1 \leq | a | \leq 10 ^ 5, 1 \leq k \leq 10 ^ 9

思路

先观察样例,以第二组样例为示范:(这边 k=2 我就直接写两遍了)

1399013990\\ 528

什么?你看不出什么东西来?

那我帮你把零和五的位置给你标出来

1399013990\\ \ \ \ \ \ \ \ \ \ \color{red}{5\ \ \ \ \ \ \ 10}

还是不懂?

那再把528拆开来看看?

528 = 512 + 16 = 2 ^ {\color{red}{4}} + 2 ^ {\color{red}{9}}\\ 其中:\\ 4 = 4 + 0 \times 5\\ 9 = 4 + 1 \times 5

现在能看出来了吧

我们不妨记 0 / 5 之前的数字有 a 个,一小段字符串长度为 n ,重复了 k 次。

那么最终答案为:

\displaystyle \sum_{i = 0}^{k - 1}{2 ^ {a + {i \times {n}}}}\\

那么我们就可以开始循环枚举 i 来计算出答案了

吗?

再看一眼数据范围:

1 \leq k \leq 10 ^ 9

显然 O(k) 的时间复杂度是不行的

那么怎么办呢?

我们可以尝试把上面的式子化简一下:

\displaystyle \sum_{i = 0} ^ {k - 1}{2 ^ {a + {i \times {n}}}} = \displaystyle \sum_{i = 0} ^ {k - 1}({2 ^ {a} \times {2 ^ {i \times {n}}}}) =2 ^ a \times \displaystyle \sum_{i = 0} ^ {k - 1}{2 ^ {i \times {n}}} \\ =2 ^ a \times (2 ^ 0 + 2 ^ n + 2 ^ {2 n} + \cdots + 2 ^ {{(k - 1)} n})\\ \text{记}\ S\ =2 ^ 0 + 2 ^ n + 2 ^ {2 n} + \cdots + 2 ^ {{(k - 1) }n}\\ \text{则}\ 2 ^ n\ S\ =2 ^ n + 2 ^ {2 n} + 2 ^ {3 n} \cdots + 2 ^ {{k} n}\\ \therefore 2 ^ n S - S = (2 ^ n + 2 ^ {2 n} + 2 ^ {3 n} \cdots + 2 ^ {k n})-(2 ^ 0 + 2 ^ n + 2 ^ {2 n} + \cdots + 2 ^ {(k - 1) n})\\ \therefore (2 ^ n - 1) S = 2 ^ {k n} - 2 ^ 0\\ \therefore S\ = \frac{2 ^ {k n} - 1} {2 ^ n - 1}\\ \text{将}\ S\ = \frac{2 ^ {k n} - 1} {2 ^ n - 1}\ \text{代入原式得}\\ \text{原式}\ = 2 ^ a \times (2 ^ 0 + 2 ^ n + 2 ^ {2 n} + \cdots + 2 ^ {({k - 1}) n})\\ = 2 ^ a \times S\\ = 2 ^ a \times \frac {2 ^ {k n} - 1} {2 ^ n - 1}\\ = \frac{2 ^ {k n + a} - 2 ^ a} {2 ^ n -1 }\\

别忘了,最后还要对 10 ^ 9 + 7 取模, 我们可以通过费马小定理来求解分数取模后的结果

\frac {2 ^ {k n + a} - 2 ^ a}{ 2 ^ n - 1 }\mod {p} = ({2 ^ {k n + a} - 2 ^ a}) \times( {2 ^ n - 1}) ^ {p - 2} \mod {p}

代码

#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
#define int long long //开long long是个好习惯

/*
int qpow(int a,int b){//快速幂
    int ans=1;
    while(b){
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans%mod;
}
*/

long long qpow(long long a, long long b,long long c){//我的快速幂不知道为什么会T,所以借鉴了jhd大佬的
    if(b==0) return 1;
    if(b==1) return a%c;
    long long t=qpow(a,b/2,c)%c;
    return (((t*t)%c)*(b%2==0?1:a%c))%c;
}


signed main(){
    string s;
    int ans=0;
    int n,k;
    cin>>s;
    scanf("%lld",&k);
    n=s.size();
    for(int i=0;i<n;i++){
        if(s[i]-'0'==0 || s[i]-'0'==5){//公式部分
            int a=i;
            int fenzi=(qpow(2,k*n+a,mod)-qpow(2,a,mod)+mod)%mod;
            int fenmu=qpow((qpow(2,n,mod)-1+mod)%mod,mod-2,mod);
            ans+=fenzi*fenmu%mod;
            //cout<<fenzi<<" "<<fenmu<<endl;
            //计算时一定要记得减法后要+mod再%mod
        }
    }
    printf("%lld\n",ans%mod);
    return 0;
}

jinhangdong大佬

作者认为这道题结合了数论的很多知识,是到很综合的题,值得一做!

update 2024/7/21 15:54 费马小定理的网址贴错了TAT,已改。

update 2024/7/22 11:37 完善了一些问题,使语言更加通顺

update 2024/7/22 15:31 LATEX公式与文字间少了空格,被退回了qwq。呃……jinhongdong大佬名字少写了一个字母,赶紧补上去(逃