CodeForces - 296B Yaroslav and Two Strings (容斥)

Yaroslav and Two Strings

         

         Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s, similarly, wj represents the j-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 length n. 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 by 1000000007 (109 + 7).

Input

The first line contains integer n (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 number 1000000007 (109 + 7).



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


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

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

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



#include<bits/stdc++.h>
#define mod 1000000007
using 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;
}








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值