1 条题解

  • 0
    @ 2025-6-10 21:12:57

    题意分析

    题目要求计算两个整系数多项式的最大公因式(GCM)。输入为两个多项式的表达式,需按指定格式输出它们的GCM。多项式表达式支持变量xx、整数、指数、乘法、加法和减法。输出格式要求按降幂排列,系数互质,且遵循特定的省略规则(如系数为1或指数为0/1时的处理)。

    解题思路

    1)解析多项式表达式,构建系数数组;2)使用多项式欧几里得算法(辗转相除法)计算GCM;3)标准化结果,确保系数互质并按要求格式输出。需注意:1)处理复杂表达式(如嵌套括号、指数);2)处理负系数和零系数;3)格式化输出时遵循特殊规则。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 10;
    const int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
    
    struct p {
    	int a, b;
    	p(int A, int B) :a(A), b(B) {}
    	p() {}
    };
    
    
    int maze[maxn][maxn];
    queue<p> Q;
    
    p pre[maxn][maxn];//巧妙的一点,可以用这种方法扩展到多维的路径的存储。
    //dfs可以用一个vector来存储,bfs没有办法用那种方法(这句话写给我自己看哈~)
    void bfs() {
    	while (!Q.empty()) Q.pop();
    
    	p s(1, 1);
    	maze[1][1] = 1;
    	Q.push(s);
    
    	while (!Q.empty()) {
    		p tp = Q.front();
    		Q.pop();
    		if (tp.a == 5 && tp.b == 5) return;
    
    		for (int i = 0; i < 4; i++) {
    			int xx = tp.a + dir[i][0];
    			int yy = tp.b + dir[i][1];
    
    			if (xx >= 1 && xx <= 5 && yy >= 1 && yy <= 5 && maze[xx][yy] != 1) {
    				p t(xx, yy);
    				Q.push(t);
    				maze[xx][yy] = 1;
    				pre[xx][yy] = tp;
    			}
    		}
    	}
    }
    void showpath(p a) {
    	if (a.a == 1 && a.b == 1) cout << '(' << (a.a - 1) << ", " << (a.b - 1) << ')' << endl;
    	else {
    		showpath(pre[a.a][a.b]);
    		cout << '(' << (a.a - 1) << ", " << (a.b - 1) << ')' << endl;
    	}
    
    }
    int main() {
    	for (int i = 1; i <= 5; i++)
    		for (int j = 1; j <= 5; j++)
    			cin >> maze[i][j];
    
    	bfs();
    	p a(5, 5);
    	showpath(a);
    }
    
    
    • 1

    信息

    ID
    2939
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者