G - Good Numbers UESTC - 832 (打表 找规律)
If we sum up every digit of a number and the result can be exactly divided by 1010 , we say this number is a good number.
You are required to count the number of good numbers in the range from AA to BB , inclusive.
Input
The first line has a number TT (T≤10000T≤10000 ) , indicating the number of test cases.
Each test case comes with a single line with two numbers AA and BB (0≤A≤B≤10180≤A≤B≤1018 ).
Output
For test case XX , output Case #X:
first, then output the number of good numbers in a single line.
Sample Input
2 1 10 1 20
Sample Output
Case #1: 0 Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.
分析:题目中的数据很大,所以可以往找规律的方面考虑
打表之后会发现,每10个数都会有一个符合要求的数
如果第十个数(整除10的数)是符合要求的数 就再加一个
代码如下:
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; typedef long long ll; #define INF 0x7fffffff int main() { ll a,b,cnta,cntb,h,num,t; cin>>t; for(ll r=1;r<=t;r++) { cin>>a>>b; a=a-1; cnta=0; cntb=0; cnta=a/10; h=a-a%10; num=0; while(h>0) { num+=h%10; h=h/10; } if(num%10==0) cnta++; for(ll i=a-a%10+1;i<=a;i++) { // puts("1"); h=i; num=0; while(h>0) { num+=h%10; h=h/10; } if(num%10==0) cnta++; } if(a<0) cnta=0; cntb=b/10; h=b-b%10; num=0; while(h>0) { num+=h%10; h=h/10; } if(num%10==0) cntb++; for(ll i=b-b%10+1;i<=b;i++) { h=i; num=0; while(h>0) { num+=h%10; h=h/10; } if(num%10==0) cntb++; } // cout<<cnta<<endl; // cout<<cntb<<endl; printf("Case #%lld: %lld\n",r,cntb-cnta); } }