• Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)


    题目链接

    Description

    Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i andj(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).

    Sample Input

    Input
    2
    90
    09
    Output
    1
    Input
    2
    11
    55
    Output
    0
    Input
    5
    ?????
    ?????
    Output
    993531194

    题意:

    对于两个数字串 S 和 W,如果存在 i 和 j 使得:S(i)>W(i) && S(j)<W(j) 那么说这两个串是不可比较的,现在给了两个长度均为 n(1≤n≤105) 的串 S 和 W,用 '?' 代表未知的字母,问,有多少种可能的情况,使得 S  和 W 不可比较?

    分析:

    求出所有可能的情况的数量,设为 ans

    求出 S 比 W 大的情况,即:S(i)≥W(i) 的情况数量,设为 res1

    求出 S 比 W 小的情况,即;S(i)≤W(i) 的情况数量,设为 res2

    求出 S 和 W 相等的情况,即:S(i)==W(i) 的情况数量,设为 res3

    结果应该是 ans-res1-res2+res3

    给的串的所有情况 = s完全>=w的情况  +  w完全>=s的情况  -  s==w的情况  + s>w && s<w的情况。

    刚开始用的是所有情况 - 完全大于 - 完全小于 - 完全等于。  这种做法不对,少减了大于和等于 或者 小与和等于混合的情况。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <cmath>
      6 #include <algorithm>
      7 #define LL __int64
      8 const int maxn = 1e5 + 10;
      9 const LL mo = 1e9 + 7;
     10 using namespace std;
     11 char s[maxn], w[maxn];
     12 LL n, cnt;
     13 
     14 LL cal1()
     15 {
     16     LL i, res = 1;
     17     for(i = 0; i < n; i++)
     18     {
     19         if(s[i]!='?' && w[i]!='?')
     20         {
     21             if(s[i]<w[i])
     22             {
     23                 res = 0;
     24                 break;
     25             }
     26         }
     27         else if(s[i]=='?' && w[i]=='?')
     28         res = (res*55)%mo;
     29         else if(s[i]=='?')
     30         res = (res*(10-w[i]+48))%mo;
     31         else
     32         res = (res*(s[i]-48+1))%mo;
     33     }
     34     return res%mo;
     35 }
     36 
     37 LL cal2()
     38 {
     39     LL i, res = 1;
     40     for(i = 0; i < n; i++)
     41     {
     42         if(s[i]!='?' && w[i]!='?')
     43         {
     44             if(s[i]>w[i])
     45             {
     46                 res = 0;
     47                 break;
     48             }
     49         }
     50         else if(s[i]=='?' && w[i]=='?')
     51         res = (res*55)%mo;
     52         else if(s[i]=='?')
     53         res = (res*(w[i]-48+1))%mo;
     54         else
     55         res = (res*(10-s[i]+48))%mo;
     56     }
     57     return res%mo;
     58 }
     59 
     60 LL cal3()
     61 {
     62     LL i, res = 1;
     63     for(i = 0; i < n; i++)
     64     {
     65         if(s[i]!='?' && w[i]!='?')
     66         {
     67             if(s[i]!=w[i])
     68             {
     69                 res = 0;
     70                 break;
     71             }
     72         }
     73         else if(s[i]=='?' && w[i]=='?')
     74         res = (res*10)%mo;
     75     }
     76     return res%mo;
     77 }
     78 
     79 int main()
     80 {
     81      LL i;
     82      LL ans, res1, res2, res3;
     83      while(~scanf("%I64d", &n))
     84      {
     85          scanf("%s%s", s, w);
     86          ans = 1; cnt = 0;
     87          for(i = 0; i < n; i++)
     88          {
     89              if(s[i]=='?') cnt++;
     90              if(w[i]=='?') cnt++;
     91          }
     92          for(i = 0; i < cnt; i++)
     93          ans = (ans*10)%mo;
     94          res1 = cal1();
     95          res2 = cal2();
     96          res3 = cal3();
     97 
     98          printf("%I64d
    ", (ans-res1-res2+res3+mo+mo)%mo);
     99      }
    100      return 0;
    101 }
  • 相关阅读:
    016 vue的组件通信
    015 vue组件中的数据
    014 vue的组件化开发
    013 vue的js中的高阶函数
    012 vue的v-model的使用
    011 vue的购书案例
    010 vue的过滤器的使用
    CF1519F
    CF1519E
    CF1517F
  • 原文地址:https://www.cnblogs.com/bfshm/p/4109219.html
Copyright © 2020-2023  润新知