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 }

原文地址:https://www.cnblogs.com/mj-liylho/p/8962320.html

时间: 2024-07-28 21:42:49

SGU 169 numbers 数学的相关文章

SGU 169 Numbers (找规律)

题意:中文题,直接忽略... 析:先说说我的思路,我一看这个题第一感觉就是要找规律,要是挨着算,猴年马月都跑不完,更何况时间限制是0.25s,怎么找规律呢,我算了一下前10位,分别是8,1,1,3,1,1,4,1,1,3,然后我就觉得应该是113114循环再加一第一位是8,果然AC了. 然后结束后我看看了题解好像是算出来的,因为数很大又不是高精度,肯定是要找规律了,假设n有k位,分别从右往左a1,a2...ak,首先a1(也就是个位)肯定不是9(因为如果是9,那么n+1就有0了),所以呢n+1各

sgu 169 Numbers

题意:n和n+1同时被数位乘积整除的k位数个数. 假如a是237,b是238.由于个位以前的数一样.那么对于2,如果a%2==0,b%2就!=0,如果a%3==0,b%3就!=0.因此个位以前的数只能是1.再列举个位的情况. #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <ioman

POJ 3252 Round Numbers 数学题解

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

UVA 11481 - Arrange the Numbers 数学

Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of ?rst N natural numbers. You canearrange this sequence in many ways. There will be N! di?erent arrangements. You have to calculatethe number of arrangement of ?rst N natural numbers

POJ2402/UVA 12050 Palindrome Numbers 数学思维

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionallynumbers can of course be ordered in size. The ?rst few

UVA 11582 Colossal Fibonacci Numbers! 数学

n比较小,最多n*n就回出现循环节.... Colossal Fibonacci Numbers! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem F: Colossal Fibonacci Numbers! The i'th Fibonacci number f (i) is recursively defined in the

poj 3252 Round Numbers(数学)

链接:http://poj.org/problem?id=3252 题意:一个数写成二进制,0不少于1就是round number,求给定区间内round number的个数. 分析:显然第一步转化为0到n的round number个数..设n写成二进制有len位,第一位取0时,后面只要满足0的个数要求就行了,不用考虑是否比n大..可以先预处理一下长度不大于len的round number个数,记做t[len],t[len]=t[len-1]+∑C(len-1  k) , (k=0,1,...,

HDU 3117 Fibonacci Numbers 数学

http://acm.hdu.edu.cn/showproblem.php?pid=3117 fib是有一个数学公式的. 这里的是标准的fib公式 那么fib = 1 / sqrt(5) * ((1 + sqrt(5) / 2) ^ n - ((1 - sqrt(5)) / 2)^n) = 1 / sqrt(5) * (A^n - B^n) 那么,求后4位可以直接矩阵快速幂. 不能用上面公式的快速幂取模,因为存在精度误差. 然后求前4位的话,就是一个套路公式了. 在上一篇博客.http://ww

[AGC011E] Increasing Numbers [数学]

题面 传送门 思路 首先,我们观察一下上升数的性质 可以发现,它一定可以表示为最多9个全是1的数字的和 那么我们设$N$可以被表示成$k$个上升数的和,同时我们设$p_i=\underbrace{111\cdots 11}_{i}$ 我们令$a_{i,j}$表示构成$N$的第$I$个上升数的第$j$个全1数的位数 那么可以写出这样的式子 $N=\sum_{i=1}^k\sum_{j=1}^9 p_{a[i][j]}$ 我们发现,$p_{i,j}$这样子摆在这里非常不好操作,那么我们继续观察$p_