Description
You’re given two positive integers A and B, and a string P, representing their product but with a missing digit indicated with an ∗. Find the missing digit.
It’s guaranteed that the missing digit isn’t 0.
Input
The first line contains three integers a, b and
p
(
1
≤
a
,
b
<
1
0
6
,
1
≤
p
<
2
∗
1
0
6
)
p (1≤a,b<10^6, 1≤p<2∗10^6)
p(1≤a,b<106,1≤p<2∗106) − the number of digits in A, B and P respectively.
The second line contains positive number A.
The third line contains positive number B.
The fourth line contains string P − the product of A and B with a missing digit.
Output
Print one integer representing the missing digit in P.
Examples
Input
1 1 2
3
8
2*
Output
4
Input
2 2 3
10
10
*00
Output
1
Solution
① 可以Hash乱搞,和2020 HDU多校的某题基本一样
② 任何非负整数模9同余与其每位上数字和(很好证明所以忽略)
所以可以求出a%9 , b%9 的值,再枚举c中缺少的数,若此时(a*b)%9 == c % 9 则就找到了答案
正是题目保证了缺少的数不是0,这种方法才有用
Hint
这题卡时间+空间,除了线性解法应该都过不去
Code 1
const int base1 = 10;
const int base2 = 10;
const int mod1 = 1e9 + 7;
const int mod2 = 19260817;
int na,nb,nc;
char a[maxn], b[maxn], c[maxn];
int p1[maxn], p2[maxn];
void init(){
p1[0] = p2[0] = 1ll * 1;
for(int i = 1;i < maxn;++i) {
p1[i] = (1ll * p1[i-1] * base1) % mod1;
p2[i] = (1ll * p2[i-1] * base2) % mod2;
}
}
void solve(){
init();
ll suma1 = 0, suma2 = 0;
ll sumb1 = 0, sumb2 = 0;
ll sumc1 = 0, sumc2 = 0;
for(int i = 1;i <= na;++i) {
suma1 = (suma1 * base1 + (a[i] - '0')) % mod1;
suma2 = (suma2 * base2 + (a[i] - '0')) % mod2;
}
for(int i = 1;i <= nb;++i) {
sumb1 = (sumb1 * base1 + (b[i] - '0')) % mod1;
sumb2 = (sumb2 * base2 + (b[i] - '0')) % mod2;
}
int pos = 0;
for(int i = 1;i <= nc;++i) {
if(c[i] == '*') {pos = i; c[i] = '0';}
sumc1 = (sumc1 * base1 + (c[i] - '0')) % mod1;
sumc2 = (sumc2 * base2 + (c[i] - '0')) % mod2;
}
ll res = 0;
for(ll i = 1;i < 10;++i) {
if((suma1*sumb1)%mod1 == (sumc1+(i*p1[nc - pos])%mod1+mod1)%mod1) {
if((suma2*sumb2)%mod2 == (sumc2+(i*p2[nc - pos])%mod2+mod2)%mod2)
res = i;break;
}
}
printf("%lld\n", res);
}
int main() {
scanf("%d%d%d",&na,&nb,&nc);
scanf("%s %s %s",a+1,b+1,c+1);
solve();
return 0;
}
Code 2
int na,nb,nc;
char a[maxn], b[maxn], c[maxn];
int main() {
scanf("%d%d%d",&na,&nb,&nc);
scanf("%s %s %s",a+1,b+1,c+1);
int suma = 0, sumb = 0, sumc = 0;
for(int i = 1;i <= na;++i) suma += a[i] - '0', suma%=9;
for(int i = 1;i <= nb;++i) sumb += b[i] - '0', sumb%=9;
for(int i = 1;i <= nc;++i) if(c[i] != '*') sumc += c[i] - '0', sumc%=9;
for(int i = 1;i < 10;++i) {
if((suma * sumb)%9 == (sumc + i) % 9) {
printf("%d\n", i); return 0;
}
}
return 0;
}