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.
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···ak-1
由此可得出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 using namespace std; 3 int k; 4 int main() 5 { 6 while(scanf("%d",&k)!=EOF) 7 { 8 k--; 9 int ans=1; 10 if(!k) puts("8"); 11 else 12 { 13 if(k%3==0) ans+=2; 14 if(k%6==0) ans++; 15 pi(ans); 16 } 17 } 18 return 0; 19 }