链接:
https://www.nowcoder.com/acm/contest/106/B
来源:牛客网
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
One day Xiao Ming is walking on a straight road and sees many trees line up in the right side. Heights of each tree which is denoted by a non-negative integer from 0 to 9 can form a tree string. It's very surprising to find that the tree string can be represent as an initial string repeating K times. Now he wants to remove some trees to make the rest of the tree string looks beautiful. A string is beautiful if and only if the integer it represents is divisible by five. Now he wonders how many ways there are that he could make it.
Note that when we transfer the string to a integer, we ignore the leading zeros. For example, the string “00055” will be seen as 55. And also pay attention that two ways are considered different if the removed trees are different. The result can be very large, so printing the answer (mod 1000000007) is enough. And additionally, Xiao Ming can't cut down all trees.
输入描述:
The first line contains a single integer K, which indicates that the tree string is the initial string repeating K times.
The second line is the initial string S.
输出描述:
输入
1
100
输出
Initially, the sequence is ‘100’. There are
6 ways:
100
1_0
10_
_00
__0
来源:牛客网
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
One day Xiao Ming is walking on a straight road and sees many trees line up in the right side. Heights of each tree which is denoted by a non-negative integer from 0 to 9 can form a tree string. It's very surprising to find that the tree string can be represent as an initial string repeating K times. Now he wants to remove some trees to make the rest of the tree string looks beautiful. A string is beautiful if and only if the integer it represents is divisible by five. Now he wonders how many ways there are that he could make it.
Note that when we transfer the string to a integer, we ignore the leading zeros. For example, the string “00055” will be seen as 55. And also pay attention that two ways are considered different if the removed trees are different. The result can be very large, so printing the answer (mod 1000000007) is enough. And additionally, Xiao Ming can't cut down all trees.
输入描述:
The first line contains a single integer K, which indicates that the tree string is the initial string repeating K times.
The second line is the initial string S.
输出描述:
A single integer, the number of ways to remove trees mod 1000000007.
示例1输入
1
100
输出
6
说明Initially, the sequence is ‘100’. There are
6 ways:
100
1_0
10_
_00
__0
_0_
二项式定理:C(0,n)+C(1,n)+.....+C(n,n)=2^n;
等比数列求和公式:Sn=a1*(1-q^n)/(1-q);
费马小定理求逆元:
费马小定理:假如M是素数,且H与M互质,那么H^(M−1)≡(1%M)
乘法逆元:我们知道(A/B)%M=(A∗(1/B))%M。令1/B等于H,那么H就是B关于M的乘法逆元,其实就是关于M的一个相反数,B∗H≡(1%M)
那么联立两者A∗H≡(1%M)
得出乘法逆元H=A^(M−2)
#include<bits/stdc++.h>
#define ll long long
ll mod=1000000007;
using namespace std;
ll pow(ll n,ll m,ll mod)
{
ll ans=1;
ll t=n%mod;
while(m)
{
if(m&1)
{
ans=ans*t%mod;
}
m>>=1;
t=t*t%mod;
}
return ans;
}
int main()
{
int n;
cin>>n;
char s[100010];
cin>>s;
ll ans=0;
int t=strlen(s);
ll q=pow(2,t,mod);
for(ll i=0;i<t;i++)
{
if(s[i]=='0'||s[i]=='5')
{
ll a1=pow(2,i,mod);
ll p;
p=(a1*(1-pow(q,n,mod)))%mod;
ll H=pow(1-q,mod-2,mod);
p=(p*H)%mod;
ans+=p;
ans%=mod;
}
}
cout<<ans<<endl;
return 0;
}