#include <bits/stdc++.h>
#define l long long
using namespace std;
int main() {
// your code goes here
l t;
cin>>t;
for(l i=0;i<t;i++)
{
int n,d;
cin>>n>>d;
//cout<<n<<"\n";
string s=to_string(n);
//cout<<s<<"\n";
string s1=s;
string s2="";
vector v1;
for(l j=0;j<s.length();j++)
{
if(int(s[j])-48==d)
{
v1.push_back(j);
}
}
if(v1.size()==0)
{
cout<<0<<"\n";
continue;
}
l x=-1;
int flag=0;
int flag1=0;
if(s.length()==1)
{
if(int(s[0])-48 ==d)
{
cout<<1<<"\n";
}
else{
cout<<0<<"\n";
}
}
else{
if(d==0)
{
int z=0;
for(l j=0;j<s.length();j++)
{
if(int(s[j])-48==d)
{
z=j;
break;
}
}
for(l j=z;j<s.length();j++)
{
s[j]=‘1’;
}
}
else if(d==9)
{
int v=0;
if(s[0]==‘9’)
{
for(l i=0;i<s.length();i++)
{
s[i]=0;
}
s=‘1’+s;
}
else
{
for(int i=0;i<s.length();i++)
{
if(s[i]==‘9’)
{
v=i;
break;
}
}
while(v>0 &&int(s[v])-48 >=8)
{
v–;
}
if(v==0)
{
if(int(s[v])-48>=8)
{
for(l i=0;i<s.length();i++)
{
s[i]=‘0’;
}
s=to_string(1)+s;
}
else{
s[v]=int(s[v])+1;
for(l i=v+1;i<s.length();i++)
{
s[i]=‘0’;
}
}
}
else{
s[v]=int(s[v])+1;
for(l i=v+1;i<s.length();i++)
{
s[i]=‘0’;
}
}
}
}
else{
for(l j=0;j<s.length();j++)
{
if(int(s[j])-48 == d)
{
x=j;
break;
}
}
s[x]=int(s[x])+1;
for(l j=x+1;j<s.length();j++)
{
s[j]=‘0’;
}
}
//cout<<s<<"\n";
cout<<stoll(s)-stoll(s1)<<"\n";
}
}
return 0;
}
These was my solution but I don’t understand where it was failing can anyone help me??
#include <iostream>
#include <algorithm>
#include <vector>
#include <bitset>
#include <math.h>
using namespace std;
void doer(){
int n, d,count=0,ith_percent,ith_digit;
cin>>n>>d;
bool fnd = true;
bool fnd2 = false;
while(fnd){
fnd2 = false;
string s = to_string(n);
int digit_len = s.size();
for(int i = digit_len;i>0;i--){
ith_percent = n%(int)pow(10,i);
ith_digit = ith_percent/pow(10,i-1);
if(ith_digit==d){
fnd2 = true;
count+=(int)pow(10,i-1)-n%(int)pow(10,i-1);
n+=(int)pow(10,i-1)-n%(int)pow(10,i-1);
}
}
if(fnd2==false){
fnd=false;
}
}
cout<<count<<endl;
}
int main() {
// your code goes here
// freopen("inputf.in", "r", stdin);
// freopen("outputf.in", "w", stdout);
int t;
cin>>t;
while(t--){
doer();
}
return 0;
}
1 Like
ronot
October 11, 2021, 3:28pm
5
can anyone tell which corner case I missed
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl “\n”
ll find(ll x,ll num)
{
ll a[x];
ll i=0,sum=0;
while(num!=0 && i<x)
{
a[i]=num%10;
num=num/10;
i++;
}
for(ll j=i-1;j>=0;j--)
{
sum=sum*10+a[j];
}
return sum;
}
int main()
{
ll tc;
cin>>tc;
while (tc--)
{
ll n, d;
cin>>n>>d;
ll count=0,x=0,k=0,num=0,numx=1,que=n,flag=0;
while(n!=0)
{
count++;
if(n%10==d)
{
flag++;
x=count;
}
n=n/10;
}
if(flag==0)
{
cout<<"0"<<endl;
}
else if(x==1)
{
cout<<"1"<<endl;
}
else if(d==0)
{
for(ll i=0;i<x;i++)
{
num=num*10+1;
}
k=find(x,que);
cout<<num-k<<endl;
}
else if((count==x && d!=0) || (count-1==x && d!=0) || x==2 && d!=0)
{
for(ll i=0;i<x-1;i++)
{
numx=numx*10;
}
k=find(x-1,que);
cout<<numx-k<<endl;
}
else
{
for(ll i=0;i<x;i++)
{
numx=numx*10;
}
k=find(x,que);
cout<<numx-k<<endl;
}
}
return 0;
}
#include
#include <math.h>
using namespace std;
int main() {
int T;
cin >> T;
while(T–)
{
int N, D;
cin >> N;
cin >> D;
int arr[10], i = 0, flag = 0,j;
while(N > 0)
{
arr[i] = N % 10;
i++;
N = N/10;
}
for(j = i-1; j>=0; j–)
{
if(arr[j] == D)
{
flag = 1;
break;
}
}
if(flag == 0)
cout <<“0” <<endl;
else
{
if(j == 0)
{
cout << “1” <<endl;
}
else
{
int res = 10 - arr[0];
int a = 1;
for(int k = 1; k < j; k++)
{
res = res + (9 - arr[k])*pow(10,k);
a = a + pow(10,k);
}
if(D == 0)
{
cout << res+a << endl;
}
else
cout << res << endl;
}
}
}
// your code goes here
return 0;
}
can anyone tell me which corner case i missed??
ssjgz
October 11, 2021, 4:52pm
9
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
sam_384
October 11, 2021, 7:02pm
12
https://www.codechef.com/viewsolution/52082921
Can someone tell me what corner case I missed?
can anyone tell me what’s wrong with my approach or what test cases i am missing ?
#include <iostream>
#include<cmath>
using namespace std;
unsigned long long int rem(unsigned long long int n,int d){
unsigned long long int k=0;
unsigned long long int a=0;
int m;
int l=-1;
m=log10(n);
m+=1;
int h=0;
int flag=0;
a=n;
for(int i=0;i<m;i++){
h=n%10;
if(h==d){
l=i;
}
n=n/10;
}
n=a;
if( l==-1){
return 0;
}
else{
for(int i=0;i<m ;i++){
h=n%10;
if(h==d){
if(i<l){
for(int f=0;f<10;f++){
if(h+f>=10 && ((h+f)%10)!=d){
k+=f*pow(10,i);
n+=f;
flag=1;
break;
}
}
if(flag==0){
k+=1*pow(10,i);
n+=1;
}
else{
flag=0;
}
}
else if( i>=l){
k+=1*pow(10,i);
n+=1;
}
}
else{
if(i<l){
a=n/10;
a=a%10;
if(a+1!=d){
for(int f=1;f<10;f++){
if(h+f>=10 && ((h+f)%10)!=d){
k+=f*pow(10,i);
n+=f;
break;
}
}
}
}
}
n=n/10;
if(n==0){
break;
}
}
}
return k;
}
int main() {
unsigned long long int t;
cin>>t;
for(unsigned long long int i=0;i<t;i++){
unsigned long long int n;
int d;
cin>>n;
cin>>d;
d=rem(n,d);
cout<<d<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define mod 1000000007
void testCase(){
ll n, k;
cin>>n>>k;
if(n==k){
cout<<1<<endl;
return;
}
string num = to_string(n);
string sum = “”;
ll carry = 0;
char find = (k+‘0’);
int index = num.size()-1;
if(k==0){
while(index>=0){
if(num[index] == find){
int si = (num.size() - 1 - sum.size());
int ei = index;
while(ei<=si){
if(num[si] == find){
num[si] = (((num[si] - '0') + 1) + '0');
char newChar = '1';
sum = newChar + sum;
}else{
char newChar = '0';
sum = newChar + sum;
}
si--;
}
}
index--;
}
if(sum.size() == 0){
cout<<0<<endl;
return;
}
cout<<sum<<endl;
return;
}
while(index>=0){
if(carry>0){
ll newNum = (num[index] - '0') + carry;
carry = newNum/10;
num[index] = ((newNum%10) + '0');
}
if(num[index] == find){
int si = (num.size() - 1 - sum.size());
int ei = index;
while(ei<=si && (si<=(num.size()-1))){
if(ei == si){
if(carry==0 && (num[si]==find)){
char one = '1';
sum = one + sum;
ll newNum = ((num[si] - '0') + 1);
carry = newNum/10;
num[si] = ((newNum%10) + '0');
}else{
ll newNum = ((num[si] - '0') + carry);
carry = newNum/10;
num[si] = ((newNum%10) + '0');
}
si--;
continue;
}
ll qty = 10 - ((num[si] - '0') + carry);
if(qty==10){
qty = 0;
}
ll newNum = ((num[si] - '0') + qty + carry);
carry = newNum/10;
num[si] = ((newNum%10) + '0');
char newChar = qty + '0';
sum = newChar + sum;
si--;
}
}
index--;
}
if(sum.size() == 0){
cout<<0<<endl;
return;
}
int j = 0;
while((j<sum.size()) && (sum[j]=='0')){
j++;
}
if(j>0){
sum.erase(0, j);
}
cout<<sum<<endl;
}
int main(){
ll t = 1;
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>t;
while(t–){
testCase();
}
}
my code is not getting AC. Could anyone tell me which test case is failing my code?
ssjgz
October 11, 2021, 8:21pm
17
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
I did it in a different way not the most efficient though.
https://www.codechef.com/viewsolution/52123069
The main logic snippet of my solution(without using strings)
ll n,d;
cin>>n>>d;
ll tenPower = 1;
ll sum = 0;
ll original = n;
while(n>0){
if(n%10 == d){
sum = (n+1)*tenPower - original;
n++;
if(d == 0){
sum += (tenPower - 1)/9; // Add the 111.... if d is 0
}
}
tenPower *= 10;
n /= 10;
}
cout<<sum<<endl;
1 Like