Skip to content

Commit 1f613fd

Browse files
committed
linear recurrence, matrix exponentiation
1 parent df361c8 commit 1f613fd

File tree

5 files changed

+302
-0
lines changed

5 files changed

+302
-0
lines changed

10551-d.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.math.BigInteger;
4+
5+
//Given 2 strings p and m in base b return p % m in base b
6+
7+
class Main {
8+
public static void main(String args[]){
9+
int b;
10+
BigInteger p, m;
11+
String num, div, ans;
12+
Scanner s = new Scanner(System.in);
13+
14+
while(true){
15+
b = s.nextInt();
16+
if (b == 0) break;
17+
18+
p = new BigInteger(s.next(), b); //converted to base 10
19+
m = new BigInteger(s.next(), b);
20+
ans = p.mod(m).toString(b);
21+
System.out.println(ans);
22+
}
23+
24+
}
25+
26+
static class FastReader {
27+
// static class FastReader extends Scanner{
28+
// INFO : final classes cant be extended
29+
30+
BufferedReader br;
31+
StringTokenizer st;
32+
33+
public FastReader() {
34+
br = new BufferedReader(new
35+
InputStreamReader(System.in));
36+
}
37+
38+
String next() {
39+
while (st == null || !st.hasMoreElements()) {
40+
try {
41+
st = new StringTokenizer(br.readLine());
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
return st.nextToken();
47+
}
48+
49+
int nextInt() {
50+
return Integer.parseInt(next());
51+
}
52+
53+
long nextLong() {
54+
return Long.parseLong(next());
55+
}
56+
57+
double nextDouble() {
58+
return Double.parseDouble(next());
59+
}
60+
61+
String nextLine() {
62+
String str = "";
63+
try {
64+
str = br.readLine();
65+
} catch (IOException e) {
66+
e.printStackTrace();
67+
}
68+
return str;
69+
}
70+
}
71+
}

10925-d.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.math.*; //for BigInteger
4+
class Main {
5+
// public static void main(){
6+
public static void main(String args[]){
7+
Scanner s = new Scanner(System.in);
8+
int c = 0;
9+
BigInteger sum = BigInteger.ZERO;
10+
11+
while(true){
12+
++c;
13+
int n = s.nextInt();
14+
int f = s.nextInt();
15+
if (n == 0 && f == 0) break;
16+
sum = BigInteger.ZERO;
17+
while(n-- > 0){
18+
sum = sum.add(s.nextBigInteger());
19+
}
20+
// Bill #1 costs 16200000000: each friend should pay 540000000
21+
System.out.printf("Bill #%d costs %s: each friend should pay %s\n\n",
22+
c, sum.toString(), sum.divide(BigInteger.valueOf(f)));
23+
}
24+
}
25+
26+
static class FastReader {
27+
BufferedReader br;
28+
StringTokenizer st;
29+
30+
public FastReader() {
31+
br = new BufferedReader(new
32+
InputStreamReader(System.in));
33+
}
34+
35+
String next() {
36+
while (st == null || !st.hasMoreElements()) {
37+
try {
38+
st = new StringTokenizer(br.readLine());
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
return st.nextToken();
44+
}
45+
46+
int nextInt() {
47+
return Integer.parseInt(next());
48+
}
49+
50+
long nextLong() {
51+
return Long.parseLong(next());
52+
}
53+
54+
double nextDouble() {
55+
return Double.parseDouble(next());
56+
}
57+
58+
String nextLine() {
59+
String str = "";
60+
try {
61+
str = br.readLine();
62+
} catch (IOException e) {
63+
e.printStackTrace();
64+
}
65+
return str;
66+
}
67+
}
68+
}

FIBOSUM-d.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
typedef vector<ll> vi;
5+
typedef vector<vi> vvi;
6+
const int mod = 1000000007;
7+
8+
9+
vvi mul(const vvi& a, const vvi& b){
10+
vvi c(a.size(), vi(b[0].size(), 0));
11+
12+
for(int r = 0; r < a.size(); ++r){
13+
for(int j = 0; j < b[0].size(); ++j){
14+
for(int x = 0; x < b.size(); ++x){
15+
c[r][j] += (a[r][x] % mod) * (b[x][j] % mod);
16+
c[r][j] %= mod;
17+
}
18+
}
19+
}
20+
return c;
21+
}
22+
23+
vvi pow(const vvi& a, int n){
24+
if (n <= 1) return a;
25+
if (n & 1) return mul(a, pow(a, n-1));
26+
27+
vvi ans = pow(a, n/2);
28+
ans = mul(ans, ans);
29+
return ans;
30+
}
31+
32+
ll sum(int n){
33+
vvi t = {
34+
{1, 1, 0}, //the last 0 will be modified accordingly
35+
{0, 0, 1},
36+
{0, 1, 1}
37+
};
38+
39+
vvi tn = pow(t, n);
40+
41+
// for(int i = 0; i < tn.size(); ++i){
42+
// for(int j = 0; j < tn[0].size(); ++j){
43+
// cout << tn[i][j] << " ";
44+
// }
45+
// cout << endl;
46+
// }
47+
48+
ll ans = tn[0][2] % mod;
49+
return ans;
50+
}
51+
52+
int main(){
53+
int t;
54+
cin >> t;
55+
while(t--){
56+
ll n, m;
57+
cin >> n >> m;
58+
59+
m = sum(m+1);
60+
n = sum(n);
61+
cout << (m - n + mod) % mod << "\n";
62+
}
63+
return 0;
64+
65+
}

JULKA-d.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.math.BigInteger;
4+
5+
class Main {
6+
// public static void main(){
7+
public static void main(String args[]){
8+
Scanner s = new Scanner(System.in);
9+
for(int i = 0; i < 10; ++i){
10+
BigInteger sum = s.nextBigInteger();
11+
BigInteger excess = s.nextBigInteger();
12+
BigInteger h = sum.subtract(excess).divide(BigInteger.valueOf(2));
13+
System.out.println(h.add(excess));
14+
System.out.println(h);
15+
}
16+
}
17+
18+
static class FastReader {
19+
BufferedReader br;
20+
StringTokenizer st;
21+
22+
public FastReader() {
23+
br = new BufferedReader(new
24+
InputStreamReader(System.in));
25+
}
26+
27+
String next() {
28+
while (st == null || !st.hasMoreElements()) {
29+
try {
30+
st = new StringTokenizer(br.readLine());
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
return st.nextToken();
36+
}
37+
38+
int nextInt() {
39+
return Integer.parseInt(next());
40+
}
41+
42+
long nextLong() {
43+
return Long.parseLong(next());
44+
}
45+
46+
double nextDouble() {
47+
return Double.parseDouble(next());
48+
}
49+
50+
String nextLine() {
51+
String str = "";
52+
try {
53+
str = br.readLine();
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
return str;
58+
}
59+
}
60+
}

largeFactorial.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef int digit;
5+
6+
void mul(vector<digit>& v, int n){
7+
int carry = 0;
8+
int prod = 0;
9+
10+
for(int i = 0; i < v.size(); ++i){
11+
prod = v[i] * n + carry;
12+
v[i] = prod % 10;
13+
carry = prod / 10;
14+
}
15+
16+
while(carry){
17+
v.push_back(carry % 10);
18+
carry /= 10;
19+
}
20+
}
21+
22+
void factorial(int n){
23+
vector<digit> v(1,1);
24+
25+
for(int i = 2; i <= n; ++i){
26+
mul(v, i);
27+
}
28+
29+
for(int i = v.size() - 1; i >=0; --i){
30+
cout << v[i];
31+
}
32+
}
33+
34+
int main(){
35+
int n;
36+
cin >> n;
37+
factorial(n);
38+
}

0 commit comments

Comments
 (0)