利用等差数列公式就行了,可以考虑特判一下m >= n($ m, n eq 1 $),这时一定输出“AIYAMAYA”。
#include <iostream> using namespace std; int main() { int m, n; cin >> m >> n; int s = 0, f = 0; for (int a1 = 1; s < m + 1; a1 += 2) { int an = a1 + (n - 1)*2; s = n*(a1 + an)/2; if (s == m) { f = 1, s = a1; break; } } if (f) for (int i = 1; i <= n; i++) cout << s + (i - 1)*2 << ' '; else cout << "AIYAMAYA" << endl; return 0; }