lightoj1136

题目中给出的数是1,12,123,1234,12345,123456,1234567,12345678,123456789,12345678910,1234567891011。。。这种数字,问在第A个到第B个数之间有多少个数能被3整除。A <= 2^31,这个范围显然有点大了,不能枚举。

一个数能被3整除的等价情况就是这个数的各个位上的数的和能被3整除,而且上面的数字是连续的,对于连续的三个数字必然是能够被3整除的,但是如12345678910这个数来看,重点是后面这个10 怎么处理,其实有个公式(10*k + b) % 3 == (k + b) % 3,也就是这下数在按照每个位的时候是可以连在一起当做一个整体的,就是(1+2+3+4+5+...+9+1+0) % 3 == (1+2+...+9+10),这样就满足上面的连续数了,那么第(3K+1)的数组成的数当中呢后面3K个连续的数是能够被整除的,但是前面还有个1是不能的,对于第(3K+2)的数,后面的3K个数是可以整除的,前面1+2也是可以的,所以就是k/3*2+(k%3==2?1:0)。

n*(n+1)/2 % 3 == 0 ->n % 3 != 1。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
LL calc(LL k){
    return k/3*2+(k%3==2?1:0);
}
int main()
{    
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    int t,icase=0;
    cin >> t;
    while(t--){
        LL a,b;
        cin >> a >> b;
        printf("Case %d: %lld\n",++icase,calc(b)-calc(a-1));
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值