P.S. Patel
BAN USERstart with no. of 1's as 0 in answer. Then dynamically increase no. of 1's in answer until we get the answer. We will get the ans when answer modulo n becomes zero. use this : (a+b)%n= (a%n + b%n)%n.
let at some time ans is 1111 (it mean we have 1111%n), then
11111%n= (10000 + 1111)%n = (10000%n + 1111%n)%n.
also 10000%n= (10*(1000%n))%n ( use of (a*b)%n= (a*(b%n))%n )
#include"stdio.h"
#define SIZE 100000
int modulo(int num[],int n)
{
int pre_ten=1;
int pre_mod=0;
int i=0,count=0;
while(i<SIZE)
{
count++; i++;
pre_mod=(pre_mod + pre_ten)%n ;
if(pre_mod==0) break ;
pre_ten=(pre_ten*10)%n ;
}
if(i==SIZE) return -1;
return count ;
}
// I assume that answer will come within 100000 digits
int main()
{
int num[SIZE] ;
int i;
for(i=0;i<SIZE;i++)
num[i]=1;
int n;
scanf("%d",&n);
int c=modulo(num,n) ;
printf("%d\n",c);
// c is total 1's in answer
// so we have answer string.
return 0;
}
do typecast of void pointer p to the type of which you want to print value, stored in void pointer. eg, if you want to print int value of binary combination represented by void pointer p, then do typecast like, *(int *)p.
- P.S. Patel August 20, 2013