Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

PS 부수기

Project Euler #205 - Dice Game 본문

Project Euler

Project Euler #205 - Dice Game

jyheo98 2021. 9. 14. 09:18

Naive solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int main() {
    IOS;
    ll ans=0;
    clock_t st, en;
    st=clock();
    for(int i1=1;i1<=4;i1++){
        for(int i2=1;i2<=4;i2++){
            for(int i3=1;i3<=4;i3++){
                for(int i4=1;i4<=4;i4++){
                    for(int i5=1;i5<=4;i5++){
                        for(int i6=1;i6<=4;i6++){
                            for(int i7=1;i7<=4;i7++){
                                for(int i8=1;i8<=4;i8++){
                                    for(int i9=1;i9<=4;i9++){
                                        for(int j1=1;j1<=6;j1++) {
                                            for(int j2=1;j2<=6;j2++) {
                                                for(int j3=1;j3<=6;j3++) {
                                                    for(int j4=1;j4<=6;j4++) {
                                                        for(int j5=1;j5<=6;j5++) {
                                                            for(int j6=1;j6<=6;j6++) {
                                                                if(i1+i2+i3+i4+i5+i6+i7+i8+i9>j1+j2+j3+j4+j5+j6)
                                                                    ans++;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    ll tot=4LL*4*4*4*4*4*4*4*4*6*6*6*6*6*6;
    cout << fixed << setprecision(10);
    cout << ans << "\n";
    cout << (long double)ans/tot << "\n";
    en=clock();
    cout<<(double)(en-st)/CLOCKS_PER_SEC<<'\n';
}
cs

전설의 15중 포문을 완성했다.

실행시간 : 42s

 

최적화 solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int main() {
    IOS;
    ll ans=0;
    clock_t st, en;
    st=clock();
    vector<ll> d4(37),d6(37);
    for(int i1=1;i1<=4;i1++){
        for(int i2=1;i2<=4;i2++){
            for(int i3=1;i3<=4;i3++){
                for(int i4=1;i4<=4;i4++){
                    for(int i5=1;i5<=4;i5++){
                        for(int i6=1;i6<=4;i6++){
                            for(int i7=1;i7<=4;i7++){
                                for(int i8=1;i8<=4;i8++){
                                    for(int i9=1;i9<=4;i9++){
                                        d4[i1+i2+i3+i4+i5+i6+i7+i8+i9]++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    for(int j1=1;j1<=6;j1++) {
        for(int j2=1;j2<=6;j2++) {
            for(int j3=1;j3<=6;j3++) {
                for(int j4=1;j4<=6;j4++) {
                    for(int j5=1;j5<=6;j5++) {
                        for(int j6=1;j6<=6;j6++) {
                            d6[j1+j2+j3+j4+j5+j6]++;
                        }
                    }
                }
            }
        }
    }
    for(int i=0 ; i<=36 ; i++) {
        for(int j=0 ; j<i ; j++) {
            ans+=d4[i]*d6[j];
        }
    }
    ll tot=4LL*4*4*4*4*4*4*4*4*6*6*6*6*6*6;
    cout << fixed << setprecision(10);
    cout << ans << "\n";
    cout << (long double)ans/tot << "\n";
    en=clock();
    cout<<(double)(en-st)/CLOCKS_PER_SEC<<'\n';
}
cs

주사위마다 가능한 숫자 수를 저장해주고, 후에 처리해줬다.

실행시간 0.003s

Comments