题解

虚空终端  •  1年前


#include<iostream>
using namespace std;
int Gcd(int a, int b, int& x, int& y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}

	int g = Gcd(b, a % b, x, y);
	int temp = x;
	x = y;
	y = temp - a / b * y;
	return g;
}
int main()
{
	int a, b, m;
	cin >> a >> b >> m;
	int g;
	int x, y;
	g = Gcd(a, m, x, y);
	int c = b / g;
	if (b % g != 0)
	{
		cout << "No solution";
		return 0;
	}
	int d = m / g, ans = x*c;
	if (ans < 0)
	{
		while (ans < 0)
			ans += d;
	}
	else
	{
		while (ans > 0)
			ans -= d;
		ans += d;
	}
	while (ans < m)
	{
		cout << ans <<' ';
		ans += d;
	}
	return 0;
}

评论:

请先登录,才能进行评论