classSolution {
public:staticint reduce_mod60_str(const std::string &s) {
int r =0; for (char ch : s) r = (r*10+ (ch -'0')) %60; return r;
}
staticintfib_mod10_by_idx(int k) {
int a =0, b =1;
for (int i =0; i < k; ++i) { int nxt = (a + b) %10; a = b; b = nxt; }
return a;
}
intpartial_sum_last_digit_str(const std::string &ms, const std::string &ns) {
int m = reduce_mod60_str(ms); int n = reduce_mod60_str(ns);
int a = (n +2) %60; int b = (m +1) %60;
int fa = fib_mod10_by_idx(a); int fb = fib_mod10_by_idx(b);
return (fa - fb +10) %10;
}
};
classSolution {
privatestaticintreduceMod60Str(String s) {
int r = 0; for (char ch : s.toCharArray()) r = (r*10 + (ch -'0')) % 60; return r;
}
privatestaticintfibMod10ByIdx(int k) {
int a = 0, b = 1; for (int i=0;i<k;++i) { int nxt=(a+b)%10; a=b; b=nxt; } return a;
}
publicintpartialSumLastDigitStr(String ms, String ns) {
int m = reduceMod60Str(ms), n = reduceMod60Str(ns);
int a = (n + 2) % 60, b = (m + 1) % 60;
int fa = fibMod10ByIdx(a), fb = fibMod10ByIdx(b);
return (fa - fb + 10) % 10;
}
}
1
2
3
4
5
6
7
8
9
objectSolution {
privatefunreduceMod60Str(s: String): Int { var r=0; for (ch in s) r=(r*10 + (ch - '0')) % 60; return r }
privatefunfibMod10ByIdx(k: Int): Int { var a=0; var b=1; repeat(k) { val nxt=(a+b)%10; a=b; b=nxt }; return a }
funpartialSumLastDigitStr(ms: String, ns: String): Int {
val m = reduceMod60Str(ms); val n = reduceMod60Str(ns)
val a = (n + 2) % 60; val b = (m + 1) % 60return (fibMod10ByIdx(a) - fibMod10ByIdx(b) + 10) % 10 }
}
classSolution:
@staticmethoddefreduce_mod60_str(s: str) -> int:
r =0for ch in s:
r = (r*10+ (ord(ch) -48)) %60return r
@staticmethoddeffib_mod10_by_idx(k: int) -> int:
a, b =0, 1for _ in range(k):
a, b = b, (a + b) %10return a
defpartial_sum_last_digit_str(self, ms: str, ns: str) -> int:
m = self.reduce_mod60_str(ms); n = self.reduce_mod60_str(ns)
a = (n +2) %60; b = (m +1) %60return (self.fib_mod10_by_idx(a) - self.fib_mod10_by_idx(b) +10) %10defpartial_sum_last_digit(self, m: int, n: int) -> int:
return self.partial_sum_last_digit_str(str(m), str(n))
1
2
3
4
5
6
7
8
9
10
pubstructSolution;
impl Solution {
fnreduce_mod60_str(s: &str) -> usize { s.bytes().fold(0usize, |r,ch| (r*10+ (ch -b'0') asusize) %60) }
fnfib_mod10_by_idx(k: usize) -> u8 { let (mut a, mut b) = (0u8, 1u8); for _ in0..k { let nxt = (a + b) %10; a = b; b = nxt; } a }
pubfnpartial_sum_last_digit_str(ms: &str, ns: &str) -> u8 {
let m = Self::reduce_mod60_str(ms); let n = Self::reduce_mod60_str(ns);
let a = (n +2) %60; let b = (m +1) %60;
((Self::fib_mod10_by_idx(a) +10) - Self::fib_mod10_by_idx(b)) %10 }
}
⏰ Time complexity: O(L + P) where L is the length (in digits) of m and n when given as strings and P = 60 (Pisano period for mod 10). Reduction takes O(L) and the iterative Fibonacci steps take at most O(60).
After reducing indices to k1=(n+2)%60 and k2=(m+1)%60, fast-doubling computes F(k) mod 10 in O(log k) steps. For mod 10 the k are ≤ 60 so this is optional.
deffib_pair(n: int, mod: int):
if n ==0: return (0,1)
a,b = fib_pair(n>>1, mod)
c = (a * ((2*b - a) % mod)) % mod
d = ((a*a) % mod + (b*b) % mod) % mod
if n &1:
return (d, (c+d) % mod)
else:
return (c, d)
# reduce indices then return (fib_pair(k1,10)[0] - fib_pair(k2,10)[0] + 10) % 10