sgu 169 Numbers

来源:互联网 发布:淘宝网宠物窝 编辑:程序博客网 时间:2024/05/01 03:15

169. Numbers

time limit per test: 0.5 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


k=1 时情况比较特殊,有8种
如果k>1 我们研究除最后一位的其他位,我们将证明他们都是1
否则假设 有某一位不为 1 假设为a (a>1 )

那么n%a==0 将n+1 我们将发现 (n+1) % a !=0
否则假设a|(n+1)

我们有

n=k*a ,n=k*a+1

k*a+1 = 0 (mod a)
1 = 0 mod(a)

a=1矛盾!

所以除最后一位的其他位均为1

现在我们来看看最后一位有几种可能。

显然最后一位可以是 1,2,5
不能为 4 8
最后一位若是3,则有(k-1)%3==0
最后一位若是7 则有(k-1)%6==0

有了以上结论,我们就很容易解决本道题。

贴代码:
#include<iostream>#include<cstring>#include<cstdio>#include<set>#include<algorithm>#include<vector>#include<cstdlib>#define inf 0xfffffff#define CLR(a,b) memset((a),(b),sizeof((a)))#define FOR(a,b) for(int a=1;a<=(b);(a)++)using namespace std;int const nMax = 1010;int const base = 10;typedef int LL;typedef pair<LL,LL> pij;//#define int long long//    std::ios::sync_with_stdio(false);int k;/*测试用的 int sum(int n) {    int s=1;    while(n){        s*=(n%10);        n/=10;    }    return s;}bool f(int n) {    int s=sum(n) ;    if(s&&n%s==0) return true;    return false;}bool g(int n) {    return f(n)&&f(n+1) ;}main(){    int s=0;    for(int i=1;i<10000000ll;i++)if(g(i)) cout<<i<<endl;    return 0;}*/int main(){    cin>>k;    if(k==1){         cout<< 8 << endl;         return 0;    }    int ans=1;    if(k%6==1&&k%3==1) ans++;    if(k%3==1) ans+=2;    cout<<ans<<endl;}