It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am inputting a number n where 1<=n<=10^5. I need a number of length n. So I use pow(10,n-1) but it doesnt work when n=100000. What is the error ?
EDIT: Its codeforces div2 round 152 problem B.
Chilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.
A number's length is the number of digits in its decimal representation without leading zeros.
Input
A single input line contains a single integer n (1 ≤ n ≤ 10^5).
My code works upto n=19. It fails on pretest 9.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int f=0;
unsigned long long n;unsigned long long out;
cin>>n;
unsigned long long num=1;unsigned long long lim=10;
for(unsigned long long z=0;z<n;z++)
{num=num*10;lim=lim*10;}num=num/10;lim=lim/10;
for(;num<lim;num++)
{
if((num%2==0)&&(num%3==0)&&(num%5==0)&&(num%7==0)){f=1;out=num;break;}
}
if(f==1){cout<<out;}
else if(f==0){cout<<"-1";}
return 0;
}
Working with big numbers is not trivial; you cannot just use built-in types like int, double, long, etc for this. In order to calculate a number with 100000 digits, you need to have more than 300000 bits (a few kilobytes); this is in no way easy. Instead, you can print the answer without calculating it!
Saying that a number num is divisible by 2, 3, 5 and 7 is the same as num % 210 == 0. So the answer to your question looks like this:
100000000000... (really many zeros) ...00000xy0
All you need is to find two digits x and y, and print the above "number".
So you have to calculate pow(10, 99999) % 210 without calculating pow(10, 99999). To do it, start with pow(10, 0) = 1 and multiply by 10 successively:
pow(10, 0) % 210 = 1
pow(10, 1) % 210 = (1 * 10) % 210 = 10
pow(10, 2) % 210 = (10 * 10) % 210 = 100
pow(10, 3) % 210 = (100 * 10) % 210 = (1000 % 210) = 160
pow(10, 4) % 210 = (160 * 10) % 210 = (1600 % 210) = 130
pow(10, 5) % 210 = (130 * 10) % 210 = (1300 % 210) = 40
...
After you calculate pow(10, 99999) % 210 in this manner (suppose it's xyz), adding 210 - xyz will make the number divisible by 210. So, to output the answer, print 1, then print 99996 times 0, then print 210 - xyz.
For typical 32 and 64 bit floating point data types (float and double), they are constrained to the range:
float: 3.4E +/- 38 (that is, 3.4 * 10^(+/-38)) (with 7 digits of precision)
double: 1.7E +/- 308 (that is, 1.7 * 10^(+/-308)) (with 15 digits of precision)
A number with 100000 digits is waaaay outside the range of these datatypes. Hence, it fails (in some way), though you haven't told us how it fails.
Related
I am a bit confused how this short function from the C++ {fmt} library works.
inline std::uint32_t digits10_clz(std::uint32_t n) {
std::uint32_t t = (32 - __builtin_clz(n | 1)) * 1233 >> 12;
return t - (n < powers_of_10_u32[t]) + 1;
}
I understand the logic that you can approximate the log10 using log2(__builtin_clz) and that you need to adjust for exact value, but the multiplication is a mystery to me.
Recall the formula for changing the base of logarithm from b to d is
logdx = logbx / logbd
In our case, b is 2 (binary), and d is 10 (decimal). Hence, you need to divide by log210, which is the same as multiplying by 1/log210, i.e by 0.30102999566.
Now recall that shifting by 12 is the same as dividing by 212, which is 4096. Dividing 1233 by 4096 yields 0.30102539062, which is a pretty good approximation for the denominator in the base change formula.
This question already has answers here:
Rounding up to the nearest multiple of a number
(32 answers)
Closed 6 years ago.
I'm trying to design a function int smallestDivisibleAfter(int number, int divisor) such that it returns the smallest number greater than or equal to number that is divisible by divisor (which is non-zero) . Here all inputs and outputs are assumed to be non-negative.
Examples:
smallestDivisibleAfter(9,4); // Returns 12
smallestDivisibleAfter(16,2); // Returns 16
I came up with the code number + divisor - number % divisor. However this ceases to work when number % divisor == 0, since then smallestDivisibleAfter(16,2); // Returns 18 instead of 16.
In addition, number - 1 + divisor - (number - 1)% divisor does not work since int will be replaced by unsigned long long when I put this code into action.
What is the best solution here?
If you want to avoid jumps, try:
number - number % divisor + divisor * !!(number % divisor)
The !!x just converts the number to a boolean with 0 if x==0 and 1 otherwise.
((number / divisor) + 1) * divisor)
This will return the next greatest multiple of divisor after number.
If you ever plan on using floats:
floor(number / divisor).
I am writing this on an Arduino so I don't believe the full scope of C or C++ is available to me, or is it?
I have a sequence of numbers being generated and I want to collect the value of the tens and units columns off of the end;
For example, if the first generated number were 8028, I would want to minus 28 from it and record 28. If the next number were 3479 I would want to take 79 off and store that with 28 (as a running total, so 107).
int number=0;
int remainder=0;
int runningTotal=0;
while (true) {
number = random number between 1000 and 65,535;
remainder = 10's units from number;
number = number - remainder;
runningTotal += remainder;
}
So I'm collecting a running total of anything less than one hundred on the end of the number.
You would want to use a modulus if you can, in c/c++ its the % operator:
8028 % 100
3479 % 100
etc
x % 100 will get you the remainder of dividing x by 100, that is the last two base-10 digits.
8028 / 100 => 80
8028 % 100 => 28
3479 / 100 => 34
3479 % 100 => 79
So division and modulus are all you need.
I have to check, if given number is divisible by 7, which is usualy done just by doing something like n % 7 == 0, but the problem is, that given number can have up to 100000000, which doesn't fit even in long long.
Another constrain is, that I have only few kilobytes of memory available, so I can't use an array.
I'm expecting the number to be on stdin and output to be 1/0.
This is an example
34123461273648125348912534981264376128345812354821354127346821354982135418235489162345891724592183459321864592158
0
It should be possible to do using only about 7 integer variables and cin.get(). It should be also done using only standard libraries.
you can use a known rule about division by 7 that says:
group each 3 digits together starting from the right and start subtracting and adding them alternativly, the divisibility of the result by 7 is the same as the original number:
ex.:
testing 341234612736481253489125349812643761283458123548213541273468213
549821354182354891623458917245921834593218645921580
(580-921+645-218+593-834+921-245+917-458+623-891+354-182
+354-821+549-213+468-273+541-213+548-123+458-283+761-643
+812-349+125-489+253-481+736-612+234-341
= 1882 )
% 7 != 0 --> NOK!
there are other alternatives to this rule, all easy to implement.
Think about how you do division on paper. You look at the first digit or two, and write down the nearest multiple of seven, carry down the remainder, and so on. You can do that on any abritrary length number because you don't have to load the whole number into memory.
Most of the divisibility by seven rules work on a digit level, so you should have no problem applying them on your string.
You can compute the value of the number modulo 7.
That is, for each digit d and value n so far compute n = (10 * n + d) % 7.
This has the advantage of working independently of the divisor 7 or the base 10.
You can compute the value of the number modulo 7.
That is, for each digit d and value n so far compute n = (10 * n + d) % 7.
This has the advantage of working independently of the divisor 7 or the base 10.
I solved this problem exactly the same way on one of programming contests. Here is the fragment of code you need:
int sum = 0;
while (true) {
char ch;
cin>>ch;
if (ch<'0' || ch>'9') break; // Reached the end of stdin
sum = sum*10; // The previous sum we had must be multiplied
sum += (int) ch;
sum -= (int) '0'; // Remove the code to get the value of the digit
sum %= 7;
}
if (sum==0) cout<<"1";
else cout<<"0";
This code is working thanks to simple rules of modular arithmetics. It also works not just for 7, but for any divisor actually.
I'd start by subtracting some big number which is divisible by 7.
Examples of numbers which are divisible by 7 include 700, 7000, 70000, 140000000, 42000000000, etc.
In the particular example you gave, try subtracting 280000000000(some number of zeros)0000.
Even easier to implement, repeatedly subtract the largest possible number like 70000000000(some number of zeros)0000.
Because I recently did work dealing with breaking up numbers, I will hint that to get specific numbers - which is what you will need with some of the other answers - think about integer division and using the modulus to get digits out of it.
If you had a smaller number, say 123, how would you get the 1, the 2, and the 3 out of it? Especially since you're working in base 10...
N = abc
There is a simple algorithm to verify if a three-digit number is a multiple of 7:
Substitute a by x and add it to bc, being x the tens of a two-digit number multiple of 7 whose hundreds is a.
N = 154; x = 2; 2 + 54 = 56; 7|56 and 7|154
N = 931; x = 4; 4 + 31 = 35; 7|35 and 7|931
N = 665; x = 5; 5 + 65 = 70; 7|70 and 7|665
N = 341; x = 6; 6 + 41 = 47; 7ł47 and 7ł341
If N is formed by various periods the inverse additive of the result of one period must be added to the sum of the next period, this way:
N = 341.234
6 + 41 = 47; - 41 mod 7 ≡ 1; 1 + 4 + 34 = 39; 7ł39 and 7łN
N = 341.234.612.736.481
The result for 341.234 is 39. Continuing from this result we have:
-39 mod 7 ≡ 3; 3 + 5 + 6 + 1 + 2 + 1 = 18; - 18 mod 7 ≡ 3; 3 + 0 + 36 = 39; - 39 mod 7 ≡ 3;
3 + 1 + 81 = 85; 7ł85 and 7łN
This rule may be applied entirely through mental calculation and is very quick.
It was derived from another rule that I created in 2.005. It works for numbers of any magnitude and for divisibility by 13.
At first Take That Big Number in string And then sum every digit of string. at last check if(sum%7==0)
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int n,i,j,sum,k;
sum=0;
string s;
cin>>s;
for(i=0;i<s.length();i++)
{
sum=sum+(s[i]-'0');
}
if(sum%7==0)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
return 0;
}
I need some division algorithm which can handle big integers (128-bit).
I've already asked how to do it via bit shifting operators. However, my current implementation seems to ask for a better approach
Basically, I store numbers as two long long unsigned int's in the format
A * 2 ^ 64 + B with B < 2 ^ 64.
This number is divisible by 24 and I want to divide it by 24.
My current approach is to transform it like
A * 2 ^ 64 + B A B
-------------- = ---- * 2^64 + ----
24 24 24
A A mod 24 B B mod 24
= floor( ---- ) * 2^64 + ---------- * 2^64 + floor( ---- ) + ----------
24 24.0 24 24.0
However, this is buggy.
(Note that floor is A / 24 and that mod is A % 24. The normal divisions are stored in long double, the integers are stored in long long unsigned int.
Since 24 is equal to 11000 in binary, the second summand shouldn't change something in the range of the fourth summand since it is shifted 64 bits to the left.
So, if A * 2 ^ 64 + B is divisible by 24, and B is not, it shows easily that it bugs since it returns some non-integral number.
What is the error in my implementation?
The easiest way I can think of to do this is to treat the 128-bit numbers as four 32-bit numbers:
A_B_C_D = A*2^96 + B*2^64 + C*2^32 + D
And then do long division by 24:
E = A/24 (with remainder Q)
F = Q_B/24 (with remainder R)
G = R_C/24 (with remainder S)
H = S_D/24 (with remainder T)
Where X_Y means X*2^32 + Y.
Then the answer is E_F_G_H with remainder T. At any point you only need division of 64-bit numbers, so this should be doable with integer operations only.
Could this possibly be solved with inverse multiplication? The first thing to note is that 24 == 8 * 3 so the result of
a / 24 == (a >> 3) / 3
Let x = (a >> 3) then the result of the division is 8 * (x / 3). Now it remains to find the value of x / 3.
Modular arithmetic states that there exists a number n such that n * 3 == 1 (mod 2^128). This gives:
x / 3 = (x * n) / (n * 3) = x * n
It remains to find the constant n. There's an explanation on how to do this on wikipedia. You'll also have to implement functionality to multiply to 128 bit numbers.
Hope this helps.
/A.B.
You shouldn't be using long double for your "normal divisions" but integers there as well. long double doesn't have enough significant figures to get the answer right (and anyway the whole point is to do this with integer operations, correct?).
Since 24 is equal to 11000 in binary, the second summand shouldn't change something in the range of the fourth summand since it is shifted 64 bits to the left.
Your formula is written in real numbers. (A mod 24) / 24 can have an arbitrary number of decimals (1/24 is for instance 0.041666666...) and can therefore interfere with the fourth term in your decomposition, even once multiplied by 2^64.
The property that Y*2^64 does not interfere with the lower weight binary digits in an addition only works when Y is an integer.
Don't.
Go grab a library to do this stuff - you'll be incredibly thankful you chose to when debugging weird errors.
Snippets.org had a C/C++ BigInt library on it's site a while ago, Google also turned up the following: http://mattmccutchen.net/bigint/