Codeforces 584A Olesya and Rodion 【构造】

本文介绍了一道编程题目,要求构造一个n位的正整数使其能被t整除,若无法构造则输出-1。文章给出了具体实现思路及AC代码。

题目链接:Codeforces 584A Olesya and Rodion

A. Olesya and Rodion
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. Find some number that satisfies both of them.

Your task is: given the n and t print an integer strictly larger than zero consisting of n digits that is divisible by t. If such number doesn’t exist, print  - 1.

Input
The single line contains two numbers, n and t (1 ≤ n ≤ 100, 2 ≤ t ≤ 10) — the length of the number and the number it should be divisible by.

Output
Print one such positive number without leading zeroes, — the answer to the problem, or  - 1, if such number doesn’t exist. If there are multiple possible answers, you are allowed to print any of them.

Examples
input
3 2
output
712

题意:让你构造一个n位的正整数使得它可以被t整除,不存在输出-1。

思路:特判,先填好前面,然后后面全填0。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e4 + 7;
const int MAXN = 1e5 + 10;
void add(LL &x, LL y) { x += y; x %= MOD; }
int main()
{
    int n, t;
    while(scanf("%d%d", &n, &t) != EOF) {
        if(n == 1 && t == 10) {
            printf("-1\n");
            continue;
        }
        if(t == 10) {
            t = 1;
        }
        printf("%d", t);
        for(int i = 1; i < n; i++) {
            printf("0");
        }
        printf("\n");
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值