SGU 169

来源:互联网 发布:java中while的用法 编辑:程序博客网 时间:2024/05/01 00:15
G - Numbers
Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u
Submit Status Practice SGU 169

Description

169. Numbers

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB
input: standard 
output: standard



Let us call P(n) - the product of all digits of number n (in decimal notation). 
For example, P(1243)=1*2*4*3=24; P(198501243)=0. 
Let us call n to be a good number, if (p(n)<>0) and (n mod P(n)=0). 
Let us call n to be a perfect number, if both n and n+1 are good numbers. 

You are to write a program, which, given the number K, counts all such 
numbers n that n is perfect and n contains exactly K digits in decimal notation.

Input
Only one number K (1<=K<=1000000) is written in input.

Output
Output the total number of perfect k-digit numbers.

Sample test(s)

Input
1
Output
8
[submit]
[forum]

Author:All-Russian mathematical olympiad juryResource:District mathematical olympiad, 8th formDate: 










Server time: 2016-05-08 06:19:18Online Contester Team © 2002 - 2016. All rights reserved.题解:
P(n)定义为n的所有位数的乘积,例如P(1243)=1*2*3*4=24,然后如果P(n)!=0且n mod P(n) = 0,则称n为good number.
如果n和n+1都为good numbers,则称n为perfect number。然后给出位数k(1<=k<=1000000),找出位数为k的perfect number.

题目看起来很玄虚,而且给出的位数k很大,其实越大的数据不是用高精度的话往往就会有简便的方法,这道题也不例外。
设n有i位,各位分别为a1,a2,...,ai,因为个位为9的数不可能为perfect number(因为n+1不是good number)。
所以n+1的各位分别为a1+1, a2, a3, ... , ai
因为要求n mod P(n) = 0,所以n = s*a1*a2*...*ai,类似的有n+1 = t*(a1+1)*a2*a3*...*ai
所以(n+1)-n = 1 = [t*(a1+1)-s*a1]*a2*a3*...*ai
所以可以推出a2,a3,... ,ai必都为1,则有a1 | n, (a1+1) | (n+1)
所以只需考虑a1的情况,a1有8个取值,(考虑位数大于1的情况)
a1=1时,显然是可以的。
a1=2时,需要判断3能否整除n+1,因为前面有k-1个1,所以只需判断(k-1+3)%3是否等于0
a1=3时,(a1+1)=4,显然4不能整除14,所以3不行
a1=4同上也不行
a1=5时,判断6能否整除n+1,显然与判断a1=3一样
a1=6时,判断7能否整除n+1,经过简单的除法计算可以知道当前面1的个数(k-1)是6的倍数时才有7 | (n+1)
a1=7时,8不能整除118,所以7不行
a1=8时同上不行;
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int main(){    int k;    scanf ("%d", &k);    if (k == 1)    {        printf ("8\n");        return 0;    }    int count = 1;    if ((k-1)%3==0)    {        count += 2;        if ((k-1)%6==0)        {            count++;        }    }    printf ("%d\n", count);    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 天热眼睛有眼屎怎么办 眼睛里膜起来了怎么办 眼睛一边大一边小怎么办 眼白膜鼓起来了怎么办 主持问答环节没人提问怎么办 转学原学校不给怎么办 村长借东西不还怎么办 村长不上报建房申请怎么办 村长不上报建房手续申请怎么办 村长不给村民盖章怎么办? 找村干部办事难怎么办 洪洞县村长不给我盖章怎么办 睾丸穿刺取精只配到6个胚胎怎么办 孩子一只耳朵听不到声音怎么办 孕早期孕囊生长慢怎么办 试管2次不着床怎么办? pescm球员年龄大了怎么办 实况足球俱乐部经理球员老了怎么办 你不是我的菜怎么办 苹果平板电脑耳机有回音怎么办 obs直播有电流音怎么办 语音里网吧很吵怎么办 电脑k歌有延迟怎么办 想开个跆拳道馆怎么办营业执照? 壶嘴拐弯处漏水怎么办 裂纹茶壶嘴坏了怎么办 小孩刚上学怕她上火怎么办 在幼儿园小朋友不愿叠衣服怎么办 孩子在家听话幼儿园不听话怎么办 变魔术时观众说看过这个怎么办 孕妇8个月摔跤了怎么办 孩子吃了残奶怎么办 小米手机变卡了怎么办 主持时说错话了怎么办 小鲜肉老了不红怎么办 同学聚会大家玩手机你怎么办 率土之滨被掠夺怎么办 戒指戴手上取不下来怎么办 择离开我我该怎么办 解小手解不出来怎么办 学生把班里的班费弄丢了怎么办