Beautiful Trees Cutting

Link: https://www.nowcoder.com/acm/contest/106/B
Source: Niuke.com

Time limit: C/C++ 1 second, other languages ​​2 seconds
Space limit: C/C++ 32768K, other languages ​​65536K
64bit IO Format: %lld

Topic description


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.

Enter description:

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 .

Output description:

A single integer, the number of ways to remove trees mod 1000000007.
Example 1

enter

1
100

output

6

illustrate

Initially, the sequence is ‘100’. There are
6 ways:
100
1_0
10_
_00
__0
_0_
Example 2

enter

3
125390

output

149796 
The meaning of this question is to give you a number k and a string s, k is the number of repetitions, s is the repeated string, such as example 2, 125390 repeated three times becomes 12 5 39 0 12 5 39 0 12 5 39 0
asks you to remove a number of characters from the string (not all of them), and then the number represented by this string can be divided by 5. We can know that if it is to be divisible by 5
, the one digit of the number must be 0 Or 5, such as 125390, if the 5 in it is a single digit (390 is taken away), then the previous 1 and 2 have two options to take or not to take, so it is 2^2, then if he repeats it twice
, it is 2 ^(2+6) (6 is the length of the string), the repeated production is 2^(2+2*6), it can be found that this is a proportional sequence, find the proportional sequence: http://www.cnblogs.com /6262369sss/p/8973446.html
Then every time we find a 0 or 1 in s, we find their proportional sequence sum.
Code:
#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)
        years =years*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;
}
intmain ()
{
    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;
                years = (years+Sn)% mod;
            }
        }
        cout<<ans<<endl;
     }
     return 0; 
}

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073931&siteId=291194637