#include <bits/stdc++.h>
using namespace std;
const long long N=20;
long long a[N];
long long n,m;
long long p;
long long res=0;
void dfs(long long len,long long cur,long long mul,long long limit){
if(mul>n)return;
if(len==limit){
res+=p*(n/mul);
return;
}
if(cur>m)return;
dfs(len+1,cur+1,mul*a[cur],limit);
dfs(len,cur+1,mul,limit);
}
signed main(){
scanf("%lld%lld",&n,&m);
p=-1;
for(long long i=1;i<=m;i++)scanf("%lld",&a);
for(long long i=1;i<=m;i++){
p=(-p);
dfs(0,1,1,i);
}
cout<<res;
}
高斯消元
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 110;
const double eps = 1e-6;
int n;
double a[N][N];
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[c]) > fabs(a[t][c]))
t = i;
if (fabs(a[t][c]) < eps) continue;
for (int i = c; i < n + 1; i ++ ) swap(a[t], a[r]);
for (int i = n; i >= c; i -- ) a[r] /= a[r][c];
for (int i = r + 1; i < n; i ++ )
if (fabs(a[c]) > eps)
for (int j = n; j >= c; j -- )
a[j] -= a[r][j] * a[c];
r ++ ;
}
if (r < n)
{ for (int i = r; i < n; i ++ )//
if (fabs(a[n]) > eps)
return 2;
return 1;
}
for (int i = n - 1; i >= 0; i -- )
for (int j = i + 1; j < n; j ++ )
a[n] -= a[j][n] * a[j];
return 0;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n + 1; j ++ )
cin >> a[j];
int t = gauss();
if (t == 0)
{
for (int i = 0; i < n; i ++ ) printf("%.2lf\n", a[n]);
}
else puts("No Solution");
return 0;
}
高斯消元解异或线性方程组
#include <bits/stdc++.h>
using namespace std;
const long long N=20;
long long a[N];
long long n,m;
long long p;
long long res=0;
void dfs(long long len,long long cur,long long mul,long long limit){
if(mul>n)return;
if(len==limit){
res+=p*(n/mul);
return;
}
if(cur>m)return;
dfs(len+1,cur+1,mul*a[cur],limit);
dfs(len,cur+1,mul,limit);
}
signed main(){
scanf("%lld%lld",&n,&m);
p=-1;
for(long long i=1;i<=m;i++)scanf("%lld",&a);
for(long long i=1;i<=m;i++){
p=(-p);
dfs(0,1,1,i);
}
cout<<res;
}
高斯消元
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 110;
const double eps = 1e-6;
int n;
double a[N][N];
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[c]) > fabs(a[t][c]))
t = i;
if (fabs(a[t][c]) < eps) continue;
for (int i = c; i < n + 1; i ++ ) swap(a[t], a[r]);
for (int i = n; i >= c; i -- ) a[r] /= a[r][c];
for (int i = r + 1; i < n; i ++ )
if (fabs(a[c]) > eps)
for (int j = n; j >= c; j -- )
a[j] -= a[r][j] * a[c];
r ++ ;
}
if (r < n)
{ for (int i = r; i < n; i ++ )//
if (fabs(a[n]) > eps)
return 2;
return 1;
}
for (int i = n - 1; i >= 0; i -- )
for (int j = i + 1; j < n; j ++ )
a[n] -= a[j][n] * a[j];
return 0;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n + 1; j ++ )
cin >> a[j];
int t = gauss();
if (t == 0)
{
for (int i = 0; i < n; i ++ ) printf("%.2lf\n", a[n]);
}
else puts("No Solution");
return 0;
}
高斯消元解异或线性方程组