# Help in VIIT CP-DIZHARD Division question

can someone help me with this question?
question link - CodeChef: Practical coding for everyone
my approach is this. I’m not getting the answer.

``````#include<bits/stdc++.h>
typedef long long ll;
#define endl "\n"
using namespace std;

ll sum(ll n)
{
ll m,sum=0;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
return sum;
}

void sol()
{
ll  k,a,b;
cin>>k>>a>>b;// example - 6 10 7
long double x= (a/b);// 1
long double f=  double(a)/ double(b); // 1.42857
long double removed_part = (f-x);// 1.42857 - 1 = 0.42857

long double ok =removed_part*pow(10,k); //   428571

string temp = to_string(ok); // in string 42857
string r = temp.substr(0, k); //extract the first k  charcters

ll xx= stoll(r);// to int
cout<<sum(xx)<<endl;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t=1;
cin>>t;
while(t--)
{
sol();
}
return 0;
}

``````

Try this test case
1
200 2 3

1 Like

T=int(input())

for i in range(T):
K=int(input())
A,B=map(int,input().split())
x=A/B
n=x-1
H=round(n,K)
H=H*(10**K)

``````sum=0
i=0
while(H!=0):
sum=sum+H%10
H=H//10
i=i+1
print(int(sum))
``````

Op:
1
200
2 3
763

1 Like

I think when you do this,

might just result in an overflow as (1<=k<=500).
You can do this in an alternate way like this (it’s just a normal implementation of school level division).

void solve(){
int t;
cin>>t;
while(t–){
int k,a,b,rem,temp=0,sum=0;
cin>>k>>a>>b;
rem=a%b;
if(rem){
rem*=10;
while(temp!=k){
// cout<<rem/b<<" ";
sum+=rem/b;
rem%=b;
rem*=10;
temp++;
}
}
cout<<sum<<nl;
}
}

Hope this helps

1 Like

yep , out of bounds

thanks vats,