Pagini recente » Cod sursa (job #2763872) | Cod sursa (job #2278388) | Cod sursa (job #208346) | Cod sursa (job #1936541) | Cod sursa (job #487266)
Cod sursa(job #487266)
#include<fstream>
using namespace std;
ifstream in("next.in");
ofstream out("next.out");
const int N=1000010;
char a[N];
short int z[N];
short int w[N];
long long d;
void read()
{
in.getline(a,N);
in>>d;
}
void invers(short int t[N])//n=vector size
{
/*
short int r[N],n=t[0];
for(short int i=1;i<=n;i++)
r[n-i+1]=t[i];
for(short int i=1;i<=n;i++)
t[i]=r[i];
*/
short int aux;
for(short int i=1,j=t[0];i<j;++i,--j)
{
aux = t[i];
t[i] = t[j];
t[j] = aux;
}
}
void convert()
{
short int i;
z[0]=0;
for(i=0;a[i]!=0;i++)
z[++z[0]]=a[i]-'0';
}
bool imp(short int x[N],long long y,short int q[N])
{
long long val=0,c;
short int i;
for(i=x[0];val<y;i--)
val=val*10+x[i];
q[0]=0;
c=val/y;
val-=c*y;
q[++q[0]]=c;
for(;i>0;i--)
{
val=val*10+x[i];
c=val/y;
val-=c*y;
q[++q[0]]=c;
}
invers(q);
if(val==0)
return true;
return false;
}
void scrie(short int v[N])
{
for(short int i=v[0];i>0;i--)
out<<v[i];
out<<"\n";
}
void suma(short int x[N],short int y)
{
short int i,aux,t=y;
for(i=1;i<=x[0] || t!=0;i++)
{
aux=t+x[i];
x[i]=aux%10;
t=aux/10;
}
x[0]=i-1;
}
void produs (short int x[N],short int y)
{
short int i,aux,t=0;
for(i=1;i<=x[0] || t!=0;i++)
{
aux = t + x[i]*y;
x[i] = aux%10;
t = aux/10;
}
x[0] = i-1;
}
int main()
{
read();
convert();
invers(z);
if(imp(z,d,w)==true)
out<<a;
else
{
suma(w,1);
// scrie(w);
produs(w,d);
scrie(w);
}
return 0;
}