結果

問題 No.220 世界のなんとか2
ユーザー tamiflu__shrinetamiflu__shrine
提出日時 2015-06-22 20:43:16
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,577 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 60,260 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 23:45:23
合計ジャッジ時間 1,365 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <math.h>
using namespace std;
int main(void){
    
    /*
        1 ~ 10^p から
            (A)3の倍数
        および
            (B)3の付く数の個数
        を求めるプログラム。
        
        ベン図で考えて (A) + (B) - (A)かつ(B) で求めることにした
    */
    
    // 入力
    unsigned long long p = 0;
    cin >> p;
    
    // 結果
    unsigned long long result1 = 0;
    unsigned long long result2 = 0;
    unsigned long long result3 = 0;
    
    // (A)3の倍数
    // p1=3, p2=33, p3=333, ... を一般化すると
    // A(p) = (unsigned long long)(powl(10, p) / 3)
    result1 = (unsigned long long)(powl(10, p) / 3);
    
    // (B)3の付く数
    // p1=1, p2=19, ... を一般化すると
    // A(1) = 1
    // A(p) = A(p-1) * 9 + powl(10, p-1)
    for (int i = 1, init = 1; i <= p; i++) {
     
        if (i == 1) {
            result2 = init;
        } else {
            result2 = init * 9 + (unsigned long long)powl(10, i - 1);
            init    = result2;
        }
    }
    
    // (A)かつ(B)
    // p1=1, p2=7, ... を一般化すると
    // A(1) = 1
    // A(p) = A(p) * 3 + (A(p-1) - 1) * 6 + (unsigned long long)(powl(10, p) / 3) + 1
    for (int i = 1, init = 1; i <= p; i++) {
     
        if (i == 1) {
            result3 = init;
        } else {
            result3 = init * 3 + (init - 1) * 6 + (unsigned long long)(powl(10, i-1) / 3) + 1;
            init    = result3;
        }
    }
    
    // 出力
    printf("%llu\n", result1 + result2 - result3);
}
0