Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings

题目链接:点这里!!!!

题意:

给定两个长度为n(n<=1e5)的串s,w,串中能包含‘0‘~‘9‘,‘?‘,‘?‘可以代替‘0‘~‘9‘中任何一个数字。

问你能组成多少对特殊的串(‘?‘代替为数字后的s,w),使得存在(1<=i,j<=n)si<wi,sj>wj。

题解:

1、我们假设‘?’的总个数为num,则能够成的总情况为10^num。

2、我们可以通过求相反的情况来求出答案。

3、可以分为4种情况。

(1)已知存在(1<=i,j<=n)si<wi,sj>wj。我们的答案为sum。

(2)已知存在(1<=i<=n)si<wi,不存在(1<=j<=n)sj>wj。我们可以构造出所有(1<=i<=n)si<=wi的情况有ans种,则答案为sum-ans。

(3)已知存在(1<=i<=n)si>wi,不存在(1<=j<=n)sj<wj。我们可以构造出所有(1<=i<=n)si>=wi的情况有ans种,则答案为sum-ans。

(4)已知不存在(1<=i<=n)si>wi,(1<=j<=n)sj<wj。我们可以构造出所有(1<=i<=n)si<=wi的情况有ans1种,我们可以构造出所有(1<=i<=n)si<=wi的情况有ans2种,我们可以构造出所有(1<=i<=n)si<=wi的情况有ans3种。根据容斥我们可以得出答案为sum-(ans1+ans2-ans3)。

注意取模!!!!

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const LL  MOD = 1E9+7;
const LL N = 1e5+15;
const int maxn = 5e5+15;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
char s[N],w[N];
LL mpow(LL a,LL k){
    LL ans=1;
    while(k){
        if(k&1) ans=ans*a%MOD;
        a=a*a%MOD;
        k=k/2;
    }
    return ans;
}
int main(){
    scanf("%d%s%s",&n,s,w);
    int flag1=0,flag2=0;
    LL num=0;
    for(int i=0;i<n;i++){
        if(s[i]!='?'&&w[i]!='?'){
            if(s[i]>w[i]) flag1=1;
            if(s[i]<w[i]) flag2=1;
        }
        if(s[i]=='?')num++;
        if(w[i]=='?')num++;
    }
    LL sum=mpow(10,num);
    if(flag1+flag2==2);
    else if(flag1==1){
        LL ans=1;
        for(int i=0;i<n;i++){
            if(s[i]=='?'||w[i]=='?'){
                if(s[i]=='?'&&w[i]=='?') ans=ans*1ll*55%MOD;
                else if(s[i]=='?') ans=ans*1ll*(10-w[i]+'0')%MOD;
                else ans=ans*1ll*(s[i]-'0'+1)%MOD;
            }
        }
        sum=sum-ans;
    }
    else if(flag2==1){
        LL ans=1;
        for(int i=0;i<n;i++){
            if(s[i]=='?'||w[i]=='?'){
                if(s[i]=='?'&&w[i]=='?') ans=ans*1ll*55%MOD;
                else if(s[i]=='?') ans=ans*1ll*(w[i]-'0'+1)%MOD;
                else ans=ans*1ll*(10-s[i]+'0')%MOD;
            }
        }

        sum=sum-ans;
    }
    else {
        LL ans1=1;
        for(int i=0;i<n;i++){
            if(s[i]=='?'||w[i]=='?'){
                if(s[i]=='?'&&w[i]=='?') ans1=ans1*1ll*55%MOD;
                else if(s[i]=='?') ans1=ans1*1ll*(10-w[i]+'0')%MOD;
                else ans1=ans1*1ll*(s[i]-'0'+1)%MOD;
            }
        }
        LL ans2=1;
        for(int i=0;i<n;i++){
            if(s[i]=='?'||w[i]=='?'){
                if(s[i]=='?'&&w[i]=='?') ans2=ans2*1ll*55%MOD;
                else if(s[i]=='?') ans2=ans2*1ll*(w[i]-'0'+1)%MOD;
                else ans2=ans2*1ll*(10-s[i]+'0')%MOD;
            }
        }
        LL ans3=1;
        for(int i=0;i<n;i++){
                if(s[i]=='?'&&w[i]=='?') ans3=ans3*10ll%MOD;
        }
        sum=sum-(ans1+ans2-ans3);
    }
    sum%=MOD;
    sum=(sum+MOD)%MOD;
    printf("%I64d\n",sum);
    return 0;
}
/*
2
3?
57

2
5?
37
*/
时间: 2024-08-13 23:43:41

Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings的相关文章

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,

Codeforces Round #179 (Div. 2)---C. Greg and Array

Greg has an array a?=?a1,?a2,?-,?an and m operations. Each operation looks as: li, ri, di, (1?≤?li?≤?ri?≤?n). To apply operation i to the array means to increase all array elements with numbers li,?li?+?1,?-,?ri by value di. Greg wrote down k queries

Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists of n steps.

Codeforces Round #247 (Div. 2) ABC

Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/WayToACM/tree/master/CodeForces/431 A - Black Square 题目地址 题意: Jury玩别踩白块,游戏中有四个区域,Jury点每个区域要消耗ai的卡路里,给出踩白块的序列,问要消耗多少卡路里. 分析: 模拟水题.. 代码: /* * Author: illuz

Codeforces Round #433 (Div. 2)

题目链接:Codeforces Round #433 (Div. 2) codeforces 854 A. Fraction[水] 题意:已知分子与分母的和,求分子小于分母的 最大的最简分数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 int gcd(int a,int b){re

Codeforces Round #450 (Div. 2)

Codeforces Round #450 (Div. 2) http://codeforces.com/contest/900 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb

Codeforces Round #580 (Div. 1)

Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了,放个代码吧. #include<bits/stdc++.h> using namespace std; void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-'

【题解】Codeforces Round #600(Div.2)

Codeforces Round #600(Div.2) https://codeforces.com/contest/1253/problem A.Single Push 思路:数组各位相减,得到b-a之后的.如果全为0,或者只有一段非0且数字相同则可行,否则不可行.具体实现的话,可以左右两边指针向中间搜到第一个不为0的数,再判断中间是否均为同一个数.复杂度\(O(n)\). 注意:多组数据一定要判断是否需要清空.这里我a[n+1]没有清0,结果WA on test55-- AC代码: #in

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T