Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Sunday, March 30, 2014

SIRNUMS-SIR CHIRAG AND MAGIC NUMBERS

SIR CHIRAG AND MAGIC NUMBERS

given below code is for sirnums spoj or sir chirag and magic numbers spoj.
if you want logic mail me. email :- raj.nishant360@gmail.com
#include <bits/stdc++.h>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--){
 int k,x;
 scanf("%d%d",&k,&x);
 char a[1000009];
 int temp,carry,temp1;
 int flag=0;
 for(int i=x;i<=9;i++)
 {
  a[k-1]=i+48;
  carry=0;
  for(int j=k-2;j>=0;j--)
  {
   a[j]=((a[j+1]-48)*x + carry)%10 + 48;
   carry = ((a[j+1]-48)*x + carry)/10;
  }
  a[k]=0;
  temp=a[0]-48;
  temp1= (temp*x + carry)%10;
  carry=(temp*x + carry)/10;
  if(temp1 == i && carry==0){
   printf("%s\n",a);
   flag=1;
   break;
  }
 }
 if(!flag)
  printf("Impossible\n");
 }
 return 0;
}

No comments:

Post a Comment

Your comment is valuable to us