#include <queue>
#define INF 1 << 30
// prim最小生成树算法
void prim()
{
for(int i=2; i<=V; ++i)
{
int Minner;
int MinCost = INF;
for(int j=2; j<=V; ++j)
{
if(!visited[j] && dis[j] < MinCost)
{
MinCost = dis[j];
MInner = j;
}
}
if(MinCost == INF)
break;
totalCost += MinCost;
visited[Minner] = 1;
// dis[Minner] = 0; //!!!!!!!!!!!!!!!!
for(int j=2; j<=V; ++j)
{
if(!visited[j] && dis[j] > a[Minner][j])
{
dis[j] = a[Minner][j];
}
}
}
}
// 并查集
void unioner(int n1, int n2)
{
if(a[n1] < a[n2])
{
a[n1] += a[n2];
a[n2] = n1;
}
else
{
a[n2] += a[n1];
a[n1] = n2;
}
}
int finder(int n)
{
if(a[n] < 0)
return n;
else
{
return a[n] = finder(a[n]);
}
}
// bfs 求最短路径
struct node{
int x, y;
int step;
node(int xx = 0, int yy = 0) : x(xx), y(yy) {}
}
};
int dir[4][2] = {{1,0}, {-1, 0}, {0,1}, {0, -1}};
vodi bfs()
{
queue q;
while(!q.empty)
{
int tmp = q.front();
if(tmp.x != m || tmp.y != n)
{
for(int i=0; i<4;++i)
{
int sx, sy;
sx = tmp.x + dir[i][0];
sy = tmp.y + die[i][1];
if(!visited[sx][sy] && a[sx][sy] == 1)
{
visited[sx][sy] = 1;
q.push(node(sx,sy));
}
}
}
else
{
cout << tmp.step << '\n';
return 0;
}
q.pop();
}
}
// 二分法
int MaxSub(int a[], int left, int right)
{
int maxLeft, maxRight;
int maxLBorder, maxRBorder;
int LBooder, RBorder;
if(left == right)
{
if(a[left] < 0)
return 0;
return a[left];
}
else
{
int center = left + (right-left)/2;
maxLeft = MaxSub(a, left, center);
maxRight = MaxSub(a, center+1, right);
maxLBorder = LBorder = 0;
maxRBorder = RBooder = 0;
for(int i=center; i>= left; --i)
{
LBorder += a[i];
if(LBorder > maxLBorder)
maxLBorder = LBorder;
}
for(int i=center+1, i<= right; ++i)
{
RBorder += a[i];
if(RBorder > maxRBorder)
maxRBorder = RBorder;
}
return max3(maxLeft, maxRight, maxLBorder+maxRBorder);
}
}
// 递推
for(int i=1; ; ++i)
{
int tmp = i;
int n = N;
while(n--)
{
tmp = getLast(tmp);
if(tmp < 0)
break;
}
if(tmp > 0)
cout << i << ' ' << tmp*N + 1 << '\n';
}
int getLast(int n)
{
double res = (N*n + 1);
if(res % (N-1) != 0)
return -1;
else
return res / (N-1);
}
// 位操作
int getBit(int n, int pos)
{
return n & (1 << pos);
}
void flipBit(int &n, int pos)
{
n ^= (1 << pos);
}
void setBit(int &n, int pos, int key)
{
if(key)
{
n |= (1 << pos);
}
else
n &= ~(1 << pos);
}
//矩阵相乘
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
{
if(i == j)
tmp[i][j] = 1;
}
while(N--)
{
memset(ans, 0, sizeof(a));
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
for(int k=0; k<n; ++k)
{
ans[i][j] += tmp[i][k]*num[k][j];
}
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
{
tmp[i][j] = ans[i][j];
}
}
//分解质因数
int main()
{
int n, m;
cin >> n >> m;
for(int i=n; i<=m; ++i)
{
cout << i<<'=';
int k = i;
int j = 2;
while(j <= sqrt(double(i)))
{
if(k%j == 0)
{
k = k/j;
if(k > 1)
{
cout << j<<'*';
continue;
}
if(k == 1)
cout << j<<endl;
}
++j;
}
if(k == i)
cout << k<<endl;
if(k >1 && k<i)
cout << k << endl;
}
return 0;
}
经过这一个半月,明天考试,自己临时抱佛脚整理的一些算法
最新推荐文章于 2022-02-09 15:45:51 发布