SGU 169

G - Numbers
Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

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

Author: All-Russian mathematical olympiad jury
Resource: District mathematical olympiad, 8th form
Date:  











Server time: 2016-05-08 06:19:18 Online 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值