A

来源:互联网 发布:webstorm运行js代码 编辑:程序博客网 时间:2024/05/17 22:40


A - Division by 3

 LightOJ - 1136


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 by3.

Sample Input

2

3 5

10 110

Sample Output

Case 1: 2

Case 2: 67


题意:就是在一个由等差数列前n项和组成的数列中,给你一个区间问有多少个3的倍数。


这道题看似简单却错了好几次,第一次暴力解超时,后来换思路,WA。又换思路又WA,心累,,,,


#include<stdio.h>int main(){int T;int t=1;scanf("%d",&T);while(T--){int A,B;int sum1=0,sum2=0;scanf("%d%d",&A,&B);if(A%3==0)sum1++;if((B+1)%3==0)sum1++;printf("Case %d: %d\n",t++,(B-A)/3*2+sum1);//第三次算是第二次的简化版,思路一致判断条件更好一点。 //************************** //if(A%3==1)//sum1=A/3*2;//if(A%3==2)//sum1=A/3*2+1;//if(A%3==0)//sum1=A/3*2-1;//第二次发现数列中每三项就有两个三的倍数 //if(B%3==1)//用区间端点直接算结果一改再改,一错再错 //sum2=B/3*2;//if(B%3==2)//sum2=B/3*2+1;//if(B%3==0)//sum2=B/3*2+1;//****************************//int sum=0;//for(int i=A;i<=B;i++)//{//int ans=i*(1+i)/2;//第一次暴力,超时 //if(ans%3==0)//sum++;//}//***************************** }return 0; } 








原创粉丝点击