SGU105 Div 3

SGU105 Div 3

题目大意

对于这样一个数列:1, 12, 123, 1234, ..., 12345678910, ...
输入N,输出该数列的前N项中有多少个数能被3整除

算法思路

由于N mod 3的结果等于N的数位和 mod 3的结果
该序列等价于:1, (1 + 2) % 3 = 0, (0 + 0) % 3 = 0, (0 + 1) % 3 = 1, (1 + 2) % 3 = 0, ...
故可以直接计算结果

时间复杂度: O(1)

代码

/** * Copyright (c) 2015 Authors. All rights reserved. * * FileName: 105.cpp * Author: Beiyu Li <[email protected]> * Date: 2015-05-21 */
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

int main()
{
        int n;
        scanf("%d", &n); --n;
        printf("%d\n", n / 3 * 2 + n % 3);

        return 0;
}

你可能感兴趣的:(数论,sgu)