Automated online judge rejects my solution to math puzzle - c

Hello guys I have found this question on some website
Description
Spade is a very good detective but he is not so good at math, this time his friend Archer has come to him with a very interesting math problem. Given two numbers 1 <= N <= 10^9 and 1 <= M <= 100, how many positive numbers with length N have the sum of its digits divisible by M. Archer is very obsessive and does not want numbers with leading zeroes to count. Spade has hired you to solve this problem, now his reputation is in your hands.
Input specification
Input contains a single line with two numbers N and M separated by a single space.
Output specification
Output a single line with the answer to the problem modulo 1000007.
Sample input
2 2
Sample output
45
My Code
Though I am getting the expected output it is not accepting my answer. Can any one please point out the error in the code.
#include <stdio.h>
int main(void)
{
int n,m,i,firstnum=1,count=0,lastnum=0,j=0,no=0;
scanf("%d",&n);
if (n>=1) {
scanf("%d",&m);
if (m>=1 && m<=100)
{
for (i=1;i<n;++i) {
firstnum*=10;
++count;
}
for (i=0;i<=count;++i)
lastnum=lastnum*10+9;
if (firstnum%m==0)
++no;
for (i=++firstnum;i<=lastnum;++i) {
j=i;
int sum=0,r;
do {
r=j%10;
sum+=r;
j=j/10;
} while(j);
if (sum%m==0)
++no;
}
}
}
printf("%d",no-1);
return 0;
}

You're trying to solve this problem by brute force, which won't work for the sizes involved. A number of length 10^9 can be up to 10^(10^9), which is a huge number that won't fit in an int or even a long long int. Even if it did, trying to enumerate all numbers of this length one by one would take billions of years.
You need to come up with an approach that doesn't look at the numbers one by one. Just like you can calculate that there are 33 numbers between 1 and 100 that are divisible by 3 without looking at them all, you need to come up with such an approach here. But here it will be harder because you will need to do it without actually calculating the value of 10^n.

Your approach is not actually correct.
You have declared "firstnum" variable as int, i.e. it cannot hold value greater than 2^(32-1) (on most of the on line judges). The max value of n in 10^9, hence you are trying to put 10^(10^9) in worst case.
I Hope you have got my point. I you want the approach you can comment below my answer. I don't want to spoil the question for you. :)

Related

Is there a way to write a recursive function that multiplies the even numbers from 1 to n using c?

I've found similar questions that do that to the sums of even numbers, but when i try to change that to the product of these, it always ends up printing out 0.
#include<stdio.h>
int SumEven(int num1, int num2)
{
if(num1>num2)
return 0;
return num1*SumEven(num1+2,num2);
}
int main()
{
int num1=2,num2;
printf("Enter your Limit:");
scanf("%d",&num2);
printf("Sum of all even numbers in the given range is: %d",SumEven(num1,num2));
}
this is an example of one of those i tried to adapt but it only returns 0, any ideas?
You need to post code for a question like this. But the probable cause- when you do a sum, you start with an initial value of 0. When you do products, it needs to be 1. Otherwise you multiply 0*i, which is always 0.
After a bit of thought, I am revising my original answer even though it is a workable solution. Actually, all that you would need to do is change one line of code. Instead of the following linesof code.
if(num1>num2)
return 0;
You just need to do the following.
if(num1>num2)
return 1;
Following were some small limit test results that agreed with manual calculations.
:~/C_Programs/Console/MultEven/bin/Release$ ./MultEven
Enter your Limit:4
Sum of all even numbers in the given range is: 8
:~/C_Programs/Console/MultEven/bin/Release$ ./MultEven
Enter your Limit:8
Sum of all even numbers in the given range is: 384
:~/C_Programs/Console/MultEven/bin/Release$ ./MultEven
Enter your Limit:10
Sum of all even numbers in the given range is: 3840
Try that out.
Regards.

Bizarre situation - 100 digit number input asked

