A. Two Bags of Potatoes

來源:互聯網
上載者:User
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 ykn (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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.