#include <stdio.h>
#include <string.h>
int len, m, i, j, cl;
bool b;
bool cut(char s[])
{
int len = strlen(s);
if (len % m != 0)
return 0;
cl = len / m;
for (i = 0; i < m-1; i++)
for (j = 0; j < cl; j++)
if (s[i * cl + j] != s[(i+1) * cl + j]) return 0;
return 1;
}
bool isr(int n)
{
char s[100];
sprintf(s, "%d", n);
len = strlen(s);
m = len;
while (!cut(s) && m > 1) m--;
return m-1;
}
int main()
{
int t, n, i;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
i = 1;
while (!isr(n*i)) i++;
printf("%d\n", n*i);
}
return 0;
}
hi , every one. i never write indentation in my code and it is my limition. here is my logic...
for i = 1 to 9
{
may iXiX / iXiXiX ........ /iXXiXX/iXXiXXiXX.......up to iXXXXXXXX
///// X = 1 to 8
///// repeted 2 to 18/lenth
so -> like iXXXiXXXiXXX =>
i000i000i000 +100010001 * XXX = N * n //hear XXX = unknown integer
XXX=(N *n -i000i000i000)/100010001; // N = possible small integer
now , i calculate the ans of XXX ,if it is possible and check for minimum.
shakil wrote:My code pass TimeString input and output . Can any one give some input and output which my previous post code fail.
Thanks in advance.
I don't quite understand your code.
why does your X goes from 1 to 8 instead of 1 to 9?
Also, make sure all of the intermediate calculations don't go out of bound for long long. For this problem, it is very easy to go over the bound if not coded carefully.
There is a simpler way to do this problem.
My code is less than 35 lines long.
var ile,iile,i,leng,numb:longint;
k,w,nn,ret,ans,ww,n,g,tmp:int64;
function nwd(x,y:int64):int64;
begin
if (x=0) then nwd:=y else nwd:=nwd(y mod x,x);
end;
begin
readln(ile);
for iile:=1 to ile do
begin
readln(g);
ans:=-1; ret:=-1;
for leng:=1 to 10 do
for numb:=2 to 9 do
if (numb*leng<=18) then
begin
ans:=-1;
n:=g;
w:=1; k:=1;
for i:=1 to leng do w:=w*10;
for i:=1 to numb-1 do k:=w*k+1;
nn:=nwd(n,k);
n:=n div nn;
if (n<w) then
begin
if (n>=w div 10) then ans:=k*n else
begin
if ((w div 10) mod n=0) then ans:=k*(w div 10) else
begin
tmp:=(w div 10) div n;
if ((tmp+1)*n<w) then ans:=k*(tmp+1)*n;
end;
end;
end;
if (ans<>-1) and (ret=-1) then ret:=ans;
if (ret<>-1) and (ans<>-1) and (ans<ret) then ret:=ans;
end;
repeat
until ret<>-1;
writeln(ret);
end;
end.