Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4714 Accepted Submission(s): 1499
You are required to count the number of good numbers in the range from A to B, inclusive.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 1018).
求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);
}
}
思路:找出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");
}