Codeforces-289a I Polo the Penguin and Segments

A. Polo the Penguin and Segments
time limit per test
2 seconds
memory limit per test
256 megabytes

Little penguin Polo adores integer segments, that is, pairs of integers [lr] (l ≤ r).

He has a set that consists of n integer segments: [l1r1], [l2r2], ..., [lnrn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [lr] to either segment [l - 1; r], or to segment [lr + 1].

The value of a set of segments that consists of n segments [l1r1], [l2r2], ..., [lnrn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.

Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.

It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).

Output

In a single line print a single integer — the answer to the problem.

Sample test(s)
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0

——————————————————计划满满的分割线——————————————————

思路:此题考的是读懂题意。。。。。。读懂后,你发现它其实就是让你统计整数的个数,看它能否被k整除,不能的话,需要增加ans或者减少ans才可以(ans取最小的)

代码如下:

#include 
int main(){
	int n, add = 0, k;
	int x, y, mod, ans;
	scanf("%d%d", &n, &k);
	while(n--){
        scanf("%d%d", &x, &y);
        add += (y - x + 1);
	}
    mod = add % k;
    if(mod)  ans = k - mod;
    else  ans = 0;
    printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(ACM-Codeforces)