A. Remainder
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a huge decimal number consisting of nn digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.
You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.
You are also given two integers 0≤y<x<n0≤y<x<n. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder 10y10y modulo 10x10x. In other words, the obtained number should have remainder 10y when divided by 10x.
Input
The first line of the input contains three integers n,x,y (0≤y<x<n≤2⋅1050≤y<x<n≤2⋅105) — the length of the number and the integers x and y, respectively.
The second line of the input contains one decimal number consisting of n digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1.
Output
Print one integer — the minimum number of operations you should perform to obtain the number having remainder 10的y次幂 modulo 10的x次幂. In other words, the obtained number should have remainder 10的y次幂 when divided by 10的x次幂.
Examples
input
Copy
11 5 2
11010100101
output
Copy
1
input
Copy
11 5 1
11010100101
output
Copy
3
Note
In the first example the number will be 1101010010011010100100 after performing one operation. It has remainder 100100 modulo 100000100000.
In the second example the number will be 1101010001011010100010 after performing three operations. It has remainder 1010 modulo 100000100000.
题意:给你 一个长度为n的字符串,改变了之后的字符串取模10的x次幂等于10的y次幂,求原字符串和新字符串相差了几个数字。
做法:(麻烦得思路):把原字符串后x位放到一个数组里,把10的y次幂的数字放在一个数组里,分别翻转两个数组,遍历10的y次幂那个数组,与10的x次幂数组相比较,如果数字不相同那么sum++,再遍历10的x次幂从10的y次幂结尾开始遍历,到x数组末尾为止,找出有多少个‘1’,有‘1,’,sum++,最后计算sum的个数就是答案。
(简单得思路):可以直接找到一个位置y,使得y为‘1’,y前面的数字如果是‘1’,sum++,如果y后面的数字是‘1’,sum++,最后计算sum的个数就是答案。
麻烦的代码:
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
char a[400005];
char b[400005];
char c[400005];
char x5[400005];
char x2[400005];
int main()
{
int n,x,y;
while(scanf("%d %d %d",&n,&x,&y)!=EOF)
{
int o=0;
scanf("%s",a);
int len = strlen(a);
int g = 0;
for(int i=len-1;i>=0;i--)
{
g++;
b[o++] = a[i];
// printf("%c ",a[i]);
if(g == x)
{
break;
}
}
string c5;
c5 = "1";
for(int i=0;i<y;i++)
{
c5 += "0";
}
// cout <<c5<<endl;
// printf("&&&&&&&&&&&&&&");
// int q= pow(10,y);
// printf("q = %d\n",q);
// itoa(q,c,10);
// printf("c = %s\n",c);
int len2 = c5.size();
int x1=0;
int x3=0;
for(int i=0;i<o;i++)
{
// printf("%c ",b[i]);
x5[x1++] = b[i];
}
// printf("\n");
// for(int i=0;i<x1;i++)
// {
// printf("%c ",x5[i]);
// }
// printf("\n");
for(int i=len2-1;i>=0;i--)
{
x2[x3++] = c5[i];
}
// for(int i=0;i<x3;i++)
// {
// printf("%c ",x2[i]);
// }
// printf("\n");
int ans=0;
for(int i = 0;i < x3;i++)
{
// for(int j=o-1;j>=0;j--)
{
// printf("******%c %c\n",x2[i],x5[i]);
if(x2[i]==x5[i])
{
// printf("77777777777");
// break;
}
else
{
ans++;
// break;
}
}
}
// printf("%d\n",ans);
int gh = x - x3;
// printf("gh = %d\n",gh);
for(int i=x3;i<x1;i++)
{
// printf("b[i] = %c\n",b[i]);
if(b[i]!='0')
{
// printf("ans = %d\n",ans);
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}