Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4714 Accepted Submission(s): 1499
Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
You are required to count the number of good numbers in the range from A to B, inclusive.
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
Output
For test case X, 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: 1HintThe answer maybe very large, we recommend you to use long long instead of int.
求A到B之间各位数之和能被10整除的数的个数
打表可以看出来每十个数之间必有一个这样的数,n/10得到前面的个数,在加上n/10*10~n中有没有满足的 ,如 128,120-128之间127满足,即12+1个
code:
#include<stdio.h> typedef long long LL; LL solve(LL n) { LL p=n/10; LL sum=0; for(LL i=p*10; i<=n; i++) { LL s=i; sum=0; while(s) { sum+=s%10; s/=10; } if(sum%10==0) return p+1; } return p; } int main() { int T,t=0; scanf("%d",&T); while(T--) { LL a,b,ans; scanf("%lld%lld",&a,&b); ans=solve(b)-solve(a-1); printf("Case #%d: ",++t); printf("%lld\n",ans); } }
题意:长度为n的0,1字符串表示n个位置,共有a个长度为b的轮船,1 表示已经射击k次但并没有射击到船的位置,求至少还要射击几次才能射击到至少一艘轮船思路:找出0位置可以有轮船的所有位置,假设有x个位置可能有轮船,射击x-(a-1)次,必定射击中一艘轮船
code:
#include<stdio.h> using namespace std; int pos[200005]; int main() { char s[200005]; int n,a,b,k; scanf("%d%d%d%d",&n,&a,&b,&k); scanf("%s",s+1); int sum=0,l=0; for(int i=1;i<=n;i++) { if(s[i]=='1') { sum=0; } else { sum++; if(sum%b==0) { pos[l++]=i; } } } int ans=l-(a-1); printf("%d\n",ans); for(int i=0;i<ans;i++) { printf("%d ",pos[i]); } printf("\n"); }