BUPT2017 wintertraining(16) #5 C
HDU - 1021

题意

1, 12, 123, 1234, ..., 12345678910, ... 问第a到第b个数(inclusive)里有几个可以被3整除。

题解

前几项的各位数之和对3取模,可以找到规律——余数是1,0,0,1,0,0,1,0,0,1...。也就是每三个数有两个可被3整除。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int T;
int get(int x){
	return x/3*2+(x%3>1);
}
int main() {
	scanf("%d",&T);
	for(int c=1,a,b;c<=T;c++){
		scanf("%d%d",&a,&b);
		printf("Case %d: %d\n",c,get(b)-get(a-1));
	}
	return 0;
}

相关文章:

  • 2021-05-28
  • 2021-07-11
  • 2021-11-23
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2021-11-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案