【2017河南省省賽個人選拔賽補題】hdu4722 Good Numbers9(找規律)+CodeForces 729D(思路題)

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.
 

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 <= 1018).
 

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: 1
Hint
The 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);

    }
}


D. Sea Battle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of bconsecutive cells. No cell can be part of two ships, however, the ships can touch each other.

Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss").

Galya has already made k shots, all of them were misses.

Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.

It is guaranteed that there is at least one valid ships placement.

Input

The first line contains four positive integers nabk (1 ≤ n ≤ 2·1051 ≤ a, b ≤ n0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made.

The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string.

Output

In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.

In the second line print the cells Galya should shoot at.

Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left.

If there are multiple answers, you can print any of them.

Examples
input
5 1 2 1
00100
output
2
4 2
input
13 3 2 3
1000000010001
output
2
7 11
Note

There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.


題意:長度爲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");
}


發佈了70 篇原創文章 · 獲贊 25 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章