Page 2 of 2

Re: 748 - Exponentiation

Posted: Mon Feb 20, 2012 11:34 am
by asitmahato
Please help me to solve this questions..

Basically i want to know the table method.

can any one explain it to me here or any kind of link..

Thank you very much in advance..

Re: 748 - Exponentiation

Posted: Tue Feb 21, 2012 8:30 am
by brianfry713
Use JAVA's BigDecimal.

748 - Exponentiation

Posted: Sat Feb 09, 2013 12:42 am
by szstream
I have been doing problem 748 for several days, but I still couldn't get a AC and I always get a WA.It really drove me crazy since I had submitted it for 12 times.And I had tried hard to find out what the problem is.But there is no clue I can begin with because my sample input and out are exactly the same as the problem shows.Also I had tried other data which could not prove my program is wrong.So I am here to seek for help.Please help me find out what the problem is or provide some test data for me. Thank you in advanced.My code are as follow:

Code: Select all

#include<iostream>
#include<stdio.h>
#include<cstring>
#define max 250
using namespace std;
int c[max];
int temp[max];
int len;
int dot(char* oc,int nc[])
{
	int j=1,i,k=0;
	int oclen=strlen(oc)-1;
	for(i=oclen;i>=0;i--)
	{
		if(oc[i]=='.')
		{
			k=j-1;
		  continue;
		}
		nc[j++]=oc[i]-'0';
	}
	return k;
}
void multiply(int* a,int* b)
{
	memset(c,0,sizeof(c));
	int i,j;
	for(i=max;b[i]==0;i--);
	int lb=i;
	for(i=6;a[i]==0;i--);
	int la=i;
     len=la+lb;
	for(i=1;i<=la;i++)
	for(j=1;j<=lb;j++)
	c[i+j-1]=c[i+j-1]+a[i]*b[j];
	for(i=1;i<len;i++)
	{
	c[i+1]=c[i+1]+c[i]/10;
	c[i]=c[i]%10;
	}
	if(c[len]==0)
	len--;
}
int main()
{
		char s[7];
		int n;
	while(scanf("%s",s)!=EOF)
	{
		cin>>n;
		int a[7]={0},i,j;
		int p=dot(s,a);
		for(i=6;i>=0&&a[i]==0;i--);
		if(i==-1||n==0)
		{
			if(!n)
			cout<<1<<endl;
			else
			cout<<0<<endl;
			memset(c,0,sizeof(c));
		    memset(temp,0,sizeof(temp));
		    len=0;
		   continue;
		}
	    for(i=1;i<7;i++)
		c[i]=a[i];
		int ok=0;
		for(j=1;j<n;j++)
		{   
		for(i=0;i<max;i++)
		 temp[i]=c[i];
		multiply(a,temp);
		ok=1;
		}
		if(ok)
		{
		while(c[len]==0&&len>p*n)
		len--;
		for(i=len;i>p*n;i--)
		cout<<c[i];
		int down=1;
		while(c[down]==0&&down<=p*n)
		down++;
		if(down!=p*n+1&&p!=0)
         cout<<'.';
		for(i=p*n;i>=down;i--)
		cout<<c[i];
		}
		else
		{
		int up=6;
		while(c[up]==0&&up>p)
			up--;
		for(i=up;i>p;i--)
			cout<<c[i];
		int down=1;
		while(c[down]==0&&down<=p)
			down++;
		if(down!=p+1&&p!=0)
			cout<<'.';
		for(i=p;i>=down;i--)
			cout<<c[i];
		}
		cout<<endl;
		memset(c,0,sizeof(c));
		memset(temp,0,sizeof(temp));
		len=0;
	}
return 0;
}

Re: 748 - Exponentiation

Posted: Tue Feb 12, 2013 12:11 am
by brianfry713
On my Linux machine your code is printing this for the sample input:

Code: Select all

242563650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000548815620517731830194541.899025343415715973535967221869852721
198192620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000005148554641076956121994511276767154838481760200726351203835429763013462401
432159972000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043992025569.928573701266488041146654993318703707511666295476720493953024
552811692000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000029448126.764121021618164430206909037173276672
754769652000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090429072743629540498.107596019456651774561044010001
780524652000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001.126825030131969720661201

Re: 748 - Exponentiation

Posted: Sat Feb 16, 2013 8:40 pm
by szstream
brianfry713 wrote:On my Linux machine your code is printing this for the sample input:

Code: Select all

