VITA SUM:
Tom the cat is brushing up his Math skills. He has a bag containing N balls of different colors. Now Tom can randomly pick any even number of balls from the bag. Tom wants to find out the sum of all such combinations of balls that he can pull out from the bag. He can pull out at max K balls in one pick.
Input Format:
First line contains two space separated numbers N and K
First line contains two space separated numbers N and K
Output Format:
The output is the sum of all the combinations of balls he can pull out modulo 10^9+7 i.e. (1000000007)
The output is the sum of all the combinations of balls he can pull out modulo 10^9+7 i.e. (1000000007)
Constraints:
- 0<=N,k<=10^14
- N >= k
Sample Input and Output
SNo. | Input | Output | Explanation |
---|---|---|---|
1 | 4 4 | 8 | We need 4C0 + 4C2+ 4C4= 1+6+1=8 |
2 | 8 3 | 29 | We need 8C0 + 8C2= 1+28=29 |
SOLUTION:
#include<stdio.h>
unsigned long long int fact(unsigned long long int);
int main()
{
unsigned long long int k,s=0,i,n;
scanf("%llu %llu",&n,&k);
if(n>0 && n>=k && k<=100000000000000)
{
for(i=0;i<=k;i+=2)
{
s+=(fact(n)/(fact(i)*fact(n-i)));
printf("%llu\n",s);
}
s = s % 1000000007;
printf("%llu",s);
}
return 0;
}
unsigned long long int fact(unsigned long long int n)
{
if(n==1)
return 1;
else if(n==0) return 1;
else
return(n*fact(n-1));
}
VERIFY JSON OBJECT VALIDITY:
A JSON object is a key-value pair data structure that is enclosed within { }. A sample JSON object would look like
{
"key1":"value1",
"key2":"value2",
"key3": {
"key4":"value4",
"key5":"value5"}
"key6":"value6",
"key7":[
{
"key8":"value8"
}]
}
Given a JSON object, ignore the literal values of the object and check whether the distinguishing characters and notation of the object are valid to determine if the JSON object is valid or not.
Note:
{
"key1":"value1",
"key2":"value2",
"key3": {
"key4":"value4",
"key5":"value5"}
"key6":"value6",
"key7":[
{
"key8":"value8"
}]
}
Given a JSON object, ignore the literal values of the object and check whether the distinguishing characters and notation of the object are valid to determine if the JSON object is valid or not.
Note:
- Key3 points to another JSON object (Concept of nesting of JSON objects).
- Key7 points to an array of JSON objects.
Input Format:
- First line contains a pattern of JSON without any literal
Output Format:
Print 1 if pattern is valid, -1 otherwise.
Print 1 if pattern is valid, -1 otherwise.
Constraints:
- A JSON object should start with '{' and ends with a '}'.
- The key and value should be separated by a ':'.
- A ',' suggests an additional JSON property.
- An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself.
Example 1:
Input
{:[{},{}]}
Output
1
Explanation
{
"Key": [{
"Key": "Value"
}, {
"Key": "Value"
}]
}
Pattern is following all constraints hence prints 1
Input
{:[{},{}]}
Output
1
Explanation
{
"Key": [{
"Key": "Value"
}, {
"Key": "Value"
}]
}
Pattern is following all constraints hence prints 1
Example 2:
Input
{:{[]},{}}
Output
-1
Explanation
Convert this pattern in a JSON Object
{
"Key": {
[
"Key": "Value"
]
},
{
"Key": "Value"
}
}
Constraint 4 "An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself." not followed here, so it's a invalid pattern, hence prints -1
Input
{:{[]},{}}
Output
-1
Explanation
Convert this pattern in a JSON Object
{
"Key": {
[
"Key": "Value"
]
},
{
"Key": "Value"
}
}
Constraint 4 "An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself." not followed here, so it's a invalid pattern, hence prints -1
SOLUTION:
#include<stdio.h>
#include<string.h>
int main()
{
char arr[10000];
int i,length,e=0,m=0,l=0;
scanf("%s",arr);
length=strlen(arr);
if(arr[0]!='{' || arr[length-1]!='}')
e++;
for(i=1;i<length-1;i++)
{
if(arr[i]==':' && arr[i+1]=='[' && (m==0))
{
m++;
}
else if(arr[i]==']' && (m==1))
m--;
else if(arr[i]=='{' && (l==0))
l++;
else if(arr[i]=='}' && (l>0))
l--;
else if(arr[i]==',' && arr[i+1] == '{') continue;
else if(arr[i] == '[') continue;
else
e++;
}
if(e!=0 || m!=0 || l!=0)
printf("-1");
else
printf("1");
return 0;
}
Happy Coding!!! :) :P xD
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
Deletesolution for vitasum will give tle
ReplyDeleteinput:-
100000 100000
what was your result when you compiled?
DeleteNee Valga. Un Kulam Valga......
ReplyDeletecan you please tell me in english?
Delete