GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — code-library/Math/Lagrange Interpolation Brute.cpp at master · SuckSolace/code-library

Expand file tree

/

Lagrange Interpolation Brute.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
81 lines (76 loc) · 3.51 KB
/

Lagrange Interpolation Brute.cpp

Copy path

File metadata and controls

81 lines (76 loc) · 3.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9, mod = 1e9 + 7;
template <int32_t MOD>
struct modint {
int32_t value;
modint() = default;
modint(int32_t value_) : value(value_) {}
inline modint<MOD> operator + (modint<MOD> other) const { int32_t c = this->value + other.value; return modint<MOD>(c >= MOD ? c - MOD : c); }
inline modint<MOD> operator - (modint<MOD> other) const { int32_t c = this->value - other.value; return modint<MOD>(c < 0 ? c + MOD : c); }
inline modint<MOD> operator * (modint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return modint<MOD>(c < 0 ? c + MOD : c); }
inline modint<MOD> & operator += (modint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
inline modint<MOD> & operator -= (modint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; }
inline modint<MOD> & operator *= (modint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
inline modint<MOD> operator - () const { return modint<MOD>(this->value ? MOD - this->value : 0); }
modint<MOD> pow(uint64_t k) const {
modint<MOD> x = *this, y = 1;
for (; k; k >>= 1) {
if (k & 1) y *= x;
x *= x;
}
return y;
}
modint<MOD> inv() const { return pow(MOD - 2); } // MOD must be a prime
inline modint<MOD> operator / (modint<MOD> other) const { return *this * other.inv(); }
inline modint<MOD> operator /= (modint<MOD> other) { return *this *= other.inv(); }
inline bool operator == (modint<MOD> other) const { return value == other.value; }
inline bool operator != (modint<MOD> other) const { return value != other.value; }
inline bool operator < (modint<MOD> other) const { return value < other.value; }
inline bool operator > (modint<MOD> other) const { return value > other.value; }
};
template <int32_t MOD> modint<MOD> operator * (int64_t value, modint<MOD> n) { return modint<MOD>(value) * n; }
template <int32_t MOD> modint<MOD> operator * (int32_t value, modint<MOD> n) { return modint<MOD>(value % MOD) * n; }
template <int32_t MOD> ostream & operator << (ostream & out, modint<MOD> n) { return out << n.value; }
using mint = modint<mod>;
//generates the polynomial explicitely
vector<mint> Lagrange(vector<mint> &x, vector<mint> &y) {
int n = x.size();
vector<mint> ans(n, 0);
vector<mint> all(n + 1, 0); //(x - x0) * (x - x1) * ... * (x - x(n-1))
all[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = n; j >= 0; j--) {
all[j] *= -x[i];
if (j) all[j] += all[j - 1];
}
}
for (int i = 0; i < n; i++) {
vector <mint> up(n); //all / (x - xi)
mint rem = all[n];
for (int j = n - 1; j >= 0; --j) {
up[j] = rem;
rem = all[j] + rem * x[i];
}
mint down = 1;
for (int j = 0; j < n; j++) if (i != j) down *= x[i] - x[j];
up.resize(n); down = down.inv() * y[i];
for (int j = 0; j < n; j++) ans[j] += up[j] * down;
}
return ans;
}
int32_t main() {
int n, k; cin >> n >> k;
vector<mint> y, x;
mint sum = 0; y.push_back(0); x.push_back(0);
for (int i = 1; i <= k + 1; i++) {
sum += mint(i).pow(k);
y.push_back(sum); x.push_back(i);
}
auto p = Lagrange(x, y);
mint ans = 0;
for (int i = 0; i < p.size(); i++) ans += p[i] * mint(n).pow(i);
cout << ans << '\n';
cout << 1.0 * clock() / CLOCKS_PER_SEC << '\n';
return 0;
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.