博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 394 B. Very Beautiful Number(思路较难,优化的地方多)好题。。。
阅读量:4035 次
发布时间:2019-05-24

本文共 2792 字,大约阅读时间需要 9 分钟。

1、

2、题目大意:

写出一个n位数,使得n*x的值正好是n的最后一位移到第一位的那个数,如果有输出来满足条件的最小的那个数,如果没有输出Impossible
思路:
因为n非常大,所以这道题目不可能一个数一个数的遍历,但是x是非常小的,而我们可以知道这个n位数的最后一位是>=x的,那么我们可以遍历最后一位,我们知道最后一位了,自然结果的第一位就知道了,知道了结果的第一位,原来的第一位就可以推出来,原来的第一位就是结果的第二位,
举个例子来说,n=6 x=5
我们用?代表该位未知
那么按照最后一位来假设这个六位数,每步的结果分别是
?????6      6 ????0,    5*6=30所以最后一位是0
1 ????6      6 ????0     前边数的第一位是这么计算的6/5=1...1
1 ? ? ? ? 6      6 1 ? ? ? 0     后边的1是来自前边数的第一位,因为他们相差一位
1 2 ?? ? 6      6 1 ? ? ? 0     6/5余1,对于6后边的1来说就相当于是10,11/5=2....1
1 2 ?? ? 6      6 1 2 ? ? 0     后边的步骤都同前三步了,不再解释
1 2 2 ? ? 6      6 1 ? ? ? 0
1 2 2 ? ? 6      6 1 2 ? ? 0
1 2 2 4 ? 6      6 1 2 ? ? 0
1 2 2 4 ? 6      6 1 2 4 ? 0
1 2 2 4 8 6      6 1 2 2 4 0     最后一位应是8,不是0,所以不对
那么当n=7,就是正确的,不再证明

3、题目:

B. Very Beautiful Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office.

The teacher remembers that the "very beautiful number" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the "very beautiful number" is minimal possible.

The teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the "very beautiful number". Help to restore the justice, find the "very beautiful number" for us!

Input

The single line contains integers p, x (1 ≤ p ≤ 106, 1 ≤ x ≤ 9).

Output

If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.

Sample test(s)
Input
6 5
Output
142857
Input
1 2
Output
Impossible
Input
6 4
Output
102564
Note

Sample 1: 142857·5 = 714285.

Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".

 

4、AC代码:

#include
#include
#include
using namespace std;#define N 1000005int a[N];int b[N];int main(){ int p,x,ans,flag=0; scanf("%d%d",&p,&x); for(int i=x;i<=9;i++) { a[p]=i; b[1]=i; ans=0; for(int j=1;j<=p;j++) { a[j]=(ans*10+b[j])/x; ans=(ans*10+b[j])%x; b[j+1]=a[j]; } if(b[p]==(i*x)%10) { for(int k=1;k<=p;k++) printf("%d",a[k]); printf("\n"); flag=1; break; } } if(flag==0) printf("Impossible\n"); return 0;}

 

转载地址:http://jwcdi.baihongyu.com/

你可能感兴趣的文章
Eclipse中有用的快捷键
查看>>
mysql将表字段信息拼接转换成实体类中的属性书写格式
查看>>
有return的情况下try catch finally的执行顺序
查看>>
input文本框中value值有双引号的问题
查看>>
java多线程简介
查看>>
web.xml配置加载顺序
查看>>
ServletContextListener使用详解
查看>>
UrlRewriteFilter使用说明
查看>>
java对redis的基本操作
查看>>
Java Math的 floor,round和ceil的使用
查看>>
通过url方式传递中文乱码解决办法
查看>>
Java的初始化机制、垃圾回收机制和内存分配机制
查看>>
MySQL5.6安装步骤(windows7/8_64位)
查看>>
FreeMarker基础配置
查看>>
Java中使用Jedis操作Redis
查看>>
Redis中常用命令
查看>>
spring下载
查看>>
读取request流
查看>>
微信消息模板的配置
查看>>
Spring框架结合Quartz实现任务调度实例
查看>>