SGU 169 numbers 数学

169.Numbers
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

题意:一道很有意思的题,看到这个题的时候就很容易想到这道题是有规律的,但最后也是查找题解才找到的

规律就是通过计算,得到k位数除个位数之外,所有的非个位数都为1,所以只需看最后一位的情况与前面组成的数能构成多少perfect number

假设n的各个位数为a1,a2···ak,那么n+1的各个位数为a1,a2···ak+1

因为要求n mod P(n)=0,所以有n=s1*a1*a2···*ak,n+1=s2*a1*a2···*(ak+1)

在此处s1与s2因为n mod P(n)=0所以肯定是整数(因为n一定是P(n)的m倍),所以有1=[s2*(ak+1)-s1*ak]*a1*a2···

由此可得出a1,a2···肯定为1

设个位数为x

x=1,   perfect numbers

x=2,   当6|(k-1)时是perfect numbers

x=3,不是

x=4,不是

x=5,当3|(k-1)时是perfect numbers

x=6,当6|(k-1)时是perfect numbers

x=7,不是

x=8,不是

代码:

 1 //#include"bits/stdc++.h"
 2 #include<sstream>
 3 #include<iomanip>
 4 #include"cstdio"
 5 #include"map"
 6 #include"set"
 7 #include"cmath"
 8 #include"queue"
 9 #include"vector"
10 #include"string"
11 #include"cstring"
12 #include"time.h"
13 #include"iostream"
14 #include"stdlib.h"
15 #include"algorithm"
16 #define db double
17 #define ll long long
18 #define vec vectr<ll>
19 #define mt  vectr<vec>
20 #define ci(x) scanf("%d",&x)
21 #define cd(x) scanf("%lf",&x)
22 #define cl(x) scanf("%lld",&x)
23 #define pi(x) printf("%d\n",x)
24 #define pd(x) printf("%f\n",x)
25 #define pl(x) printf("%lld\n",x)
26 //#define rep(i, x, y) for(int i=x;i<=y;i++)
27 #define rep(i, n) for(int i=0;i<n;i++)
28 const int N   = 1e4+ 5;
29 const int mod = 1e9 + 7;
30 const int MOD = mod - 1;
31 const int inf = 0x3f3f3f3f;
32 const db  PI  = acos(-1.0);
33 const db  eps = 1e-10;
34 using namespace std;
35 int k;
36 int main()
37 {
38     while(scanf("%d",&k)!=EOF)
39     {
40         k--;
41         int ans=1;
42         if(!k) puts("8");
43         else
44         {
45             if(k%3==0) ans+=2;
46             if(k%6==0) ans++;
47             pi(ans);
48         }
49     }
50     return 0;
51 }

猜你喜欢

转载自www.cnblogs.com/mj-liylho/p/8962320.html
sgu
169