codeforces Magic Five

本文介绍了一道计数魔数的问题,通过高效算法解决如何在字符串中删除部分字符以形成能被5整除的最大数量的数字。文章提供了一个具体的C++实现方案,并通过样例输入输出展示了算法的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题是水题,然后我刚开始都是用递归写的,然后就是超内存了,然后自己的脑袋不转圈了,然后就是傻了,好长时间没写题了不知道该怎么弄了,然后看了别人的代码然后才反应过来有个用递归写的完全是没有必要的,而我只想到了递归,可能递归每次写着都比较简单,所以我习惯了吧,废话不多说,贴个代码好了!

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<climits>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define repf(i,n,m) for(int i=(n); i<=(m); i++)//正循环的
#define repd(i,n,m) for(int i=(n); i>=(m); i--) //负循环的
#define fab(a) ((a)>0?(a):(0-(a)))
#define ll long long
#define arc(a) ((a)*(a))
#define inf 1000000007   //最大值的
#define exp 0.0000001     //浮点型的
#define N 200000    //记录开的数组
char s[N];
ll n,k,dp;
int len;
ll fun(ll m,int x)
{
    ll ans=1;
    while(x!=0)
    {
        if(x%2)
          ans=(ans*m)%inf;
        m=(m*m)%inf;
        x/=2;
    }
    return ans;
} 
ll rec(int x)
{ 
   if(x==0)
     return 1;
   if(x%2==0)
   {
      return (fun(dp,x/2)%inf+((1+fun(dp,x/2+1)%inf)*rec(x/2-1))%inf)%inf;
   }
   else
      return ((1+fun(dp,x/2+1)%inf)*rec(x/2))%inf;
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        cin>>k;
        ll ans=0,m=1;
        len=strlen(s);
        repf(i,0,len-1)
        { 
          if(s[i]=='0' || s[i]=='5')
          {
              ans+=m;
              if(ans>inf) ans-=inf;
          } 
          m*=2; if(m>inf) m-=inf;
        }  
        dp=fun(2,len);
    //    cout<<dp<<endl;
    //    cout<<rec(k-1)<<endl;
        ans*=rec(k-1);
        if(k==0)
          ans=0;
        cout<<ans%inf<<endl;
    }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡定的小Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值