博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2551 Ones(我的水题之路——重点,末尾有几个1)
阅读量:4069 次
发布时间:2019-05-25

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

Ones
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9058   Accepted: 5139

Description

Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?

Input

Each line contains a number n.

Output

Output the number of digits.

Sample Input

3 7 9901

Sample Output

3612

Source

题意理解起来也有一定困难。
给一个数字n,问,在其末尾加上多少个1,可以被n本身整除,如:3,加上3个1,为3111可以被3整除;7,加上7个1,为71111111.
这道题很难想,说实话不算水题,看到末尾加1,首先想到的是高精度,甚至想好了用java,之后用了long long暴力试了试,果然范围不够- -,其实想想也知道,之后看了discuss才知道正确解法,在这里向大牛致敬。
将一个数取下来,然后从1对n本身取模(%),之后将取模之后的数字乘以10加1,再取模,直到取模为0.
请先看下面的代码,在这里我做解释。
如n=3. 结果为3111.
   第一次,第一个1加上为 31 = 30 + 1, 其中30可以被3整除,所以之后的计算,我们仅需要考虑1即可。
   第二次,我们对1后面加上一个1,为1 * 10 + 1 = 11 = 9 + 2.其中9可以被3整除,需要对2末尾加1,再整除3即可。
   第三次,我们对2后面加上一个1,为21可以整除3,循环结束。
   所以我们一共在n末尾加上了三个1.输出3.
注意点:
1)由这里,整理一个规律(可能已经有定理了= =):就是说一个大数,如果要对于它进行操作然后取整,可以先对其取模,之后再对于其余数进行后续处理。
代码(1AC):
#include 
#include
#include
int main(void){ int num; int n; int i, j; while (scanf("%d", &n) != EOF){ num = i = 1; num %= n; for (; num; i++){ num = num * 10 + 1; num %= n; } printf("%d\n", i); } return 0;}

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

你可能感兴趣的文章
web test flow
查看>>
web test LoadRunner SAP / java / Java Vuser / web_set_max_html_param_len
查看>>
OS + UNIX AIX command
查看>>
OS + UNIX AIX performance
查看>>
OS + UNIX AIX Tools
查看>>
my ReadBook_liutongjingjixue / circulation economics
查看>>
my ReadBook_wangluoyingxiaoyucehua / network marketing / wangluoyingxiao
查看>>
db base database
查看>>
Spring2.5+MINA2搭建Socket Server
查看>>
jcharts画线图,饼图和柱状图
查看>>
监控服务器端口,Down掉会自动重启,并发送邮件 Linux Shell
查看>>
Git提交错误:RPC failed; result=22, HTTP code = 411
查看>>
Druid使用ConfigFilter
查看>>
Elicpse使用技巧-打开选中文件文件夹或者包的当前目录
查看>>
eclips 运行项目内存不足的解决方案
查看>>
linux 挂载盘阵 smb
查看>>
漫谈 JAVA程序员、架构师、项目经理
查看>>
ceph (luminous 版) crushmap 与 pool结合用于物理划分 IO 使用域
查看>>
mysql 相关索引
查看>>
tomcat7 - cacti 备忘
查看>>