748 - Exponentiation

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

asitmahato
New poster
Posts: 11
Joined: Mon Feb 20, 2012 11:04 am

Re: 748 - Exponentiation

Post 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..
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 748 - Exponentiation

Post by brianfry713 »

Use JAVA's BigDecimal.
Check input and AC output for thousands of problems on uDebug!
szstream
New poster
Posts: 4
Joined: Sat Feb 09, 2013 12:05 am

748 - Exponentiation

Post 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;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 748 - Exponentiation

Post 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
Check input and AC output for thousands of problems on uDebug!
szstream
New poster
Posts: 4
Joined: Sat Feb 09, 2013 12:05 am

Re: 748 - Exponentiation

Post 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++ .
lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 748 - Exponentiation

Post 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.
szstream
New poster
Posts: 4
Joined: Sat Feb 09, 2013 12:05 am

Re: 748 - Exponentiation

Post 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.
shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm
Location: Bangladesh

Re: 748 - Exponentiation

Post 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);
        }
    }
}

Code: Select all

enjoying life ..... 
shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm
Location: Bangladesh

Re: 748 - Exponentiation

Post by shuvokr »

Doesn't match the sample I/O. You need to write your own code to print the output.

Code: Select all

enjoying life ..... 
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 748 - Exponentiation

Post by uDebug »

On this problem, examine the following line from the sample output carefully:

Code: Select all

.00000005148554641076956121994511276767154838481760200726351203835429763013462401
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
ultima_key
New poster
Posts: 10
Joined: Tue Mar 25, 2014 12:50 pm

Re: 748 - Exponentiation

Post 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
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 748 - Exponentiation

Post 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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
Post Reply

Return to “Volume 7 (700-799)”