Time Limit: 2 second(s) | Memory Limit: 32 MB |
There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Now you are given two integers A and B, you have to find the number of integers from Ath number to Bth (inclusive) number, which are divisible by 3.
For example, let A = 3. B = 5. So, the numbers in the sequence are, 123, 1234, 12345. And 123, 12345 are divisible by 3. So, the result is 2.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains two integers A and B (1 ≤ A ≤ B < 231) in a line.
Output
For each case, print the case number and the total numbers in the sequence between Ath and Bth which are divisible by 3.
Sample Input | Output for Sample Input |
2 3 5 10 110 | Case 1: 2 Case 2: 67 |
PROBLEM SETTER: JANE ALAM JAN
题目大意:给你一些序列1,12,123,1234,12345,123456,1234567,12345678,123456789,12345678910,1234567891011,.....给你两个数字a,b,问第a个序列到第b个序列之间能被三整除的数的个数。
解题思路:发现规律,每三个序列为一组1,12,123,第一个数不能被3整除,第二个第三个数能被三整除,1234,12345,123456,第一个数不能被三整除,第二个和但三个数能被三整除。
代码如下:
#include <cstdio>
int main()
{
int t;
scanf("%d",&t);
int kcase=1;
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
int tmp1=b/3;//找到前面有多少组3
tmp1=tmp1*2;//每组有两个能被三整除
if(b%3==2)//看这个数本身能否被3整除,即排在该组的第二个数
{
tmp1=tmp1+1;
}
int tmp2=a/3;//同上
tmp2=tmp2*2;
if(a%3==2)
{
tmp2=tmp2+1;
}
int ans=tmp1-tmp2;
if(a%3==0||a%3==2)//如果是第三个数或第二个数,那么多减了a这个数,那么补上去
{
ans++;
}
printf("Case %d: %d\n",kcase++,ans);
}
return 0;
}