SSL2694 August 15, 2017 raise group T1 string (math, combined number modulo)

Source: Internet
Author: User
Tags first string first row
August 15, 2017 raise group T1 string

Description

There are two string s,t of length n and consist only of lowercase letters, satisfying s and T happen to have K-bit difference. Q in all strings that happen to have K-bit different from S, T is sorted by dictionary order. Because the answer can be large, the die 10^9+7 output.

Input

The first row of two integers n,k.
The second line is a string s.
The third line is a string T.

Output

An integer line represents the answer.

Analysis: We start with the first bit of statistics:
The first bit of the first string must not be greater than the first bit of T
So if the first bit of S is smaller than the first bit of T, then the first contribution to the answer is
C (n-1,m-1) 25^ (m-1) (t[1]-' a '-1) +c (n-1,m) *25^m and then M –
If the first bit of S is larger than the first bit of T, then the first contribution to the answer is C (n-1,m-1) 25^ (m-1) (t[1]-' a ') and then M –
If the first bit of S equals the first bit of T, then the contribution to the answer is C (n-1,m-1) 25^ (m-1) (t[1]-' a ')
And then the next one to deal with it just fine.
As for the combined number modulus: a/b%c=a*b^ (c-2)%c;

Code

#include <cstdio> #include <iostream> #include <cstring> #include <string> #define MO 1000000007

#define MAXN 200000 using namespace std;
Long Long F[maxn],g[maxn],l[maxn],ans;
int n,k;

Char S[MAXN],T[MAXN];
    Long long power (int a,int b) {long long r=1,base=a;
        while (b) {if (b&1) r*=base;
        R%=mo;
        Base*=base;
        Base%=mo;
    b>>=1;
} return R;
    } long long C (int x,int y) {long long sum=f[x]*l[x-y]%mo*l[y]%mo;
return f[x]*l[x-y]%mo*l[y]%mo;
    } int main () {scanf ("%d%d", &n,&k);
    cin>>s+1;
    cin>>t+1;
    F[0]=1;g[0]=1;l[0]=1;
        for (int i=1;i<=n;i++) {f[i]=f[i-1]*i%mo;
        G[i]=g[i-1]*25%mo;
    L[i]=power (f[i],mo-2);
    } Ans=1;
            for (int i=1;i<=n;i++) {if (S[i]==t[i]) {ans+= (t[i]-' a ') *g[k-1]%mo*c (n-i,k-1)%mo;
        Ans%=mo; } if (S[i]>t[i]) {ans+=(t[i]-' a ') *g[k-1]%mo*c (n-i,k-1)%mo;
            Ans%=mo;
        k--;
            } if (S[i]<t[i]) {ans+= (t[i]-' a '-1) *g[k-1]%mo*c (n-i,k-1)%mo;
            Ans%=mo;
            if (n-i>=k) ans+=c (n-i,k)%mo*g[k]%mo;
            Ans%=mo;
        k--;
}} printf ("%lld", ans); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.