zoukankan      html  css  js  c++  java
  • Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings(组合数学)

    B. Yaroslav and Two Strings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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).

    Sample test(s)
    input
    2
    90
    09
    output
    1
    input
    2
    11
    55
    output
    0
    input
    5
    ?????
    ?????
    output
    993531194
    Note

    The first test contains no question marks and both strings are incomparable, so the answer is 1.

    The second test has no question marks, but the given strings are comparable, so the answer is 0.

     排除不符合条件的替换方法。

    AC Code:

      1 #include <iostream>
      2 #include <cstdio>
      3 
      4 using namespace std;
      5 
      6 const int maxn = 100005;
      7 const long long r = 1000000007;
      8 char s[maxn], w[maxn];
      9 int n;
     10 
     11 long long power(long long a, int b)  //计算a^b
     12 {
     13     long long res;
     14     if(b == 0) res = 1;
     15     else if(b == 1) res = a;
     16     else
     17     {
     18         res = power(a, b >> 1);
     19         res = (res * res) % r;
     20         if(b & 1) res = (res * a) % r;
     21         //cout << res << endl;
     22     }
     23     return res;
     24 }
     25 
     26 int main()
     27 {
     28     while(scanf("%d", &n) != EOF)
     29     {
     30         int a = 0, b = 0, c = 0;  //a指示是否存在s[i]>w[i],b指示是否存在s[i]<w[i]
     31         long long ans;
     32         scanf("%s %s", s, w);
     33         if(n == 1)
     34         {
     35             puts("0");
     36             continue;
     37         }
     38         for(int i = 0; i < n; i++)
     39         {
     40             if(s[i] == '?' || w[i] == '?')
     41             {
     42                 if(s[i] == '?') c++;
     43                 if(w[i] == '?') c++;
     44             }
     45             else if(s[i] > w[i]) a = 1;
     46             else if(s[i] < w[i]) b = 1;
     47         }
     48         if(!c)  //全为数字
     49         {
     50             if(a && b) ans = 1;
     51             else ans = 0;
     52         }
     53         else  //有问号
     54         {
     55             ans = power(10, c);
     56             if(a && !b)  //s[i]>w[i]
     57             {
     58                 long long dec = 1;
     59                 for(int i = 0; i < n; i++)
     60                 {
     61                     if(s[i] == '?')
     62                     {
     63                         if(w[i] == '?') dec = (dec * 55) % r;
     64                         else dec = (dec * ('9' - w[i] + 1)) % r;
     65                     }
     66                     else if(w[i] == '?') dec = (dec * (s[i] - '0' + 1)) % r;
     67                 }
     68                 ans -= dec;
     69                 if(ans < 0) ans += r;
     70             }
     71             else if(!a && b)  //s[i]<w[i]
     72             {
     73                 long long dec = 1;
     74                 for(int i = 0; i < n; i++)
     75                 {
     76                     if(w[i] == '?')
     77                     {
     78                         if(s[i] == '?') dec = (dec * 55) % r;
     79                         else dec = (dec * ('9' - s[i] + 1)) % r;
     80                     }
     81                     else if(s[i] == '?') dec = (dec * (w[i] - '0' + 1)) % r;
     82                 }
     83                 ans -= dec;
     84                 if(ans < 0) ans += r;
     85             }
     86             else if(c == 2 * n) //只有?
     87             {
     88                 ans = ans - (2 * power(55, n)) % r + power(10, n);
     89                 if(ans < 0) ans += r;
     90             }
     91             else if(!a && !b )
     92             {
     93                 long long dec1 = 1, dec2 = 1;
     94                 int d = 0;
     95                 for(int i = 0; i < n; i++)
     96                 {
     97                     if(s[i] == '?')
     98                     {
     99                         if(w[i] == '?')
    100                         {
    101                             dec1 = (dec1 * 55) % r;
    102                             d++;
    103                         }
    104                         else dec1 = (dec1 * ('9' - w[i] + 1)) % r;
    105                     }
    106                     else if(w[i] == '?') dec1 = (dec1 * (s[i] - '0' + 1)) % r;
    107                 }
    108                 for(int i = 0; i < n; i++)
    109                 {
    110                     if(w[i] == '?')
    111                     {
    112                         if(s[i] == '?') dec2 = (dec2 * 55) % r;
    113                         else dec2 = (dec2 * ('9' - s[i] + 1)) % r;
    114                     }
    115                     else if(s[i] == '?') dec2 = (dec2 * (w[i] - '0' + 1)) % r;
    116                 }
    117                 ans = ans - dec1 - dec2 + power(10, d);
    118                 if(ans < 0) ans += r;
    119             }
    120         }
    121         printf("%I64d\n", ans);
    122     }
    123     return 0;
    124 }
  • 相关阅读:
    计算机基础知识
    测试用例设计
    Windows MySql增量备份、完整备份采坑之路
    GOF 的23种JAVA常用设计模式总结 02 UML中的类图与类图之间的关系
    GOF 的23种JAVA常用设计模式总结 01 设计模式的概念分类和功能
    Springboot 整合ApachShiro完成登录验证和权限管理
    玩转Spring全家桶笔记 04 Spring的事务抽象、事务传播特性、编程式事务、申明式事务
    玩转Spring全家桶笔记 03 Spring的JDBC操作以及SQL批处理的实现
    玩转Spring全家桶笔记 02 那些好用的连接池HikariCP
    Springboot token令牌验证解决方案 在SpringBoot实现基于Token的用户身份验证
  • 原文地址:https://www.cnblogs.com/cszlg/p/3050961.html
Copyright © 2011-2022 走看看