sumi1234's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
30 (0.03 per day)
Joined:
07-November 11
Profile Views:
1,303
Last Active:
User is offline Aug 26 2013 11:50 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   sumi1234 has not set their status

Posts I've Made

  1. In Topic: Getting segmentation fault in code to find a^b mod m on spoj

    Posted 25 Aug 2013

    View Postjimblumberg, on 25 August 2013 - 01:52 PM, said:

    Please explain the following line:

            b[260] = {0};
    


    There are a couple of things wrong but you need to tell me what you're trying to do before I can offer suggestions on a fix.

    Jim

    I was just checking to see if reinitialising the array to 0 might work. As I was getting a seg fault.

    Basically, I am reading in t, which is the number of test cases.

    For each test case , I take as input 3 numbers a , b and m where a and m are in base 10 and b is in base 3

    I print a^b mod m for each test case

    I have inputted b in the form of a c string .

    convert function converts the c string b into bint in base 10.

    calc function calculates (a^b-1 mod m)*a mod m.

    so for the final result i do result%m again.

    (ab) mod m = ((a mod m)*(b mod m))mod m

    P.S. try using the ideone link. There was some problem while copy pasting so weird symbols like > are there in the program
  2. In Topic: Getting segmentation fault in code to find a^b mod m on spoj

    Posted 25 Aug 2013

    View Postjimblumberg, on 25 August 2013 - 01:21 PM, said:

    I don't know how it is working at all. I get the following error when I try to compile your code:

    Quote

    main.c||In function ‘convert’:|
    main.c|20|error: comparison between pointer and integer|



    Jim


    Sorry about that.. try this one
    #include<stdio.h>
    
    long long calc(long a,long long b,long int m)
    {
        long long temp;
        if(b==0)
            return 1%m;
        temp = calc(a%m,(b>>1),m);
        if(b%2 == 0)
            return (temp*temp)%m;
        else
            return ((a%m)*temp*temp);
            
    }
    
    long long convert(char arr[])
    {
        int i =0 ;
        long long result = 0;
        for (i=0;arr[i] != '\0';i++)
        {
            result = result*3 + (arr[i] - '0');
        }
        return result;
    }
    int main()
    {
        long int t,i,m;
        long long bint;
        scanf("%ld",&t);
        long int a;//base 10
        char b[260];
        //char *endptr;
        for(i=0;i<t;i++)
        {
            scanf("%ld",&a);
            scanf("%s",B)/>;
            scanf("%ld",&m);
            bint = convert(B)/>;
            b[260] = {0};
            if(bint==0)
                printf("%ld",1%m);
            else if(bint == 1)
                printf("%ld",a%m);
            else
            {
                printf("%lld\n",(calc(a%m,bint,m))%m); 
            }       
            
           
              
        }
        return 0;
    }
    


    Here is the ideone link for the same
    http://ideone.com/BxkzQF

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

sumi1234 hasn't added any friends yet.

Comments

sumi1234 has no profile comments yet. Why not say hello?