SGU - 169 - Numbers (觅规律)
169. Numbers
memory limit per test: 4096 KB
output: standard
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
Output
Sample test(s)
Input
Author: | All-Russian mathematical olympiad jury |
Resource: | District mathematical olympiad, 8th form |
Date: |
出得很巧妙的一道数学题
自己推导总感觉哪里少了,但是又没有发现,无奈搜了下题解,瞬间知道了,推导参考:点击打开链接
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时同上不行
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int k; int main() { while(scanf("%d", &k) != EOF) { if(k == 1) { printf("8\n"); continue; } int cnt = 1; if((k - 1) % 3 == 0) { cnt += 2; if((k - 1) % 6 == 0) { cnt ++; } } printf("%d\n", cnt); } return 0; }