How to find count of numbers between a and b (inclusive) which contains 0 as their digit. I am not able to get this with the idea which i have used below in my code, it becomes very complex in case of leading zeroes
for ex if a=1 and b=200 total number should be 29 but i am getting 39.
Can anyone suggest an efficient way of doing it?
constraints:
1<=a<=b<=10^17
code:
long long int F(long long int dig, long long int a, long long int num)
{
if(dig == 0) {
if(a>0)
return 1;
else
return 0;
}
if(mem[dig][a])
return D[dig][a];
mem[dig][a] = 1;
long long int ret = 0;
for(long long int i = 0;i<=9;i++) {
if(i==num)
ret=(ret+F(dig-1,a+1,num));
else
ret=(ret+F(dig-1,a,num));
}
D[dig][a] = ret;
return ret;
}
long long int solve(long long int x,long long int num)
{
char cad[100];
long long int ret = 0;
long long int a=0,b=0,c=0,j;
sprintf(cad,"%lld",x);
int len = strlen(cad);
long long int qued = len;
for(long long int i = 0;i < len;i++) {
qued--;
long long int d = cad[i] - '0';
for(j=0;j <d;j++) {
if(j==num) {
if(num==0 && i==0)
a=a+0;
else
a=a+1;
ret=(ret+F(qued,a,num));
}
else
ret=(ret+F(qued,a,num));
}
if(d==num)
a=a+1;
}
return ret;
}
solution is -> solve(b+1,0)-solve(a,0)
but i am getting wrong answer with this
the link from which i got the above idea is http://codeforces.com/blog/entry/8221
A Simple way to do it would be:
inList = [1, 2, ... n]
outList = []
for i in inList:
for j in len(i):
if i%10 || i==0:
//there is a 0
outList.add(i)
break;
if(i<10)
//i cannot contain a 0
break;
i=i/10
Probibly only works with positive ints, but its trivial to account for negitive numbers.
Simple and efficient O(10N) or O(17N) according to your constraints
Related
I made a function to double every other digit of a number, but for some reason it converts the in to hex before returning. I checked, and that hex is accurate to an actual number, but how do I get it to stop returning hex? Here's the code.
unsigned long long* Luhn_Algorigthem::double_every_other_value(unsigned long long int in) {
unsigned long long int* out = new unsigned long long;
int counter = 10;
for (int i = 0; i < std::to_string(in).length(); i++) {
if (i % 2 == 0) { //Is even
counter * 10;
out += (unsigned long long)((in % counter) * 2);
}
else { //Is odd
out += (unsigned long long)(std::to_string(in).at(i));
}
}
doubled_val = (unsigned long long)out;
return (unsigned long long*)33;
delete out;
}
Unsigned and long are type modifiers (like adjectives without a noun), have you tried unsigned long int for explicit type casting?
#TheUndedFish helped solve the problem for me, I ended up just getting rid of the pointer, and used the doubled val variable for it instead without allocating memory to the heap. This is the updated code:
unsigned long long int Luhn_Algorigthem::double_every_other_value(unsigned long long int in) {
//unsigned long long int* out = new unsigned long long;
int counter = 10;
for (int i = 0; i < std::to_string(in).length(); i++) {
if (i % 2 == 0) { //Is even
counter * 10;
doubled_val += (in % counter) * 2;
}
else { //Is odd
doubled_val += std::to_string(in).at(i);
}
}
//delete out;
return doubled_val;
}
You have N different balls numbered from 1 to N, and M different boxes numbered from 1 to M.
Input:
First line of input contains the number of test cases T. After that, next T lines contain the value of N and M.
Output:
For each test case, print the answer. As it can be very large, you should print it modulo 10^9 + 7.
I tried the below code, but it gives an error:
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
unsigned short int T;
unsigned long int N,M;
cin>>T;
for (int i = 0; i < T; i++)
{
cin>>N>>M;
long int res;
res= pow(M,N);
int c=0;
c=pow(10,9);
res=res%(c + 7);
cout<<res<<endl;
}
return 0;
}
You must be facing integer overflow problem, that's why you must have been getting wrong answer.
Do the following steps to fix this problem.
change the unsigned long to long long or unsigned long long. (Why? Think).
Use the logarithmic user-defined function to calculate the value of the res = pow(M,N) along with the modulo consideration side-by-side. This will boost up your program.
See my code snippet to check what changes to be made:
#include<iostream>
#define MOD 1000000007
int main() {
unsigned short int T;
unsigned long long N , M , result;
unsigned long long power(unsigned long long, unsigned long long); /*prototype of power*/
std::cin>>T;
for (int i = 0; i < T; i++) {
std::cin >> N >> M;
result = power(M , N);
std::cout << result << std::endl;
}
return 0;
}
unsigned long long power(unsigned long long M, unsigned long long N) {
if(N == 0) {
return 1;
}
unsigned long long result = power(M , N/2);
result = (result * result) % MOD;
if(N%2 == 1) {
result = (result * M) % MOD;
}
return result;
}
How can we store and print factorial(2^n) / (2^n -1))mod1000000009 in C++.Here n can be as large as 20. When I try to print this using the following code, it shows segmentation fault for n=20
#include
#include
using namespace std;
long int factorial(int n)
{
if(n<=1){return 1;}
else
return (n%1000000009)*(factorial(n-1))%1000000009;
}
int main()
{
int K;
long long int numofmatches=0;
long long int denominator=0;
long long int factor=0;
long long int times=0;
long long int players=0;
cin>>K;
if(K==1)
{
cout<<2<<endl<<2<<endl;
return 0;
}
else
{
denominator=pow(2,K);
cout<<"Denominator="<<denominator<<endl;
numofmatches=factorial(denominator)%1000000009;
denominator-=1;
cout<<"numberofmatches="<<numofmatches<<endl;
cout<<"Denominator="<<denominator<<endl;
factor=numofmatches/denominator;
cout<<"Factor="<<factor<<endl;
while(times<=denominator)
{
cout<<(times*factor)<<endl;
++times;
}
}
return 0;
}
First of all, note that (2^n)! / (2^n-1) is equal to (2^n-2)! x 2^n.
Now, (2^20-2)! by itself is already an extremely large number to calculate.
What you can do instead, is to modulo the intermediate result with 1000000009 after every multiplication:
#define MAX ((1<<20)-2)
unsigned long long res = 1;
for (unsigned int i=1; i<=MAX; i++)
res = (res*i)%1000000009;
res = (res*(MAX+2))%1000000009;
If you want to iterate all values of n between 1 and 20, then you can use:
#define MAX_N 20
unsigned int arr[MAX_N+1] = {0};
void Func()
{
unsigned int i = 1;
unsigned long long res = 1;
for (int n=1; n<=MAX_N; n++)
{
unsigned int max = (1<<n)-2;
for (; i<=max; i++)
res = (res*i)%1000000009;
arr[n] = (unsigned int)((res*(max+2))%1000000009);
}
}
BTW, for any n larger than 29 the result will simply be 0, as (2^30-2) is larger than 1000000009.
So (2^30-2)! is divisible by 1000000009, and therefore, (2^30-2)! mod 1000000009 equals 0.
You can find the problem statement here.
My solution can be found here. I am getting correct answers for the test cases but the judge is indicating all answers wrong. What is wrong with my code?
#include <iostream>
#include <vector>
#include <map>
#include <math.h>
using namespace std;
map<long long,long long> arr;
void preProcess();
long long powerOfThree(long long num);
long long powerOfTwo(long long num);
long long theta(long long num);
int main()
{
preProcess();
long long t,k;
cin>>t;
while(t--)
{
cin>>k;
cout<<theta(k)<<endl;
}
}
void preProcess()
{
arr.insert(pair<long long,long long>(0,0));//arr[0]=0;
arr.insert(pair<long long,long long>(1,0));//arr[1]=0;
arr.insert(pair<long long,long long>(2,1));//arr[2]=1;
arr.insert(pair<long long,long long>(3,1));//arr[3]=1;
//arr.insert(pair<long long,long long>(4,2));//arr[4]=2;
//arr.insert(pair<long long,long long>(5,3));//arr[5]=3;
}
long long powerOfTwo(long long num)
{
long long ret=0;
while(num%2==0)
{
ret++;
num = num/2;
}
return ret;
}
long long theta(long long num)
{
long long ret;
map<long long,long long>::iterator it = arr.find(num);
if(it != arr.end())
{
return arr[num];
}
else
{
if(num%2==0)
{
long long a = powerOfTwo(num);
long long q = (num/(long long)pow(2,a));
ret = a + theta(q);
}
else if(num%3==0)
{
long long a = powerOfThree(num);
long long q = (num/(long long)pow(3,a));
ret = a + theta(q);
}
else
{
ret = 1 + theta(num-1);
}
}
arr.insert(pair<long long,long long>(num,ret));//arr[num]=ret;
return ret;
}
long long powerOfThree(long long num)
{
long long ret=0;
while(num%3==0)
{
ret++;
num = num/3;
}
return ret;
}
My approach is to first count all the powers of 2 and 3 in number and then reduce the result by 1. Again repeat the process till it is 1.
your method will fail in a number say for eg (2^8 + 1) *3^2. There would infact be many such numbers.
I would give a solution in Python(bcoz thats what I am comfortable in):
step = [0,0,1,1]
def find(a):
global step
t = len(step)
for n in range(t,a+1):
if ((n%3 == 0) and (n%2 == 0)):
step.append( 1 + min(step[n-1],step[n/2],step[n/3]))
elif (n%2==0):
step.append( 1 + min(step[n-1],step[n/2]))
elif (n%3 == 0) :
step.append( 1 + min(step[n-1],step[n/3]))
else :
step.append( 1 + step[n-1])
return step[a]
n = int(raw_input())
arr = []
for a in range(0,n):
arr.append(raw_input())
for st in arr:
n = int(st)
print find(n)
I wrote the following code to calculate n!modulo p...Given that n and p are close...but its running in a rather funny way, cant figure out the bug..There is some overflow somewhere..The constraints are 1 < P <= 2*10^9
1 <= N <= 2*10^9
though it runs fine for few cases...what could be the error.I have used
(a/b)mod p = ((a mod p)*(b^(p-2))mod p)mod p
as p is prime....and wilsons theorem that (p-1)! mod p = p-1
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
unsigned int pow(unsigned int a, unsigned n,unsigned int p) {
unsigned int ret = 1;
while(n) {
if((n&1)== 1) ret=(ret*a)%p;
a=a%p;
a=(a*a)%p;
n=n>>1;
}
return ret;
}
int main(){_
int t;
cin>>t;
while(t--){
unsigned int n,p;
long long int r;
cin>>n>>p;
r=p-1;
if(n>=p){
cout<<"0\n";
}
else{
for(unsigned int i=p-1;i>n;i--){
r=((long long)r*pow(i,p-2,p))%p;
}
cout<<r<<"\n";
}
}
return 0;
}
21! is 51090942171709440000, while 2^64 is only 1844674407370955161: so if unsigned long long is a 64-bit quantity (as is likely), it doesn't fit.