Page 2 of 6

Posted: Tue Jun 24, 2003 7:27 pm
by czar
Nevermind, I found the problem

10139 - Factovisors

Posted: Sun Sep 14, 2003 5:40 pm
by raiku
Hi! Can someone tell me why i'm gettin a WA? Which kind of input I fail?

Thanks

[cpp]

#include <stdio.h>

#include <math.h>
#include <iostream>

#define MAX 46400

int sieve[MAX], primers[5000];

using namespace std;

long n, d, d2;


int fer_primers(void)
{
int i, j, z=1;
int s=0;

for(i=0; i<MAX; i++)
sieve=1;
sieve[0]=0;
sieve[1]=0;
primers[0]=2;

for(i=4; i<MAX; i=i+2)
sieve=0;
for(i=3; i<MAX; i=i+2)
if(sieve==1)
{
primers[z]=i;
z++;
for(j=2*i; j<MAX; j=j+i)
sieve[j]=0;
}
return z;
}



int main(int argc, char *argv[])
{
bool div;
int h, k, r, num;
int a=0, q=0, b=0, w=0;
unsigned long u;
int nprimers;

nprimers=fer_primers();



while(cin>>n>>d)
{
d2=d;
if(d==0) div=false;
else if(d==1) div=true;
else if(n==1) div=(d==1);
else if(d<=n) div=true;
else
{
div=true;
h=0;
r=0;
k=2;

while(r<nprimers && div && d>1)
{
k=primers[r];
h=0;
while(d%k==0)
{
h++;
d=d/k;
}

num=0;
u=k;
while(u<n && num<h)
{
num=num+(n/u);
u=u*k;
}
if(num<h) div=false;
r++;
}

if(d>1) div=false;
}
div? cout<<d2<<" divides "<<n<<"!"<<endl: cout<<d2<<" does not divide "<<n<<"!"<<endl;
}

return 0;
}

[/cpp]

cannot handle large values.

Posted: Fri May 07, 2004 8:16 pm
by koodeGuru
I tested your program for some huge fact values and it does not work
1009 1000
1000 divides 1009!
1000 1009
1009 does not divide 1000!
>>1073741824 1000000
>>1000000 divides 1073741824!
Actualy it should be 1000000 does not divide 1073741824! which is very obvious. Even I am having some problems handling large factorials. Your program can actualy handle 1000! which is good. Mine does not even handle 500! How sad :cry:
Someone help.Thanks.

Code: Select all

Java rules

10139-Factovisors using BigInteger

Posted: Fri May 07, 2004 9:20 pm
by koodeGuru
Here is my code for this problem:

Code: Select all

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

class Main
{
	public static void main(String []args)
	{
		Main code=new Main();
		code.execute();
	}
	
	void execute()
	{
		String s;
    	StringTokenizer st;
    	BigInteger i, j;
		
		while ((s = Main.readLn(255)) != null) 
    	{
	      st = new StringTokenizer(s);
	      i = BigInteger.valueOf(Integer.parseInt(st.nextToken()));
	      j = BigInteger.valueOf(Integer.parseInt(st.nextToken()));
	      factovisor(i,j);
	    }
	}
	
	static String readLn (int maxLg) 
	{
    	byte lin[] = new byte [maxLg];
    	int lg = 0, car = -1;
    	//String line = "";
 
    	try 
    	{
      		while (lg < maxLg) 
      		{
        	car = System.in.read();
        	if ((car < 0) || (car == '\n')) break;
        	lin [lg++] += car;
       		}
    	}
    	catch (IOException e) 
    	{ return (null); }
 
    	if ((car < 0) && (lg == 0)) return (null);
    	return (new String (lin, 0, lg));
  	}
  	
  	void factovisor(BigInteger i, BigInteger j)				//i=n and j=m;
  	{
  		BigInteger facti= BigInteger.valueOf(1);
  		//System.out.println(fact(i).toString());
  	 	for (int k = 2; k <= i.intValue(); k++) 
  	 	{
               BigInteger num = BigInteger.valueOf(k);
               facti = facti.multiply(num);
        }

  		if(facti.mod(j).intValue()==0)
  		System.out.println(j+" divides "+i+"!");
  		else
  		System.out.println(j+" does not divide "+i+"!");
  	}
  	
 }
