UVA -- 10127 (Ones)

Solution :

/// UVA -10127
// Problem Name : Ones
#include<stdio.h>
int main()
{
int count,t,n;
int g=11;
while(scanf("%d",&n)==1){
t=10;
count=1;
g=1;
while(g%n!=0){
g=(g*t+1)%n;
count++;
}
printf("%d\n",count);
}
return 0;
}
view raw gistfile1.c hosted with ❤ by GitHub


Another Approach : 


#include<stdio.h>
int long long bigmod(int long long a ,int p,int m);
int main()
{
int long long a;
int long long p,m,n,t;
int c;
while(scanf("%lld",&n)==1){
c=1;
t=10;
a=1;
while(a%n!=0){
a=a*t+1;
c++;
a=bigmod(a,1,n);
}
printf("%d\n",c);
}
return 0;
}
int long long bigmod(int long long a ,int p,int m)
{
if(p==0)
return 1;
if(p%2==0){ // p is even then split it up and mod
int c=bigmod(a,p/2,m);
return ( (c%m) * (c%m) )%m;
}
else // p is odd then make it even
return ( (a%m)* bigmod(a,p-1,m) ) %m;
}

Related Posts: