mod

For a given value of n and m, find fib(n) mod m where n is very huge. (Pisano Period)

故事扮演 提交于 2021-02-04 16:43:35
问题 Input Integers 'n' (up to 10^14) and 'm'(up to 10^3) Output Fib(n) modulo m Sample Cases Input: 239 1000 Output: 161 Input: 2816213588 239 Output: 151 Hint given in Question As it is not possible to iterate 'n' times (because n is huge), consider using Pisano Period(repetition of remainders when every element Fibonacci series is divided by any integer) Code which I wrote (maybe wrong, but passes above-mentioned cases) n, m = map(int, input().split()) a, b = 0, 1 fib_rems = [0, 1] # remainders

Why does the C++ modulo operator return 0 for -1 % str.size()?

给你一囗甜甜゛ 提交于 2021-01-27 13:41:36
问题 I'm confused why the following code produces this output: #include <iostream> #include <string> using namespace std; int main() { int i = -1; string s = "abc"; int j = s.size(); int x = 1 % 3; int y = i % j; int z = i % s.size(); cout << s.size() << endl; // 3 cout << x << endl; // 1 cout << y << endl; // -1 cout << z << endl; // 0 } Why is z = 0? Does it have to do with casting? 回答1: So, stripping down your code to a minimal example, you're asking why this prints 0 : #include <iostream>

Fast modulo 10 in c

為{幸葍}努か 提交于 2021-01-06 14:00:03
问题 I am looking for a fast modulo 10 algorithm because I need to speed up my program which does many modulo operations in cycles. I have checked out this page which compares some alternatives. As far as I understand it correctly, T3 was the fastest of all. My question is, how would x % y look like using T3 technique? I copied T3 technique here for simplicity in case the link gets down. for (int x = 0; x < max; x++) { if (y > (threshold - 1)) { y = 0; //reset total += x; } y += 1; } Regarding to

Fast modulo 10 in c

痴心易碎 提交于 2021-01-06 13:59:33
问题 I am looking for a fast modulo 10 algorithm because I need to speed up my program which does many modulo operations in cycles. I have checked out this page which compares some alternatives. As far as I understand it correctly, T3 was the fastest of all. My question is, how would x % y look like using T3 technique? I copied T3 technique here for simplicity in case the link gets down. for (int x = 0; x < max; x++) { if (y > (threshold - 1)) { y = 0; //reset total += x; } y += 1; } Regarding to

Fast modulo 10 in c

旧街凉风 提交于 2021-01-06 13:59:28
问题 I am looking for a fast modulo 10 algorithm because I need to speed up my program which does many modulo operations in cycles. I have checked out this page which compares some alternatives. As far as I understand it correctly, T3 was the fastest of all. My question is, how would x % y look like using T3 technique? I copied T3 technique here for simplicity in case the link gets down. for (int x = 0; x < max; x++) { if (y > (threshold - 1)) { y = 0; //reset total += x; } y += 1; } Regarding to

Fast modulo 10 in c

孤街醉人 提交于 2021-01-06 13:59:07
问题 I am looking for a fast modulo 10 algorithm because I need to speed up my program which does many modulo operations in cycles. I have checked out this page which compares some alternatives. As far as I understand it correctly, T3 was the fastest of all. My question is, how would x % y look like using T3 technique? I copied T3 technique here for simplicity in case the link gets down. for (int x = 0; x < max; x++) { if (y > (threshold - 1)) { y = 0; //reset total += x; } y += 1; } Regarding to

How to get only odd numbers in array and square them using numpy for python?

和自甴很熟 提交于 2020-12-04 11:45:42
问题 I've done this without NumPy in Python: def fun_list(list_, x): #set list to odd numbers in list_ raised to number x s=[] s= [i**x for i in list_ if (i%2)!=0] list_=s print(list_) Testing the function: list1 = [1, 2, 3, 4] list2 = [2, 3, 4, 5] print(fun_list(list1, 2)) print(fun_list(list2, 3)) Results: [1, 9] None [27, 125] None NOW need to do it using NumPy, which I don't understand and can't find much about it online and what I find doesn't make sense to me. This is what I've tried: import

Solving modular linear congruences for large numbers

徘徊边缘 提交于 2020-08-20 09:11:43
问题 I'm looking for a better algorithm than one I found on stackoverflow to handle 4096 byte numbers, i'm hitting a maximum recursion depth. Code from stackoverlow post, i copy/pasted it but lost the original link: def linear_congruence(a, b, m): if b == 0: return 0 if a < 0: a = -a b = -b b %= m while a > m: a -= m return (m * linear_congruence(m, -b, a) + b) // a This works fine for smaller numbers, for example: In [167]: pow_mod(8261, 63, 4033) 63 1 8261 4033 31 195 1728 4033 15 2221 1564 4033

signed int modulo unsigned int produces nonsense results

喜你入骨 提交于 2020-06-15 20:31:15
问题 I need to perform a real mathematical modulo in C. It makes sense for me to allow negative numbers for the moduled argument, since my modular calculations can produce negative intermediate results, which must be put back into the least residue system. But it makes no sense to allow negative module, therefore i wrote unsigned int mod( int x, unsigned int m ) { int r = x % m; return r >= 0 ? r : r + m; } However calling such function with negative number and positive module printf("%u\n", mod(