Beautiful Trees Cutting

链接:https://www.nowcoder.com/acm/contest/106/B
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述


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_
示例2

输入

3
125390

输出

149796
这题的意思是给你一个数字k和一个字符串s,k是重复次数,s是重复的字符串,例如样例2,125390重复三次变成125390125390125390
问你在字符串里去掉若干个字符(不可以全部去掉),然后这个字符串代表的数字可以被5整除的数量有多少,我们可以知道要被5整除那么这个数字
的个位必须要是0或5,比如125390,里面的5如果是个位数(390拿走了)那么前面的1和2,都有两种选择拿与不拿,所以是2^2,那么他重复两次
的话就是2^(2+6)(6是字符串长度),重复生产就是2^(2+2*6),可以发现这是一个等比数列,求等比数列:http://www.cnblogs.com/6262369sss/p/8973446.html
然后我们在s里每次发现一个0或1就求他们的等比数列和。
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#define mod 1000000007
typedef long long int ll;
using namespace std;
ll n,m,k,t;
char str[100005];
ll quick(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
ll T(ll q,ll n)
{
    if(n==1)
    return 1;
    ll date=T(q,n/2)%mod;
    date=(date+date*quick(q,n/2))%mod;
    if(n%2)
    date=(date+quick(q,n-1))%mod;
    return date;
}
int main()
{
    while(cin>>n)
    {
        ll ans=0;
        cin>>str;
        //cout<<str<<endl;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            if(str[i]=='0'||str[i]=='5')
            {
                ll a1=quick(2,i)%mod;
                ll q=quick(2,len)%mod;
                ll Sn=T(q,n)%mod;
                Sn=a1*Sn%mod;
                ans=(ans+Sn)%mod; 
            }
        }
        cout<<ans<<endl;
     }
     return 0; 
}


猜你喜欢

转载自www.cnblogs.com/6262369sss/p/8973589.html
今日推荐