time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes,
and the second — y (y ≥ 1) potatoes.
Valera — very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of
potatoes (x + y) in the two bags, firstly, was not gerater than n,
and, secondly, was divisible by k.
Help Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order.
Input
The first line of input contains three integers y, k, n (1 ≤ y, k, n ≤ 109; ≤ 105).
Output
Print the list of whitespace-separated integers — all possible values of x in ascending order. You should print each possible value of x exactly
once.
If there are no such values of x print a single integer -1.
Sample test(s)input
10 1 10
output
-1
input
10 6 40
output
2 8 14 20 26
解題說明:此題的想法很簡單,求一個滿足下面兩個式子的數
x+y<=n
(x+y)%k=0
這裡可以把x+y當成一個整體,找到滿足要求的x+y即可求出對應的x,這裡把x+y記為M,簡單來看M取值範圍應該是y到n,不過注意到x肯定不為0,而且y不一定被k整除,故M最小取值限制為(y/k+1)*k【這裡和y+k值是不等的】,然後增加的步長大小k,直到n即可
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;int main() {int y,n,k,i;scanf("%d %d %d",&y,&k,&n);for(i=(y/k+1)*k;i<=n;i+=k){printf("%d ",i-y);}if((y/k+1)*k>n){printf("-1");}return 0;}