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各位分别是a1+1,a2...ak,因为n mod P(n) = 0,所以n = s * a1 * a2 *...* ak,n+1 = t * (a1+1) * a2 *... * ak,即:

n+1 - n = 1 = [t*(a1+1) - s*a1] * a2 * a3 *...* ak。所以可以得出a2,a3,...ai都为1。所以 a1 | n, (a1+1) | (n+1), ("|"表示整除,不是按位或)。

并且a1要考虑8个值(1-8)。

a1 = 1时,很明显是可以的;

a1 = 2时,第一个肯定成立(因为是偶数),只要考虑(a1+1) | (n+1),也就是3能不能整除n+1,首先前k-1位都是1,所以只要考虑3|(k-1+3)就OK了;

a1 = 3时,a1+1 = 4, 很明显后缀是14的不能整除4,不成立;

a1 = 4时,同上;

a1 = 5时,a1+1 = 6,和判断3是一样的;

a1 = 6时,判断7|(n+1),我们总结知道只有当前面的k-1个1是6的倍数时,7|(n+1)才成立;

a1 = 7时,8不能整除118,不成立;

a1 = 8时,同上,不成立。

下面第一个是我的AC代码,另一个是题解的代码。

代码如下:

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;
typedef long long LL;

int main(){
    int n;  scanf("%d", &n);
    --n;
    if(!n)  printf("8\n");
    else if(n % 6 == 0)  printf("4\n");
    else if(n % 3 == 0)  printf("3\n");
    else  printf("1\n");
    return 0;
}

题解代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
    int k;
    scanf("%d", &k);
    if(k == 1) {
        printf("8\n");
        return 0;
    }
    int cnt = 1;
    if((k - 1) % 3 == 0) {
        cnt += 2;
        if((k - 1) % 6 == 0) {
            cnt ++;
        }
    }
    printf("%d\n", cnt);
    return 0;
}
时间: 2024-08-02 11:23:45

SGU 169 Numbers (找规律)的相关文章

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

URAL 2070 Interesting Numbers (找规律)

题意:在[L, R]之间求:x是个素数,因子个数是素数,同时满足两个条件,或者同时不满足两个条件的数的个数. 析:很明显所有的素数,因数都是2,是素数,所以我们只要算不是素数但因子是素数的数目就好,然后用总数减掉就好.打个表,找找规律,你会发现, 这些数除外的数都是素数的素数次方,然后就简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include &

Boxes - SGU 126(找规律)

题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一定是A == B,再继续往前推A/=2....所以判断拿球的过程中是否有2^k次方数,如果有就能放,没有肯定不能放,特殊的AB开始有为0的,直接模拟也可以,注意两个箱子大小交替不会超过两次,如果超过,一定不能交换成功,因为如果能摆放成功的话,最多交换一次. 代码如下: ===============

987654321 problem - SGU 107(找规律)

题目大意:求n位数的平方的后几位结果是987654321的个数是多少. 分析:刚看到这道题的时候怀疑过有没有这样的数,于是暴力跑了一下,发现还真有,9位的数有8个,如下: i=111111111, i*i=12345678987654321i=119357639, i*i=14246245987654321i=380642361, i*i=144888606987654321i=388888889, i*i=151234567987654321i=611111111, i*i=373456789

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

找规律/数位DP HDOJ 4722 Good Numbers

题目传送门 1 /* 2 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () 3 http://www.cnblogs.com/crazyapple/p/3315436.html 4 数位DP:http://blog.csdn.net/cyendra/article/details/11606209 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9

Humble Numbers【找规律】

Humble Numbers Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21

HDU - 4722 Good Numbers 【找规律 or 数位dp模板】

If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number. You are required to count the number of good numbers in the range from A to B, inclusive. InputThe first line has a number T (T <=

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4