CodeForces

来源:互联网 发布:手机黄金交易软件 编辑:程序博客网 时间:2024/06/05 13:28

Yaroslav and Two Strings

         

         Yaroslav thinks that two stringss and w, consisting of digits and having lengthn are non-comparable if there are two numbers,i and j(1 ≤ i, j ≤ n), such thatsi > wi andsj < wj. Here signsi represents thei-th digit of string s, similarly, wj represents thej-th digit of string w.

A string's template is a string that consists of digits and question marks ("?").

Yaroslav has two string templates, each of them has lengthn. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers.

Help Yaroslav, calculate the remainder after dividing the described number of ways by1000000007 (109 + 7).

Input

The first line contains integern (1 ≤ n ≤ 105) — the length of both templates. The second line contains the first template — a string that consists of digits and characters "?". The string's length equals n. The third line contains the second template in the same format.

Output

In a single line print the remainder after dividing the answer to the problem by number1000000007 (109 + 7).



   题意:  给定两个字符串 s,w,字符串中“ ? ”表示任意字符,问有多少种方案使 存在 i ,j ,si > wi andsj < wj   ?


   思路: 容斥,先算出总的方案数,即对于每一个‘?'  就乘以10 ,然后再减去不符合情况的,

即当 1. si>wi  ,  sj>=wj   2.  sj<wj   ,si<= wi    

然后加上多减去的,即  si==wj   的情况减了两遍,所以要加上。



#include<bits/stdc++.h>#define mod 1000000007using namespace std;typedef long long ll;string s1,s2;int n;ll work1(ll re1){    for(int i=0;i<n;i++)    {        if(s1[i]=='?'&&s2[i]=='?')  re1=re1*55%mod;        else if(s1[i]=='?'&&s2[i]!='?') re1=re1*(10-(s2[i]-'0'))%mod;        else if(s1[i]!='?'&&s2[i]=='?') re1=re1*(s1[i]-'0'+1)%mod;        else if(s1[i]-'0'<s2[i]-'0'){   re1=0;  break; }    }    return re1%mod;}ll work2(ll re2){    for(int i=0;i<n;i++)    {        if(s1[i]=='?'&&s2[i]=='?')  re2=re2*55%mod;        else if(s1[i]!='?'&&s2[i]=='?') re2=re2*(10-(s1[i]-'0'))%mod;        else if(s1[i]=='?'&&s2[i]!='?') re2=re2*(s2[i]-'0'+1)%mod;        else if(s1[i]-'0'>s2[i]-'0'){   re2=0;  break; }    }    return re2%mod;}ll work3(ll re3){    for(int i=0;i<n;i++)    {        if(s1[i]=='?'&&s2[i]=='?')  re3=re3*10%mod;        else if(s1[i]=='?'&&s2[i]!='?') re3=re3%mod;        else if(s1[i]!='?'&&s2[i]=='?') re3=re3%mod;        else if(s1[i]-'0'!=s2[i]-'0'){   re3=0;  break; }    }    return re3%mod;}int main(){    while(scanf("%d",&n)!=EOF)    {        cin>>s1>>s2;        ll ans=1;        for(int i=0;i<n;i++)        {            if(s1[i]=='?')  ans=ans*10%mod;            if(s2[i]=='?')  ans=ans*10%mod;        }        ll re1=1,re2=1,re3=1;        ans=(ans-work1(re1)-work2(re2)+mod+work3(re3))%mod;        printf("%lld\n",ans);    }    return 0;}








原创粉丝点击