文章目录
高斯消元 - 解方程 [暂且不管-需进一步优化]
#include <iostream>
using namespace std;
#include<cmath>
#include<algorithm>
const int N = 110;
int n;
double a[N][N];
const double eps = 1e-6 ;
int gauss()
{
int c,r;
for(c = 0,r = 0;c < n;c ++)
{
int t = r;
for(int i = r;i < n;i ++)
if(fabs(a[i][c]) > fabs(a[t][c]))
t = i;
if(fabs(a[t][c]) < eps) continue;
for(int i = c;i <= n;i++)swap(a[t][i],a[r][i]);
for(int i = n;i >= c;i--)a[r][i] /= a[r][c];
for(int i = r + 1;i < n;i++)
if(fabs(a[i][c]) > eps )
for(int j = n;i >= c;j--)
a[i][j] -= a[r][j] * a[i][c];
r ++ ;
}
if(r < n)
{
for(int i = r;i < n;i ++)
if(fabs(a[i][n]) > eps)
return 2;
return 1;
}
for(int i = n - 1;i >= 0;i --)
for(int j = i + 1;j < n;j++)
a[i][n] -= a[i][j] * a[i][n];
return 0;
}
int test_01()
{
cin >> n;
for(int i = 0;i < n;i++)
for(int j = 0;j < n + 1;j ++)
cin >> a[i][j];
int t = gauss();
if(t == 0)
{
for(int i = 0;i < n;i ++) printf("%.2lf",a[i][n]);
}
else if(t == 1) puts("Infinte group solutions");
else puts("No solution");
return 0;
}
885. 求组合数 I C(m,n)
const int N = 2010 , mod = 1e9 + 7;
int c[N][N];
void init()
{
for(int i = 0;i < N;i ++)
for(int j = 0;j <= i;j ++)
if(!j) c[i][j] = 1;
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1] % mod);
}
int test_02()
{
init();
int n;
scanf("%d",&n);
while(n --)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",c[a][b]);
}
return 0;
}
886 求组合数 II 【数据大小10万级别】 【费马小定理+快速幂+逆元】
typedef long long LL;
const int N = 100010,mod = 1e9 + 7;
int fact[N],infact[N];
int qmi(int a,int k,int p)
{
int res = 1;
while(k)
{
if(k % 2) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int test_03()
{
fact[0] = infact[0] = 1;
for(int i = 1;i < N;i++)
{
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i,mod - 2,mod) % mod;
}
int n;
scanf("%d",&n);
while(n --)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",(LL)fact[a] * infact[b] % mod * infact[a - b] % mod);
}
return 0;
}
887. 求组合数 III 【le18级别】 【卢卡斯定理 + 逆元 + 快速幂 】
#include<algorithm>
typedef long long LL;
int p;
int qmi(int a,int k)
{
int res = 1;
while(k)
{
if(k % 2) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int C(int a,int b)
{
int res = 1;
for(int i = 1,j = a ;i <= b;i++,j--)
{
res = (LL)res * j % p;
res = (LL)res * qmi(i,p-2) % p;
}
return res;
}
int lucas(LL a,LL b)
{
if(a < p && b <p) return C(a,b);
return (LL)C(a % p,b % p) * lucas(a / p, b / p) % p;
}
int test_04()
{
int n;
cin >> n;
while(n --)
{
LL a, b;
cin >> a >> b >> p ;
cout << lucas(a,b) << endl;
}
}
888.求组合数 IV 【没有%p – 高精度算出准确结果】 【分解质因数 + 高精度乘法 --只用一次高精度提高运行效率】
#include<vector>
const int N = 5010;
int primes[N],cnt;
bool st[N];
int sum[N];
void get_primes(int n)
{
for(int i = 2;i <= n;i++)
{
if(!st[i]) primes[cnt ++] = i;
for(int j = 0 ;primes[j] <= n / i;j++)
{
st[primes[j] * i] = true;
if(i % primes[j] == 0) break;
}
}
}
int get(int n,int p)
{
int res = 0;
while(n)
{
res += n / p;
n /= p;
}
return res;
}
vector<int> mul(vector<int> a,int b)
{
vector<int> c;
int t = 0;
for(int i = 0;i < a.size();i++)
{
t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
while(t)
{
c.push_back( t % 10);
t /= 10;
}
return c;
}
int test_05()
{
int a,b;
cin >> a >> b;
get_primes(a);
for(int i = 0;i < cnt;i++)
{
int p = primes[i];
sum[i] = get(a,p) - get(b,p) - get(a - b,p);
}
vector<int> res;
res.push_back(1);
for(int i = 0;i < cnt;i++)
for(int j = 0;j < sum[i];j++)
res = mul(res,primes[i]);
for(int i = res.size() - 1;i >= 0;i --) printf("%d", res[i]);
puts("");
return 0;
}
889 满足条件的01序列 【卡特兰数-用法极多!】
typedef long long LL;
const int mod = 1e9 + 7;
int qmi(int a,int k,int p)
{
int res = 1;
while(k)
{
if(k % 2) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int test_06()
{
int n;
cin >> n;
int a = 2 * n , b = n;
int res = 1;
for(int i = a;i > a - b;i --) res = (LL)res * i % mod;
for(int i = 1;i <= b;i ++) res = (LL)res * qmi(i,mod - 2,mod) % mod;
res = (LL)res * qmi(n + 1,mod - 2,mod) % mod;
cout << res << endl;
return 0;
}
int main() {
test_06();
return 0;
}