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).
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.
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;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- left join 、right join、inner join 区别
- 开始一个React项目(一)一个最简单的webpack配置
- 数据库_mysql(二)
- CF854B Maxim Buys an Apartment【思路】
- 三个实例演示 Java Thread Dump 日志分析
- CodeForces
- JanusGraph
- 复习-数据结构之线性表
- GitChat · 前端 | Webpack 工程的 PWA 实战
- C# 三层架构之系统的登录验证与添加数据的实现
- Struts2 文件上传
- redis-cluster配置攻略 本人亲自尝试
- 去除PDF的水印【9种方法总结】
- up