sgu 169 Numbers

39 篇文章 0 订阅
14 篇文章 0 订阅
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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值