It is taking a lot of time to calculate large factorial values. How do I minimize that? Thanks. :(

Posted: Mon May 10, 2004 5:29 am
by shamim
Are u sure that
1000000 does not divide 1073741824! .
I mean 1073741824 is larger than 1000000, therefore 1073741824! has got to be divisiblle by 1000000 :-?

shamim is right

Posted: Mon May 10, 2004 11:54 am
by sohel
Hmmm...

I think Shamim is right.. cos my AC program gives
1000000 divides 1073741824! , as it should.

Raiku:

you made a very minor mistake, I just changed one condition and got it AC....

Hint: The change is in this line

[c]
if(d>1) div=false;
[/c]

Hope it helps.

o o!

Posted: Tue May 11, 2004 4:52 am
by koodeGuru
I am sorry for the confusion. I was wrong. [java]Test java[/java][/java]

Posted: Wed Jun 02, 2004 9:10 am
by Amir Aavani
dear htl
consider the case where m has a prime factor which is bigger than Sqrt (2^31).
i think in these cases your code is wrong

10139

Posted: Fri Jun 04, 2004 7:42 am
by ..
I can't find any bug in my old accepted code........
0 doesn't divded any factorial, right?~

Posted: Fri Jun 04, 2004 10:09 am
by Andrey Mokhov
Hi!

0 doesn't divide anything.
(But take care of 0!=1)

After rejudge I got TLE. I rewrote factorization and got AC.
But before I got WA 'cause I outputted "divids" instead of "divides" :roll: - I had been looking for this stupid bug for about half an hour...

Don't you have something like that?

Have AC.
Andrey.

10139 WA after rejudgement

Posted: Fri Jun 04, 2004 1:08 pm
by WR
Hello,

after the rejudgement my program gets a WA.
Could anybody please verify the following data?

input:

Code: Select all

0 0
1 0
2 0
10 0
2147483647 0
0 1
1 1
2 1
10 1
2147483647 1
10 2
2147483647 2
10 3
5 5
6 9
0 10
6 18
6 27
1009 1000
1000 1009
20 10000
20 100000
1073741824 1000000
43213 93390991
123456789 123456871
123456789 123456873
123456789 123456790
123456789 124880621
123456789 124925329
43213 906190609
1073741824 1073741824
2147483647 1073741824
0 2147483647
1 2147483647
2 2147483647
1073741824 2147483647
2147483647 2147483647
output:

Code: Select all

0 does not divide 0!
0 does not divide 1!
0 does not divide 2!
0 does not divide 10!
0 does not divide 2147483647!
1 divides 0!
1 divides 1!
1 divides 2!
1 divides 10!
1 divides 2147483647!
2 divides 10!
2 divides 2147483647!
3 divides 10!
5 divides 5!
9 divides 6!
10 does not divide 0!
18 divides 6!
27 does not divide 6!
1000 divides 1009!
1009 does not divide 1000!
10000 divides 20!
100000 does not divide 20!
1000000 divides 1073741824!
93390991 divides 43213!
123456871 does not divide 123456789!
123456873 divides 123456789!
123456790 divides 123456789!
124880621 divides 123456789!
124925329 divides 123456789!
906190609 does not divide 43213!
1073741824 divides 1073741824!
1073741824 divides 2147483647!
2147483647 does not divide 0!
2147483647 does not divide 1!
2147483647 does not divide 2!
2147483647 does not divide 1073741824!
2147483647 divides 2147483647!
Many thanks in advance!

Posted: Fri Jun 04, 2004 9:12 pm
by tat tvam asi
helo WR!

I checked your output and found nothing wrong,
so you may use the following random io
(only the input random:))

http://morse.inf.unideb.hu/~noszaly/xxx ... _10139.tgz

Peace,
Csaba Noszaly

Posted: Sat Jun 05, 2004 5:36 am
by ..
Thanks for the in/out, but I get the same output........

Posted: Mon Jun 07, 2004 7:20 am
by WR
To tat tvam asi:

Thanks for the data. My program returns absolutely the same output!

So what could now be the problem??!!

Posted: Mon Jun 07, 2004 10:11 am
by alu_mathics
After rejudge i get w.a. I change all the long data into long long and finally got A.C.