Problem
HazelFan is given two positive integers a,b, and he wants to calculate a mod b. But now he forgets the value of b and only remember the value of a, please tell him the number of different possible results.
已知 a ,求对于任意的 b ,a % b 有多少种可能。
Idea
显然,对于一个 a
a % a 余 0
a % (a-1) 余 1
a % (a-2) 余 2
…
当然,余数最大的取值为 < a/2 ,
∵ a 为偶数时, a % (a/2+1) 余 a/2-1, a % (a/2) 余 0 ; a 为奇数时, a % (a/2+1) 余 a/2, a%(a/2) 余 1 .
同时,任意一个 b > a ,a % b 余 a 。
故总共的可能的余数为 [0,(a+1)/2)⋃a ,共 a+12+1 个
Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T, a;
scanf("%d", &T);
while(T-- && scanf("%d", &a))
printf("%d\n", (a+1)/2+1);
}