https://www.hackerrank.com/challenges/p1-paper-cutting/problem
Explanation:
when we cut n x m paper into n horizontal pieces using n-1 cuts
each of these n horizontal pieces can be cut using m-1 cuts
so number of cuts= n-1+(n*(m-1))
when we simplify n-1+(n*(m-1)) we get n-1+n*m -n =n*m-1
Program:
long solve(long int n, long int m)
{
return (n-1)+(n *(m-1)); // we can simplify it as n-1+nm-n tht is nm-1
}
https://www.hackerrank.com/challenges/diwali-lights/problem
Explanation: each bulb can have 2 states "on" or "off"
if there are n bulbs then we can get 2 power n combinations.
Since we cannot count all bulbs off as pattern so answer is 2 power n-1
Function:
long lights(unsigned long long int n)
{
unsigned long long int i,ans=1;
for(i=1;i<=n;i++)
{
ans =(ans%100000*2)%100000;
}
return ans-1;
}
Explanation:
when we cut n x m paper into n horizontal pieces using n-1 cuts
each of these n horizontal pieces can be cut using m-1 cuts
so number of cuts= n-1+(n*(m-1))
when we simplify n-1+(n*(m-1)) we get n-1+n*m -n =n*m-1
Program:
long solve(long int n, long int m)
{
return (n-1)+(n *(m-1)); // we can simplify it as n-1+nm-n tht is nm-1
}
Diwali Lights
Explanation: each bulb can have 2 states "on" or "off"
if there are n bulbs then we can get 2 power n combinations.
Since we cannot count all bulbs off as pattern so answer is 2 power n-1
Function:
long lights(unsigned long long int n)
{
unsigned long long int i,ans=1;
for(i=1;i<=n;i++)
{
ans =(ans%100000*2)%100000;
}
return ans-1;
}
or complete program is below
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
unsigned long long int n,i,ans=1;
scanf("%llu",&n);
for(i=1;i<=n;i++)
ans =(ans%100000*2)%100000;
printf("%llu\n",ans-1);
}
}
Link to modular arithmetic used in competitive programming is below
https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/tutorial/
Day 2 operators
Problem:
Solution:
diwali light program is not working
ReplyDeleteit is working just fine.
DeleteJust copy and paste it in the code already given and remove the function prototype
or try this
Delete#include
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
unsigned long long int n,i,ans=1;
scanf("%llu",&n);
for(i=1;i<=n;i++)
ans =(ans%100000*2)%100000;
printf("%llu\n",ans-1);
}
}