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.
Related
I was doing a programming question and one of the sample output is 64197148392731290. My code for that question is correct as it is giving me the right answers for other test cases (output for those test cases are in single digit).
I understand that there will be too many iterations for the test case which has 64197148392731290 as output. So what should I do to get correct answer for that test case too.
Here is the code :
#include<stdio.h>
#include<string.h>
int main() {
int test_case;long long int i, j, count, n, k, k1;
scanf("%d", &test_case);
while(test_case--) {
scanf("%lld%lld", &n, &k);
char a[n];
count=0;
k1=k;
scanf("%s", a);
while(k1--) {
strcat(a,a);
}
for(i=0;i<(n*k);i++) {
if(a[i]=='a') {
for(j=(i+1);j<(n*k);j++) {
if(a[j]=='b') {
count++;
}
}
}
}
printf("%lld\n", count);
}
return 0;
}
Sample Input and Output :
Input:
3
4 2
abcb
7 1
aayzbaa
12 80123123
abzbabzbazab
Output:
6
2
64197148392731290
My task is to count the number of subsequences "ab" (not necessarily consecutive) in the new string. The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains two integers N and K, denoting the length of the initial string S and the number of repetitions respectively.
The second line contains a string S. Its length is exactly N, and each of its characters is a lowercase English letter.
If you are trying to store input in "int" that wont work coz this number its out of range, change it to "long long int"
Well the previous answer was sure wrong. Thanks for the code.
Sorry don't have time for detailed study but preliminary analysis tells me that maybe the error is because you are trying to store a sting of length 2n in a[n]. It works for smaller values since when you declare
char a[n];
^
variable known at runtime
it actually allocates a large block so that any value of n within range is possible. For large values strcat(a,a) will probably fail.
Basically somewhere down the line the string becomes corrupt. Most probably that is because of strcat. I suggest remove strcat, do something else to a similar effect.
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;
}
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.
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. :)
Problem : Consider the following algorithm to generate a sequence of
numbers. Start with an integer n. If n is even, divide by 2. If n is
odd, multiply by 3 and add 1. Repeat this process with the new value
of n, terminating when n = 1. The input will consist of a series of
pairs of integers i and j, one pair of integers perline. All integers
will be less than 1,000,000 and greater than 0.
For each pair of
input integers i and j, output i, j in the same order in which they
appeared in the input and then the maximum cycle length for integers
between and including i and j. These three numbers should be separated
by one space, with all three numbers on one line and with one line of
output for each line of input.
sample input :
1 10
sample output:
1 10 20
so i wrote this :
#include <stdio.h>
#include <string.h>
struct line{int in1;int in2;int result;};
int cycle(int in);
int main(int argc, char *argv[]) {
int cycle(int in);
char c;
int firstIn=0;
struct line l[500] ;
int pointer=0;
while(2<3){
l[pointer].in1=0;
l[pointer].in2=0;
scanf("%u %u",&l[pointer].in1,&l[pointer].in2);
if(l[pointer].in1<1||l[pointer].in2<1){
break;
}
int maxCyc=0;
int j,m;
int min,max;
if(l[pointer].in1>l[pointer].in2){
max=l[pointer].in1;
min=l[pointer].in2;
}
else{
max=l[pointer].in2;
min=l[pointer].in1;
}
for(j=min;j<=max;j++){
m = cycle(j);
if(m>maxCyc)
maxCyc=m;
}
l[pointer].result=maxCyc;
printf("%d %d %d\n",l[pointer].in1,l[pointer].in2,l[pointer].result);
pointer++;
}
}
int cycle(int in){
int cyc = 1;
while(in>1){
if(in%2==0){
cyc++;
in=in/2;
}
else{
cyc++;
in=in*3+1;
}
}
return cyc;
}
Its completly ok but when you change while(in>1) in cycle method to while(in!=1) it gets much more slower. my question is why?!
Time when its while(in>1) : 0.683 sec
and when its while(in!=1) : I waited more than 5 min nothing
happened yet :)
for input : 1 1000000
there is no infinite loop or something because in cant get below 1 at all(for that it must be already 1) .
Best regards
When you call cycle with the input value 113383, the process eventually sets n to
827370449, and 3*827370449+1 is 2482111348, which is greater than the maximum signed int and is interpreted as -1812855948. So there's your first negative number where there should be no negative number.
If this process then eventually sets n to -2, it will loop infinitely between -2 and -1 from then on. There may be other loops I haven't considered.
If you were to use an unsigned int, there is a possibility (I haven't checked) that this too will overflow eventually, which will not result in a negative value but will result in an incorrect value, invalidating your results.
No matter what integer representation you use, it would probably be a good idea to compare n with (maximum-1)/3 at the top of each loop, where maximum is the largest possible positive value of your integer type, just to be sure you do not overflow.
As you told me it was a simple overflow problem thx everyone.
max int value is 2,147,483,647; So when i changed int cycle(int in) to int cycle(long long int in) my problem was solved.
i also figured it out that my first answer with while(in>1) was wrong.
When an integer overflow occurs,the value will go below 0 .That was the reason while(in!=1) was an infinte loop.
I was really tired that i didn't figure it out by myself. sorry for that :)