242563650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000548815620517731830194541.899025343415715973535967221869852721
198192620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000005148554641076956121994511276767154838481760200726351203835429763013462401
432159972000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043992025569.928573701266488041146654993318703707511666295476720493953024
552811692000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000029448126.764121021618164430206909037173276672
754769652000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090429072743629540498.107596019456651774561044010001
780524652000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001.126825030131969720661201
but on my windows machine my code is printing the sample output using visual c++ .

Re: 748 - Exponentiation

Posted: Sun Feb 17, 2013 1:44 am
by lbv
szstream wrote: but on my windows machine my code is printing the sample output using visual c++ .
I'm using g++ on a linux machine, and your program shows crazy output when compiled without any special flags, but seems correct (by chance) when compiled with basic optimizations (-O2). This is usually a sign of memory corruption somewhere.

Taking a look at your program, there are suspicious things around your code. For example, you declare temp as an array of size max. You pass temp as the second argument to the multiply function (called b in there). But then you do this:

Code: Select all

    for (i = max; b[i] == 0; i--);
Can you see the problem? Keep in mind that the only valid positions in an array of size N are 0 to (N-1). Accessing an invalid location of memory has unpredictable results.

That's not the only suspicious bit of code in your program. What if (la + lb) happens to be greater than max? Notice the size of your c array.

Take your time and do a thorough review of all your code.

Re: 748 - Exponentiation

Posted: Tue Feb 19, 2013 9:41 pm
by szstream
lbv wrote:
szstream wrote: but on my windows machine my code is printing the sample output using visual c++ .
I'm using g++ on a linux machine, and your program shows crazy output when compiled without any special flags, but seems correct (by chance) when compiled with basic optimizations (-O2). This is usually a sign of memory corruption somewhere.

Taking a look at your program, there are suspicious things around your code. For example, you declare temp as an array of size max. You pass temp as the second argument to the multiply function (called b in there). But then you do this:

Code: Select all

    for (i = max; b[i] == 0; i--);
Can you see the problem? Keep in mind that the only valid positions in an array of size N are 0 to (N-1). Accessing an invalid location of memory has unpredictable results.

That's not the only suspicious bit of code in your program. What if (la + lb) happens to be greater than max? Notice the size of your c array.

Take your time and do a thorough review of all your code.
Thank you for help.Thanks to your advice I have solved this problem.

Re: 748 - Exponentiation

Posted: Mon Mar 11, 2013 10:22 pm
by shuvokr
WA

Code: Select all

import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;
class Main
{
    public static void main(String args[])
    {
        Scanner s = new Scanner (System.in);
         BigDecimal R = new BigDecimal("0");
        int n;
        while(s.hasNext())
        {
            R = s.nextBigDecimal();
            n = s.nextInt();
            R = R.pow(n);
            R = R.stripTrailingZeros();
            System.out.println(R);
        }
    }
}

Re: 748 - Exponentiation

Posted: Mon Mar 11, 2013 10:46 pm
by shuvokr
Doesn't match the sample I/O. You need to write your own code to print the output.

Re: 748 - Exponentiation

Posted: Mon Feb 24, 2014 1:42 pm
by uDebug
On this problem, examine the following line from the sample output carefully:

Code: Select all

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

Re: 748 - Exponentiation

Posted: Wed Apr 02, 2014 6:10 pm
by ultima_key

Code: Select all

95.123 12
548815620517731830194541.899025343415715973535967221869852721
0.4321 20
0.00000005148554641076956121994511276767154838481760200726351203835429763013462401
5.1234 15
43992025569.928573701266488041146654993318703707511666295476720493953024
6.7592 9
29448126.764121021618164430206909037173276672
98.999 10
90429072743629540498.107596019456651774561044010001
1.0100 12
1.126825030131969720661201
^This one is the output of the program below. Please help.

Code: Select all

import java.math.*;
import java.util.*;

class Main{
	public static void main(String[] args){
		BigDecimal R;
		int n;
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			R = cin.nextBigDecimal();
			n = cin.nextInt();
			BigDecimal s = R.pow(n);
			s = s.stripTrailingZeros();
			System.out.println(s.toPlainString());
		}
	}
}
I don't understand why the judge verdicts my code with a WA. It is correct in all the sample input specified in the problem. Please help. :D

Re: 748 - Exponentiation

Posted: Wed Apr 02, 2014 7:29 pm
by uDebug
ultima_key wrote: ^This one is the output of the program below. I don't understand why the judge verdicts my code with a WA. It is correct in all the sample input specified in the problem.
The output you posted above does not match the sample output provided in the problem statement.

As a hint look at the post above yours carefully.