Acdream 1417 Numbers(暴力枚举)

传送门

Numbers

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Submit Statistic Next Problem

Problem Description

  Consider numbers from 1 to n.
  You have to find the smallest lexicographically number among them which is divisible by k.

Input

  Input file contains several test cases. Each test case consists of two integer numbers n and k on a line(1 ≤ n ≤ 1018, 1 ≤ k ≤ n).
  The last test case is followed by a line that contains two zeroes. This line must not be processed.

Output

  For each test case output one integer number — the smallest lexicographically number not exceeding n which is divisible by k.

Sample Input

2000 17

2000 20

2000 22

0 0

Sample Output

1003

100

1012

Hint

多组数据

Source

Andrew Stankevich Contest 22

题目大意:

给你一个数 n(n<10^18),让你求字典序最小的不超过 n 的能够整除 k 的最小值

解题思路:

在这里需要注意一下是字典序也就是说1001比123小,其实我们可以枚举10^p(p<=18),那么最多也就是18位,我们把10^p以后第一个对k取模等于0的数用数字组记录下来(注意枚举的时候不要超过n),最后的答案一定是在这里面,我们最后只需要将其转化为字符串进行字典序就行了。

My Code:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char str[20][20];
string s[20];
LL a[20];
int main()
{
    LL n, k;
    while(cin>>n>>k)
    {
        if(!n && !k)
            break;
        int p = 0;
        LL tp = n;
        while(tp)
        {
            p++;
            tp /= 10;
        }
        tp = 1;
        for(int i=0; i<p-1; i++)
            tp *= 10;
        int cnt = 0;
        LL tmp = tp%k;
        if(tmp)
            tmp = tp+k-tmp;
        else
            tmp = tp;
        if(tmp <= n)
            a[cnt++] = tmp;
        tp /= 10;
        while(tp)
        {
            tmp = tp%k;
            if(tmp)
                tmp = tp+k-tmp;
            else
                tmp = tp;
            if(tmp < tp*10)
                a[cnt++] = tmp;
            tp /= 10;
        }
        for(int i=0; i<cnt; i++)
        {
            int sum = 0;
            while(a[i])
            {
                int tp = a[i]%10;
                str[i][sum++] = tp+‘0‘;
                a[i] /= 10;
            }
            str[i][sum] = ‘\0‘;
        }
        for(int i=0; i<cnt; i++)
        {
            int len = strlen(str[i]);
            ///cout<<str[i]<<endl;
            for(int j=0; j<len/2; j++)
            {
                char ch = str[i][len-j-1];
                str[i][len-j-1] = str[i][j];
                str[i][j] = ch;
            }
        }
        for(int i=0; i<cnt; i++)
            s[i] = str[i];
        sort(s, s+cnt);
        cout<<s[0]<<endl;
    }
    return 0;
}
时间: 2024-10-15 04:59:21

Acdream 1417 Numbers(暴力枚举)的相关文章

ACdream 1417 Numbers

题目链接~~> 做题感悟:比赛的时候用的广搜,然后高高兴兴的写完果断TLE ,做题的时候无论什么题都要用笔画一下,模拟几组数据,这样也许就AC了(做题经验,有心者谨记!). 解题思路: 模拟几组数据你就会发现 ,一定的位数第一个大于此位数的  k  的倍数就是最小的.如果再大一倍就必定比此数的字典序小,所以只要模拟大于10 ,100,1000,1000,的第一个倍数就可以了. 代码: #include<iostream> #include<sstream> #include&

POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895   Accepted: 10906 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

区间Dp 暴力枚举+动态规划 Hdu1081

F - 最大子矩形 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located withi

hdu5143 暴力枚举

http://acm.hdu.edu.cn/showproblem.php?pid=5143 Problem Description NPY is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) is a sequence of numbers such that the difference between the consecutive term

こだわり者いろはちゃん / Iroha&#39;s Obsession (暴力枚举)

题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_a Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Iroha is very particular about numbers. There are K digits that she dislikes: D1,D2,…,DK. She is shopping, and now payi

暴力枚举——Help Me with the Game

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3394   Accepted: 2172 Description Your task is to read a picture of a chessboard position and print it in the chess notation. Input The input consists of an ASCII-art picture of a chessboar

HDU 5175 Misaki&#39;s Kiss again(数学,暴力枚举)

题目大意: After the Ferries Wheel, many friends hope to receive the Misaki's kiss again,so Misaki numbers them 1,2...N?1,N,if someone's number is M and satisfied the GCD(N,M) equals to N XOR M,he will be kissed again. Please help Misaki to find all M(1<=

UVA 725 Division ( 找出 abcde / fghij = N的形式—— 暴力枚举 )

Division Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divide

HDOJ 5305 Friends 暴力枚举

暴力枚举边的状态..... Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 120    Accepted Submission(s): 39 Problem Description There are n people and m pairs of friends. For every pair of friends,