lightoj(1136 - Division by 3 )规律

来源:互联网 发布:淘宝手机店铺收藏地址 编辑:程序博客网 时间:2024/04/29 09:39

There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Nowyou are given two integersA and B, you have to find the numberof integers fromAth number to Bth (inclusive)number, which are divisible by3.

For example, let A = 3. B = 5. So, the numbers in thesequence are, 123, 1234, 12345. And 123, 12345 are divisible by 3. So, theresult 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 numbersin the sequence betweenAth and Bth whichare divisible by3.

Sample Input

Output for Sample Input

2

3 5

10 110

Case 1: 2

Case 2: 67

 题意

有序列1,12,123,1234,…,12345678910,…现在你是给定两个整数a和b,你必须从第A个找到第B个整数(含),且能被3整除。

例如,让一个= 3。B = 5。因此,序列中的数字是,123,1234,12345。123、12345能被3整除。所以,结果是2。

思路

找规律:1,0,0,1,0,0,1,0,0,1,0,0,1,0,0......

1是不可整除,0是可整除。

代码

#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#define in(a,b) scanf("%d%d",&a,&b)using namespace std;int main(){int i,j,k,m,n,T,ans;scanf("%d",&T);k=T;while(T--){ans=0;in(m,n);while(m%3!=1){m++;ans++;}while(n%3!=1){n--;ans++;}ans=ans+(n-m)/3*2;printf("Case %d: %d\n",k-T,ans);}return 0;}


1 0
原创粉丝点击