I need to make a program that can take numbers of up to 100 digits as input. No standard int datatype will be able to do that! I've never come across such a bizarre situation.
I don't get it at all. How am I supposed to solve this?
The question I'm working on is this:
A whole number will be given, and you have to make a program that will
determine whether it's an even or odd number.
Input Specification
In the first line, there will be an integer T denoting the number of
testcases. In the following T lines, a non-negative integer will be
given. The number can have a maximum of 100 digits.
Output Specification
For every whole number given, you will have to print whether it's odd
or even as output.
Can anyone guide me on how to solve the problem (if it is even possible to do so)?
The program will take a number as input and determine whether it's odd or even.
Read the input in a string (char [101]) and analyze only last digit to check whether number is odd or even. Rest of the digits are irrelevant for this task.
There is no standard numeric type guaranteed to hold that many digits. You need to store the value in a different way, e.g., as a string or other array. If you need to perform arithmetic on these numbers, you need to implement those operations for the types you use, or use some kind of arbitrary precision library.
(Tip: You also don't necessarily need the entire number for certain operations, e.g., you can tell whether it is even or odd by looking only at the last digit…)
The exercise is to determine whether a whole number of up to 100 digits is odd or even.
This does not require you to perform arbitrary arithmetic on the number, so if you need to handle numbers larger than the largest integer type on your system, you can treat them as a string of digits.
Whether it is even or odd only depends on the last digit.
To all those who took the time and effort to answer this question,
Thanks for the answers. And thanks for showing the way. I greatly appreciate the help!
The solution to the problem which I have coded is -
#include <stdio.h>
#include <string.h>
int main()
{
int T, i, j;
scanf("%d", &T);
for (i=1; i<=T; i++)
{
char N[101];
scanf("%s", N);
int k = strlen(N);
int p = N[k-1] - 48; //char to int conversion
if (p % 2 == 1)
{
printf("odd\n");
}
else
{
printf("even\n");
}
}
return 0;
}

Why i'm getting runtime error in ideone and codechef compiler and not in my terminal?

I have just started competitive programming in SPOJ.I'm confused from sometime why i'm getting runtime error in ideone.The question is:
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
Output
For each K, output the smallest palindrome larger than K.
Example
Input:
2
808
2133
Output:
818
2222
My program:
#include <stdio.h>
int main(void)
{
int t,i,reverse,same;
scanf("%d",&t); //t is no. of test cases
int num[t]; //num[t] is array of t elements
for(i=0;i<t;i++)
scanf("%d",&num[i]);
i=0; //since i will be equal to t therefore i is assigned to 0.
while(t--)
{
if(num[i]<=1000000)
{
while(num[i]++)
{
reverse=0;
same=num[i];
while(same>0)
{
reverse=reverse*10;
reverse=reverse+same%10;
same=same/10;
}
if(reverse==num[i])
printf("%d",reverse);
printf("\n");
if(reverse==num[i])
break;
}
}
i++;
}
return 0;
}
I don't know where i'm wrong.I'm sorry i'm asking this question may this question is asked by someone before.I tried to find the result but could not get the answer.Thankyou in advance and sorry for my bad english.
The question doesn't say that the number will be less than 1000000. It says that the number has less than 1 million digits. A number with a million digits looks like this
591875018734106743196734198673419067843196874398674319687431986743918674319867431986743198674319876341987643198764319876341987643198764319876431987643198763419876431987643198764319876139876...
You can't use scanf to read a number that has a million digits, and you can't store that number in an int.
The most likely reason for your error to occur is some memory fault. Keep in mind that online judges/compilers limit your available memory and if you try to allocate/use more memory than available, you get a runtime error. This also happens on your machine, but usually you have a lot more memory available for your program than in the case of online judges.
In your case, you could reduce the memory usage of your program by changing the data type of the num array from int to something like short or even char.

sum of even terms in the fibonacci series less than 4 million

////Even Fibonacci numbers
int i=2;
int sum_of_Even=0;
int fib_array[]={};
fib_array[0]=1;
fib_array[1]=1;
while (fib_array[i]<4000000)
{
fib_array[i]=fib_array[i-1]+fib_array[i-2];
if ((fib_array[i]%2) == 0)
{
sum_of_Even+=fib_array[i];
}
i++;
}
printf("sum of Even terms in the fib sequence = %i\n", sum_of_Even);
On the terminal, the output is 3.. Help!
Program looks good...but somehow gives an output of 3 (which is pretty wrong)..
Open to suggestions on how to fix this..
Thanks..
The problem is likely here: (Anyway this is a big problem even if it is not the problem.)
int fib_array[]={};
The space allocated for your array in memory will not grow dynamically as you appear to expect. You need to manage the memory for it somehow. Right now you are overflowing this array substantially and it is amazing that this program does not crash or segfault.
Edit: Moreover, every time your while loop checks its condition for whether to run again, it accesses an entry in your array which has not been initialized! Note:
On the first run, it checks whether fib_array[2] < 4000000, which you are about to set in the body of the loop!
On the second run, it checks whether fib_array[3] < 4000000, which you are about to set in the body of the loop!
Etc.
Edit 2: Since (amazingly many) people have been posting that you need to use a 64 bit integer and that this is the source of your problems, I'd like to make the clarifying remark that the answer is in the 5 million range, so a 32 bit integer is plenty big.
fib_array[i] is not initialized when you access it in the while() test. Try changing your while loop to while(true) and using if (fib_array[i]<4000000) break; on the line after you set fib_array[i].
Also, your sum_of_Even is going to need to be a 64 bit integer, so you'll need:
#include <stdint.h>
and then declare it as uint64_t.
Another problem is you aren't declaring how large your fib_array should be, so no actual memory is being allocated to it. Try int fib_array[MAXSIZE]; where MAXSIZE is your calculation for how many entries it will need.
Suggestions on how to fix:
And mindful of #Andrey comment:
"For the record: this is a Project Euler question, and people are strongly discouraged from posting solutions to these online."
Use an array of long long fib_array[3]. To generate Fibonacci numbers only the previous 2 are used to generate the 3rd. After generating a Fibonacci number and testing for evenness, now that you have 3 Fibonacci numbers, discard the eldest and repeat.
OP's present int fib_array[]={}; is not allocating the array needed for OP's approach. fib_array[i]= causes UB.
Per #Jules solution, even a long may be insufficient, consider long long. In anycase , use the matching prinf() format specifier. Edit: Turns out the answer is < 5,000,000, so long will work.
long sum_of_Even = 0;
....
printf("sum of Even terms in the fib sequence = %li\n", sum_of_Even);
#include <stdio.h>
#include <string.h>
int main()
{
long i,s=0,f[50];
f[0]=1;
f[1]=1;
for (i=2;f[i]<4000000;i++){
f[i] = f[i-1] + f[i-2];
if (f[i]%2 == 0){
printf("%ld ",f[i]);
s += f[i];
}
}
printf("SUM = %ld\n",s);
return 0;
}

Binomial coefficient in C

Here you can find the problem I'm trying to solve:
For integers n and k (0<=k<=n<1001) determine (binomial coefficient).
Input
The first line of the standard input contains one integer t (t<1001) which is the number of test cases.
In each of the next t lines there are numbers n and k.
Output
For each test print (binomial coefficient).
Example:
Input
3
0 0
7 3
1000 2
Output:
1
35
499500
I can't seem to find anything wrong in my solution (other than it's written very poorly - I've started programming quite recently):
#include <stdio.h>
int main()
{
unsigned long int t,n,k,binomial=1;
unsigned long int number=1;
for(scanf("%lu",&t);t>0;t--)
{
scanf("%lu%lu",&n,&k);
if(k<(n/2)) k=n-k;
for(binomial=1,number=1;n>k;k++)
{
binomial=binomial*(k+1)/number;
number++;
}
printf("%lu\n",binomial);
}
return 0;
}
It works fine for the example input, but the solution is judged via a problem site
(http://www.spoj.pl/SHORTEN/problems/BINOMIAL/english/)
and the solution is not accepted. I tried other inputs too and all of them gave back the right output. My question is: Is there a reason why this solution is invalid?
As 1000C500 is around 300 digits, it cant be stored in an unsigned long. In short, you need to start over and think of a better technique.

Resources