├── README.md ├── codes ├── c │ ├── Code_2_01_1.c │ ├── Code_2_01_2.c │ ├── Code_2_01_3.c │ ├── Code_2_01_4.c │ ├── Code_2_02_1.c │ ├── Code_2_02_2.c │ ├── Code_2_03_1.c │ ├── Code_2_04_1.c │ ├── Code_2_04_2.c │ ├── Code_2_04_3.c │ ├── Code_2_06_1.c │ ├── Code_3_01_1.c │ ├── Code_3_01_2.c │ ├── Code_3_01_3.c │ ├── Code_3_02_1.c │ ├── Code_3_02_2.c │ ├── Code_3_03_1.c │ ├── Code_3_04_1.c │ ├── Code_3_04_2.c │ ├── Code_3_05_1.c │ ├── Code_3_06_1.c │ ├── Code_3_06_2.c │ ├── Code_3_06_3.c │ ├── Code_3_06_4.c │ ├── Code_3_06_5.c │ ├── Code_3_06_6.c │ ├── Code_3_07_1.c │ ├── Code_3_07_2.c │ ├── Code_3_07_3.c │ ├── Code_3_08_1.c │ ├── Code_4_01_1.c │ ├── Code_4_02_1.c │ ├── Code_4_02_2.c │ ├── Code_4_03_1.c │ ├── Code_4_04_1.c │ ├── Code_4_05_1.c │ ├── Code_4_05_2.c │ ├── Code_4_05_2_stack.c │ ├── Code_4_05_3.c │ ├── Code_4_06_1.c │ ├── Code_4_06_2.c │ ├── Code_4_06_3.c │ ├── Code_4_06_4.c │ ├── Code_4_06_5.c │ ├── Code_4_06_6.c │ ├── Code_4_07_1.c │ ├── Code_5_02_1.c │ ├── Code_5_02_2.c │ ├── Code_5_03_1.c │ ├── Code_5_03_2.c │ ├── Code_5_04_1.c │ ├── Code_5_05_1.c │ ├── Code_5_05_2.c │ ├── Code_5_06_1.c │ ├── Code_5_07_1.c │ ├── Code_5_07_2.c │ ├── Code_5_09_1.c │ ├── Code_5_09_2.c │ ├── Code_5_10_1.c │ ├── Code_5_10_2.c │ ├── Code_5_10_3.c │ └── Code_5_10_4.c ├── cpp │ ├── Code_2_01_1.cpp │ ├── Code_2_01_2.cpp │ ├── Code_2_01_3.cpp │ ├── Code_2_01_4.cpp │ ├── Code_2_02_1.cpp │ ├── Code_2_02_2.cpp │ ├── Code_2_03_1.cpp │ ├── Code_2_04_1.cpp │ ├── Code_2_04_2.cpp │ ├── Code_2_04_3.cpp │ ├── Code_2_06_1.cpp │ ├── Code_3_01_1.cpp │ ├── Code_3_01_2.cpp │ ├── Code_3_01_3.cpp │ ├── Code_3_02_1.cpp │ ├── Code_3_02_2.cpp │ ├── Code_3_03_1.cpp │ ├── Code_3_04_1.cpp │ ├── Code_3_04_2.cpp │ ├── Code_3_05_1.cpp │ ├── Code_3_06_1.cpp │ ├── Code_3_06_2.cpp │ ├── Code_3_06_3.cpp │ ├── Code_3_06_4.cpp │ ├── Code_3_06_5.cpp │ ├── Code_3_06_6.cpp │ ├── Code_3_07_1.cpp │ ├── Code_3_07_2.cpp │ ├── Code_3_07_3.cpp │ ├── Code_3_08_1.cpp │ ├── Code_4_01_1.cpp │ ├── Code_4_02_1.cpp │ ├── Code_4_02_2.cpp │ ├── Code_4_03_1.cpp │ ├── Code_4_04_1.cpp │ ├── Code_4_05_1.cpp │ ├── Code_4_05_2.cpp │ ├── Code_4_05_2_stack.cpp │ ├── Code_4_05_3.cpp │ ├── Code_4_06_1.cpp │ ├── Code_4_06_2.cpp │ ├── Code_4_06_3.cpp │ ├── Code_4_06_4.cpp │ ├── Code_4_06_5.cpp │ ├── Code_4_06_6.cpp │ ├── Code_4_07_1.cpp │ ├── Code_5_02_1.cpp │ ├── Code_5_02_2.cpp │ ├── Code_5_03_1.cpp │ ├── Code_5_03_2.cpp │ ├── Code_5_04_1.cpp │ ├── Code_5_05_1.cpp │ ├── Code_5_05_2.cpp │ ├── Code_5_06_1.cpp │ ├── Code_5_07_1.cpp │ ├── Code_5_07_2.cpp │ ├── Code_5_09_1.cpp │ ├── Code_5_09_2.cpp │ ├── Code_5_10_1.cpp │ ├── Code_5_10_2.cpp │ ├── Code_5_10_3.cpp │ └── Code_5_10_4.cpp ├── java │ ├── Code_2_01_1.java │ ├── Code_2_01_2.java │ ├── Code_2_01_3.java │ ├── Code_2_01_4.java │ ├── Code_2_02_1.java │ ├── Code_2_02_2.java │ ├── Code_2_03_1.java │ ├── Code_2_04_1.java │ ├── Code_2_04_2.java │ ├── Code_2_04_3.java │ ├── Code_2_06_1.java │ ├── Code_3_01_1.java │ ├── Code_3_01_2.java │ ├── Code_3_01_3.java │ ├── Code_3_02_1.java │ ├── Code_3_02_2.java │ ├── Code_3_03_1.java │ ├── Code_3_04_1.java │ ├── Code_3_04_2.java │ ├── Code_3_05_1.java │ ├── Code_3_06_1.java │ ├── Code_3_06_2.java │ ├── Code_3_06_3.java │ ├── Code_3_06_4.java │ ├── Code_3_06_5.java │ ├── Code_3_06_6.java │ ├── Code_3_07_1.java │ ├── Code_3_07_2.java │ ├── Code_3_07_3.java │ ├── Code_3_08_1.java │ ├── Code_4_01_1.java │ ├── Code_4_02_1.java │ ├── Code_4_02_2.java │ ├── Code_4_03_1.java │ ├── Code_4_04_1.java │ ├── Code_4_05_1.java │ ├── Code_4_05_2.java │ ├── Code_4_05_2_stack.java │ ├── Code_4_05_3.java │ ├── Code_4_06_1.java │ ├── Code_4_06_2.java │ ├── Code_4_06_3.java │ ├── Code_4_06_4.java │ ├── Code_4_06_5.java │ ├── Code_4_06_6.java │ ├── Code_4_07_1.java │ ├── Code_5_02_1.java │ ├── Code_5_02_2.java │ ├── Code_5_03_1.java │ ├── Code_5_03_2.java │ ├── Code_5_04_1.java │ ├── Code_5_05_1.java │ ├── Code_5_05_2.java │ ├── Code_5_06_1.java │ ├── Code_5_07_1.java │ ├── Code_5_07_2.java │ ├── Code_5_09_1.java │ ├── Code_5_09_2.java │ ├── Code_5_10_1.java │ ├── Code_5_10_2.java │ ├── Code_5_10_3.java │ └── Code_5_10_4.java └── python │ ├── Code_2_01_1.py │ ├── Code_2_01_2.py │ ├── Code_2_01_3.py │ ├── Code_2_01_4.py │ ├── Code_2_02_1.py │ ├── Code_2_02_2.py │ ├── Code_2_03_1.py │ ├── Code_2_04_1.py │ ├── Code_2_04_2.py │ ├── Code_2_04_3.py │ ├── Code_2_06_1.py │ ├── Code_3_01_1.py │ ├── Code_3_01_2.py │ ├── Code_3_01_3.py │ ├── Code_3_02_1.py │ ├── Code_3_02_2.py │ ├── Code_3_03_1.py │ ├── Code_3_04_1.py │ ├── Code_3_04_2.py │ ├── Code_3_05_1.py │ ├── Code_3_06_1.py │ ├── Code_3_06_2.py │ ├── Code_3_06_3.py │ ├── Code_3_06_4.py │ ├── Code_3_06_5.py │ ├── Code_3_06_6.py │ ├── Code_3_07_1.py │ ├── Code_3_07_2.py │ ├── Code_3_07_3.py │ ├── Code_3_08_1.py │ ├── Code_4_01_1.py │ ├── Code_4_02_1.py │ ├── Code_4_02_2.py │ ├── Code_4_03_1.py │ ├── Code_4_04_1.py │ ├── Code_4_05_1.py │ ├── Code_4_05_2.py │ ├── Code_4_05_2_stack.py │ ├── Code_4_05_3.py │ ├── Code_4_06_1.py │ ├── Code_4_06_2.py │ ├── Code_4_06_3.py │ ├── Code_4_06_4.py │ ├── Code_4_06_4_extra.py │ ├── Code_4_06_5.py │ ├── Code_4_06_6.py │ ├── Code_4_07_1.py │ ├── Code_5_02_1.py │ ├── Code_5_02_2.py │ ├── Code_5_03_1.py │ ├── Code_5_03_2.py │ ├── Code_5_04_1.py │ ├── Code_5_05_1.py │ ├── Code_5_05_2.py │ ├── Code_5_06_1.py │ ├── Code_5_07_1.py │ ├── Code_5_07_2.py │ ├── Code_5_09_1.py │ ├── Code_5_09_2.py │ ├── Code_5_10_1.py │ ├── Code_5_10_2.py │ ├── Code_5_10_3.py │ ├── Code_5_10_3_extra.py │ └── Code_5_10_4.py ├── editorial ├── Editorial_All.pdf ├── chap2-1 │ ├── chap2-1.md │ ├── chap2-1.pdf │ ├── prob2-1-3.c │ ├── prob2-1-3.cpp │ ├── prob2-1-3.java │ └── prob2-1-3.py ├── chap2-2 │ ├── chap2-2.md │ ├── chap2-2.pdf │ ├── prob2-2-4.c │ ├── prob2-2-4.cpp │ ├── prob2-2-4.java │ └── prob2-2-4.py ├── chap2-3 │ ├── chap2-3.md │ └── chap2-3.pdf ├── chap2-4 │ ├── chap2-4.md │ └── chap2-4.pdf ├── chap2-5 │ ├── chap2-5.md │ ├── chap2-5.pdf │ ├── prob2-5-3.c │ ├── prob2-5-3.cpp │ ├── prob2-5-3.java │ ├── prob2-5-3.py │ ├── prob2-5-4.c │ ├── prob2-5-4.cpp │ ├── prob2-5-4.java │ └── prob2-5-4.py ├── chap3-1 │ ├── chap3-1.md │ ├── chap3-1.pdf │ ├── prob3-1-2.c │ ├── prob3-1-2.cpp │ ├── prob3-1-2.java │ └── prob3-1-2.py ├── chap3-2 │ ├── chap3-2.md │ ├── chap3-2.pdf │ ├── prob3-2-2.c │ ├── prob3-2-2.cpp │ ├── prob3-2-2.java │ ├── prob3-2-2.py │ ├── prob3-2-3.c │ ├── prob3-2-3.cpp │ ├── prob3-2-3.java │ └── prob3-2-3.py ├── chap3-3 │ ├── chap3-3.md │ ├── chap3-3.pdf │ ├── prob3-3-3.c │ ├── prob3-3-3.cpp │ ├── prob3-3-3.java │ ├── prob3-3-3.py │ ├── prob3-3-4.c │ ├── prob3-3-4.cpp │ ├── prob3-3-4.java │ ├── prob3-3-4.py │ ├── prob3-3-5.c │ ├── prob3-3-5.cpp │ ├── prob3-3-5.java │ ├── prob3-3-5.py │ ├── prob3-3-6.c │ ├── prob3-3-6.cpp │ ├── prob3-3-6.java │ └── prob3-3-6.py ├── chap3-4 │ ├── chap3-4.md │ ├── chap3-4.pdf │ ├── prob3-4-3.c │ ├── prob3-4-3.cpp │ ├── prob3-4-3.java │ ├── prob3-4-3.py │ ├── prob3-4-4.c │ ├── prob3-4-4.cpp │ ├── prob3-4-4.java │ └── prob3-4-4.py ├── chap3-5 │ ├── chap3-5.md │ ├── chap3-5.pdf │ ├── prob3-5-2.c │ ├── prob3-5-2.cpp │ ├── prob3-5-2.java │ └── prob3-5-2.py ├── chap3-6 │ ├── chap3-6.md │ ├── chap3-6.pdf │ ├── prob3-6-3.c │ ├── prob3-6-3.cpp │ ├── prob3-6-3.java │ └── prob3-6-3.py ├── chap3-7 │ ├── chap3-7.md │ ├── chap3-7.pdf │ ├── prob3-7-4.c │ ├── prob3-7-4.cpp │ ├── prob3-7-4.java │ ├── prob3-7-4.py │ ├── prob3-7-6.c │ ├── prob3-7-6.cpp │ ├── prob3-7-6.java │ └── prob3-7-6.py ├── chap4-1 │ ├── chap4-1.md │ ├── chap4-1.pdf │ ├── prob4-1-2.c │ ├── prob4-1-2.cpp │ ├── prob4-1-2.java │ ├── prob4-1-2.py │ ├── prob4-1-3.c │ ├── prob4-1-3.cpp │ ├── prob4-1-3.java │ ├── prob4-1-3.py │ ├── prob4-1-4.c │ ├── prob4-1-4.cpp │ ├── prob4-1-4.java │ ├── prob4-1-4.py │ ├── prob4-1-5.c │ ├── prob4-1-5.cpp │ ├── prob4-1-5.java │ └── prob4-1-5.py ├── chap4-2 │ ├── chap4-2.md │ ├── chap4-2.pdf │ ├── prob4-2-1.c │ ├── prob4-2-1.cpp │ ├── prob4-2-1.java │ ├── prob4-2-1.py │ ├── prob4-2-2.c │ ├── prob4-2-2.cpp │ ├── prob4-2-2.java │ └── prob4-2-2.py ├── chap4-3 │ ├── chap4-3.md │ ├── chap4-3.pdf │ ├── prob4-3-2.c │ ├── prob4-3-2.cpp │ ├── prob4-3-2.java │ ├── prob4-3-2.py │ ├── prob4-3-3.c │ ├── prob4-3-3.cpp │ ├── prob4-3-3.java │ └── prob4-3-3.py ├── chap4-4 │ ├── chap4-4.md │ ├── chap4-4.pdf │ ├── prob4-4-2.c │ ├── prob4-4-2.cpp │ ├── prob4-4-2.java │ ├── prob4-4-2.py │ ├── prob4-4-3.c │ ├── prob4-4-3.cpp │ ├── prob4-4-3.java │ ├── prob4-4-3.py │ ├── prob4-4-3_fast.py │ ├── prob4-4-4.c │ ├── prob4-4-4.cpp │ ├── prob4-4-4.java │ └── prob4-4-4.py ├── chap4-5 │ ├── chap4-5.md │ ├── chap4-5.pdf │ ├── prob4-5-5.c │ ├── prob4-5-5.cpp │ ├── prob4-5-5.java │ ├── prob4-5-5.py │ ├── prob4-5-6.c │ ├── prob4-5-6.cpp │ ├── prob4-5-6.java │ ├── prob4-5-6.py │ ├── prob4-5-7.c │ ├── prob4-5-7.cpp │ ├── prob4-5-7.java │ ├── prob4-5-7.py │ ├── prob4-5-8.cpp │ ├── prob4-5-8.java │ └── prob4-5-8.py ├── chap4-6 │ ├── chap4-6.md │ ├── chap4-6.pdf │ ├── prob4-6-2.c │ ├── prob4-6-2.cpp │ ├── prob4-6-2.java │ ├── prob4-6-2.py │ ├── prob4-6-3.c │ ├── prob4-6-3.cpp │ ├── prob4-6-3.java │ └── prob4-6-3.py ├── chap4-7 │ ├── chap4-7.md │ ├── chap4-7.pdf │ ├── prob4-7-2.c │ ├── prob4-7-2.cpp │ ├── prob4-7-2.java │ ├── prob4-7-2.py │ ├── prob4-7-3.c │ ├── prob4-7-3.cpp │ ├── prob4-7-3.java │ ├── prob4-7-3.py │ ├── prob4-7-4.c │ ├── prob4-7-4.cpp │ ├── prob4-7-4.java │ └── prob4-7-4.py ├── chap5-10 │ ├── chap5-10.md │ ├── chap5-10.pdf │ ├── prob5-10-2.c │ ├── prob5-10-2.cpp │ ├── prob5-10-2.java │ ├── prob5-10-2.py │ ├── prob5-10-3.c │ ├── prob5-10-3.cpp │ ├── prob5-10-3.java │ ├── prob5-10-3.py │ ├── prob5-10-5.c │ ├── prob5-10-5.cpp │ ├── prob5-10-5.java │ ├── prob5-10-5.py │ ├── prob5-10-6.c │ ├── prob5-10-6.cpp │ ├── prob5-10-6.java │ └── prob5-10-6.py ├── chap5-2 │ ├── chap5-2.md │ ├── chap5-2.pdf │ ├── prob5-2-2.c │ ├── prob5-2-2.cpp │ ├── prob5-2-2.java │ ├── prob5-2-2.py │ ├── prob5-2-3.c │ ├── prob5-2-3.cpp │ ├── prob5-2-3.java │ └── prob5-2-3.py ├── chap5-3 │ ├── chap5-3.md │ ├── chap5-3.pdf │ ├── prob5-3-1.c │ ├── prob5-3-1.cpp │ ├── prob5-3-1.java │ └── prob5-3-1.py ├── chap5-4 │ ├── chap5-4.md │ ├── chap5-4.pdf │ ├── prob5-4-2.c │ ├── prob5-4-2.cpp │ ├── prob5-4-2.java │ ├── prob5-4-2.py │ ├── prob5-4-5.c │ ├── prob5-4-5.cpp │ ├── prob5-4-5.java │ └── prob5-4-5.py ├── chap5-5 │ ├── chap5-5.md │ ├── chap5-5.pdf │ ├── prob5-5-2.c │ ├── prob5-5-2.cpp │ ├── prob5-5-2.java │ └── prob5-5-2.py ├── chap5-6 │ ├── chap5-6.md │ ├── chap5-6.pdf │ ├── prob5-6-2.c │ ├── prob5-6-2.cpp │ ├── prob5-6-2.java │ └── prob5-6-2.py ├── chap5-7 │ ├── chap5-7.md │ ├── chap5-7.pdf │ ├── prob5-7-3.c │ ├── prob5-7-3.cpp │ ├── prob5-7-3.java │ ├── prob5-7-3.py │ ├── prob5-7-4.c │ ├── prob5-7-4.cpp │ ├── prob5-7-4.java │ └── prob5-7-4.py ├── chap5-8 │ ├── chap5-8.md │ ├── chap5-8.pdf │ ├── prob5-8-2.c │ ├── prob5-8-2.cpp │ ├── prob5-8-2.java │ ├── prob5-8-2.py │ ├── prob5-8-3.cpp │ ├── prob5-8-3.java │ └── prob5-8-3.py ├── chap5-9 │ ├── chap5-9.md │ ├── chap5-9.pdf │ ├── prob5-9-1.c │ ├── prob5-9-1.cpp │ ├── prob5-9-1.java │ ├── prob5-9-1.py │ ├── prob5-9-2.c │ ├── prob5-9-2.cpp │ ├── prob5-9-2.java │ ├── prob5-9-2.py │ ├── prob5-9-3.c │ ├── prob5-9-3.cpp │ ├── prob5-9-3.java │ └── prob5-9-3.py ├── chap6-01_05 │ ├── chap6-01_05.md │ └── chap6-01_05.pdf ├── chap6-06_10 │ ├── chap6-06_10.md │ └── chap6-06_10.pdf ├── chap6-11_15 │ ├── chap6-11_15.md │ └── chap6-11_15.pdf ├── chap6-16_20 │ ├── chap6-16_20.md │ ├── chap6-16_20.pdf │ ├── prob6-16.c │ ├── prob6-16.cpp │ ├── prob6-16.java │ ├── prob6-16.py │ ├── prob6-17.c │ ├── prob6-17.cpp │ ├── prob6-17.java │ ├── prob6-17.py │ ├── prob6-18.c │ ├── prob6-18.cpp │ ├── prob6-18.java │ ├── prob6-18.py │ ├── prob6-19.c │ ├── prob6-19.cpp │ ├── prob6-19.java │ ├── prob6-19.py │ ├── prob6-20.c │ ├── prob6-20.cpp │ ├── prob6-20.java │ └── prob6-20.py ├── chap6-21_25 │ ├── chap6-21_25.md │ ├── chap6-21_25.pdf │ ├── prob6-21.c │ ├── prob6-21.cpp │ ├── prob6-21.java │ ├── prob6-21.py │ ├── prob6-22.c │ ├── prob6-22.cpp │ ├── prob6-22.java │ ├── prob6-22.py │ ├── prob6-23.c │ ├── prob6-23.cpp │ ├── prob6-23.java │ ├── prob6-23.py │ ├── prob6-24.c │ ├── prob6-24.cpp │ ├── prob6-24.java │ ├── prob6-24.py │ ├── prob6-25.c │ ├── prob6-25.cpp │ ├── prob6-25.java │ └── prob6-25.py ├── chap6-26_30 │ ├── chap6-26_30.md │ ├── chap6-26_30.pdf │ ├── prob6-26.c │ ├── prob6-26.cpp │ ├── prob6-26.java │ ├── prob6-26.py │ ├── prob6-27.c │ ├── prob6-27.cpp │ ├── prob6-27.java │ ├── prob6-27.py │ ├── prob6-28.c │ ├── prob6-28.cpp │ ├── prob6-28.java │ └── prob6-28.py └── explanation.md ├── errata.md └── fig ├── chap2-1-1.jpg ├── chap2-1-2.jpg ├── chap2-2-1.jpg ├── chap2-2-2.jpg ├── chap2-3-1.jpg ├── chap2-3-2.jpg ├── chap2-3-3.jpg ├── chap2-3-4.jpg ├── toppage-001.png ├── toppage-002.jpg ├── toppage-003.jpg └── toppage-003.png /codes/c/Code_2_01_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | printf("%d\n", 5 + N); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /codes/c/Code_2_01_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int A[4]; 5 | scanf("%d%d%d", &A[1], &A[2], &A[3]); 6 | printf("%d\n", A[1] + A[2] + A[3]); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /codes/c/Code_2_01_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, A[59]; 5 | int Answer = 0; 6 | 7 | scanf("%d", &N); 8 | for (int i = 1; i <= N; i++) { 9 | scanf("%d", &A[i]); 10 | Answer += A[i]; 11 | } 12 | printf("%d\n", Answer); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /codes/c/Code_2_01_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int N; 6 | scanf("%d", &N); 7 | char answer[100] = ""; // char 型の配列で文字列を表す 8 | while (N >= 1) { 9 | // N % 2 は N を 2 で割った余り(例:N = 13 の場合 1) 10 | // N / 2 は N を 2 で割った値の整数部分(例:N = 13 の場合 6) 11 | char next_answer[101]; 12 | if (N % 2 == 0) { 13 | // next_answer に「"0" と answer をつなげたもの」を代入 14 | sprintf(next_answer, "0%s", answer); 15 | } 16 | if (N % 2 == 1) { 17 | // next_answer に「"1" と answer をつなげたもの」を代入 18 | sprintf(next_answer, "1%s", answer); 19 | } 20 | strcpy(answer, next_answer); 21 | N = N / 2; 22 | } 23 | printf("%s\n", answer); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codes/c/Code_2_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d%d", &a, &b); // a と b を入力する 6 | printf("%d\n", a & b); // a AND b の値を出力する 7 | printf("%d\n", a | b); // a OR b の値を出力する 8 | printf("%d\n", a ^ b); // a XOR b の値を出力する 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /codes/c/Code_2_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int cnt = 1000; 4 | 5 | int func1() { 6 | return 2021; 7 | } 8 | 9 | int func2(int pos) { 10 | cnt += 1; 11 | return cnt + pos; 12 | } 13 | 14 | int main() { 15 | printf("%d\n", func1()); // 「2021」と出力 16 | printf("%d\n", func2(500)); // 「1501」と出力 17 | printf("%d\n", func2(500)); // 「1502」と出力 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /codes/c/Code_2_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | printf("%d\n", 2 * N + 3); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /codes/c/Code_2_04_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N, X, Y; 6 | scanf("%d%d%d", &N, &X, &Y); 7 | 8 | // 答えを求める 9 | int cnt = 0; 10 | int i; 11 | for (i = 1; i <= N; i++) { 12 | // mod の計算は 2.2 節参照 13 | if (i % X == 0 || i % Y == 0) { 14 | cnt += 1; 15 | } 16 | } 17 | 18 | // 出力 19 | printf("%d\n", cnt); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codes/c/Code_2_04_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N, S; 6 | scanf("%d%d", &N, &S); 7 | 8 | // 答えを求める 9 | int answer = 0; 10 | int i, j; 11 | for (i = 1; i <= N; i++) { 12 | for (j = 1; j <= N; j++) { 13 | if (i + j <= S) { 14 | answer += 1; 15 | } 16 | } 17 | } 18 | 19 | // 出力 20 | printf("%d\n", answer); 21 | } 22 | -------------------------------------------------------------------------------- /codes/c/Code_2_06_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long N, S, A[61]; 6 | scanf("%lld%lld", &N, &S); 7 | long long i, j; 8 | for (i = 1; i <= N; i++) { 9 | scanf("%lld", &A[i]); 10 | } 11 | 12 | // 全パターンを探索:(1LL << N) は 2 の N 乗 13 | for (i = 0; i < (1LL << N); i++) { 14 | long long sum = 0; 15 | for (j = 1; j <= N; j++) { 16 | // (i & (1LL << (j-1))) != 0LL の場合、i の 2 進法表記の下から j 桁目が 1 17 | // (1LL << (j-1)) は C++ では「2 の j-1 乗」を意味します 18 | if ((i & (1LL << (j - 1))) != 0LL) { 19 | sum += A[j]; 20 | } 21 | } 22 | if (sum == S) { 23 | printf("Yes\n"); 24 | return 0; 25 | } 26 | } 27 | printf("No\n"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codes/c/Code_3_01_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 5 | bool isprime(long long N) { 6 | long long i; 7 | for (i = 2; i <= N - 1; i++) { 8 | if (N % i == 0) { 9 | // N が i で割り切れた場合、この時点で素数ではないと分かる 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | int main() { 17 | long long N; 18 | scanf("%lld", &N); 19 | if (isprime(N) == true) { 20 | printf("prime\n"); 21 | } 22 | else { 23 | printf("not prime\n"); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /codes/c/Code_3_01_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 5 | bool isprime(long long N) { 6 | long long i; 7 | for (i = 2; i * i <= N; i++) { 8 | if (N % i == 0) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | 15 | int main() { 16 | long long N; 17 | scanf("%lld", &N); 18 | if (isprime(N) == true) { 19 | printf("prime\n"); 20 | } 21 | else { 22 | printf("not prime\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codes/c/Code_3_01_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long N; 5 | scanf("%lld", &N); 6 | long long i; 7 | for (i = 1; i * i <= N; i++) { 8 | if (N % i != 0) continue; 9 | printf("%lld\n", i); // i を約数に追加 10 | if (i != N / i) { 11 | printf("%lld\n", N / i); // i ≠ N/i のとき、N/i も約数に追加 12 | } 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /codes/c/Code_3_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 正の整数 A と B の最大公約数を返す関数 4 | // GCD は Greatest Common Divisor(最大公約数)の略 5 | long long GCD(long long A, long long B) { 6 | long long answer = 0; 7 | long long i; 8 | for (i = 1; i <= A && i <= B; i++) { 9 | if (A % i == 0 && B % i == 0) { 10 | answer = i; 11 | } 12 | } 13 | return answer; 14 | } 15 | 16 | int main() { 17 | long long A, B; 18 | scanf("%lld%lld", &A, &B); 19 | printf("%lld\n", GCD(A, B)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codes/c/Code_3_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // GCD は Greatest Common Divisor(最大公約数)の略 4 | long long GCD(long long A, long long B) { 5 | while (A >= 1 && B >= 1) { 6 | if (A < B) B = B % A; // A < B の場合、大きい方 B を書き換える 7 | else A = A % B; // A >= B の場合、大きい方 A を書き換える 8 | } 9 | if (A >= 1) return A; 10 | return B; 11 | } 12 | 13 | int main() { 14 | long long A, B; 15 | scanf("%lld%lld", &A, &B); 16 | printf("%lld\n", GCD(A, B)); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /codes/c/Code_3_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N, A[109]; 6 | scanf("%d", &N); 7 | for (int i = 1; i <= N; i++) { 8 | scanf("%d", &A[i]); 9 | } 10 | 11 | // 5 つのカードの番号 (i, j, k, l, m) を全探索 12 | int answer = 0; 13 | int i, j, k, l, m; 14 | for (i = 1; i <= N; i++) { 15 | for (j = i + 1; j <= N; j++) { 16 | for (k = j + 1; k <= N; k++) { 17 | for (l = k + 1; l <= N; l++) { 18 | for (m = l + 1; m <= N; m++) { 19 | if (A[i] + A[j] + A[k] + A[l] + A[m] == 1000) { 20 | answer += 1; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | } 27 | 28 | // 出力 29 | printf("%d\n", answer); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codes/c/Code_3_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, B[100009], R[100009]; 4 | 5 | int main() { 6 | // 入力 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%d", &B[i]); 11 | } 12 | for (i = 1; i <= N; i++) { 13 | scanf("%d", &R[i]); 14 | } 15 | 16 | // 答えの計算 → 答えの出力 17 | double blue = 0.0, red = 0.0; 18 | for (i = 1; i <= N; i++) { 19 | blue += 1.0 * B[i] / N; 20 | red += 1.0 * R[i] / N; 21 | } 22 | printf("%.12lf\n", blue + red); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codes/c/Code_3_04_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N, P[59], Q[59]; 6 | scanf("%d", &N); 7 | int i; 8 | for (i = 1; i <= N; i++) { 9 | scanf("%d%d", &P[i], &Q[i]); 10 | } 11 | 12 | // 答えの計算 → 答えの出力 13 | double answer = 0.0; 14 | for (i = 1; i <= N; i++) { 15 | answer += 1.0 * Q[i] / P[i]; 16 | } 17 | printf("%.12lf\n", answer); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /codes/c/Code_3_05_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int N = 10000; // N は試行回数(適宜変更する) 6 | int M = 0; 7 | int i; 8 | for (i = 1; i <= N; i++) { 9 | double px = rand() / (double)RAND_MAX; // 0 以上 1 以下の乱数(ランダムな数)を生成 10 | double py = rand() / (double)RAND_MAX; // 0 以上 1 以下の乱数(ランダムな数)を生成 11 | // 原点からの距離は sqrt(px * px + py * py) 12 | // これが 1 以下であれば良いので、条件は「px * px + py * py <= 1」 13 | if (px * px + py * py <= 1.0) M += 1; 14 | } 15 | printf("%.12lf\n", 4.0 * M / N); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /codes/c/Code_3_06_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[200009]; 4 | 5 | int main() { 6 | // 入力 7 | scanf("%d", &N); 8 | int i, j; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%d", &A[i]); 11 | } 12 | 13 | // 選択ソート 14 | for (i = 1; i <= N - 1; i++) { 15 | int min_position = i; 16 | int min_value = A[i]; 17 | for (j = i + 1; j <= N; j++) { 18 | if (A[j] < min_value) { 19 | min_position = j; // min_position は最小値のインデックス(1~N) 20 | min_value = A[j]; // min_value は現時点での最小値 21 | } 22 | } 23 | // 次の 3 行で A[i] と A[min_position] を交換 24 | int temp = A[i]; 25 | A[i] = A[min_position]; 26 | A[min_position] = temp; 27 | } 28 | 29 | // 出力 30 | for (int i = 1; i <= N; i++) { 31 | printf("%d\n", A[i]); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /codes/c/Code_3_06_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int func(int N) { 4 | if (N == 1) { 5 | return 1; // このような場合分けすべきケースを「ベースケース」といいます 6 | } 7 | return func(N - 1) * N; 8 | } 9 | 10 | int main() { 11 | int N; 12 | scanf("%d", &N); 13 | printf("%d\n", func(N)); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /codes/c/Code_3_06_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long GCD(long long A, long long B) { 4 | if (B == 0) { 5 | return A; // ベースケース 6 | } 7 | return GCD(B, A % B); 8 | } 9 | 10 | int main() { 11 | long long A, B; 12 | scanf("%lld%lld", &A, &B); 13 | printf("%lld\n", GCD(A, B)); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /codes/c/Code_3_06_5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[109]; 4 | 5 | int solve(int l, int r) { 6 | if (r - l == 1) return A[l]; 7 | int m = (l + r) / 2; // 区間 [l, r) の中央で分割する 8 | int s1 = solve(l, m); // s1 は A[l]+...+A[m-1] の合計値となる 9 | int s2 = solve(m, r); // s2 は A[m]+...+A[r-1] の合計値となる 10 | return s1 + s2; 11 | } 12 | 13 | int main() { 14 | // 入力 15 | scanf("%d", &N); 16 | int i; 17 | for (i = 1; i <= N; i++) { 18 | scanf("%d", &A[i]); 19 | } 20 | 21 | // 再帰呼び出し → 答えの出力 22 | int answer = solve(1, N + 1); 23 | printf("%d\n", answer); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codes/c/Code_3_06_6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int func(int N) { 4 | return func(N - 1) * N; 5 | } 6 | 7 | int main() { 8 | int N; 9 | scanf("%d", &N); 10 | printf("%d\n", func(N)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /codes/c/Code_3_07_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N, H[100009], dp[100009]; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%d", &N); 9 | int i; 10 | for (i = 1; i <= N; i++) { 11 | scanf("%d", &H[i]); 12 | } 13 | 14 | // 動的計画法 → 答えの出力 15 | for (i = 1; i <= N; i++) { 16 | if (i == 1) dp[i] = 0; 17 | if (i == 2) dp[i] = abs(H[i - 1] - H[i]); 18 | if (i >= 3) { 19 | int v1 = dp[i - 1] + abs(H[i - 1] - H[i]); // 1 個前の足場からジャンプするとき 20 | int v2 = dp[i - 2] + abs(H[i - 2] - H[i]); // 2 個前の足場からジャンプするとき 21 | dp[i] = (v1 < v2 ? v1 : v2); // v1 と v2 のうち小さい方の値 22 | } 23 | } 24 | printf("%d\n", dp[N]); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /codes/c/Code_3_07_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, dp[54]; 4 | 5 | int main() { 6 | // 入力 7 | scanf("%d", &N); 8 | 9 | // 動的計画法 → 答えの出力 10 | int i; 11 | for (i = 0; i <= N; i++) { 12 | if (i <= 1) dp[i] = 1; 13 | else dp[i] = dp[i - 1] + dp[i - 2]; 14 | } 15 | printf("%d\n", dp[N]); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /codes/c/Code_4_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[100009], B[100009]; 4 | int Q, L[100009], R[100009]; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%d%d", &N, &Q); 9 | int i, j; 10 | for (i = 1; i <= N; i++) { 11 | scanf("%d", &A[i]); 12 | } 13 | for (j = 1; j <= Q; j++) { 14 | scanf("%d%d", &L[j], &R[j]); 15 | } 16 | 17 | // 累積和を求める 18 | B[0] = 0; 19 | for (i = 1; i <= N; i++) { 20 | B[i] = B[i - 1] + A[i]; 21 | } 22 | 23 | // 答えの出力 24 | for (j = 1; j <= Q; j++) { 25 | printf("%d\n", B[R[j]] - B[L[j] - 1]); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /codes/c/Code_4_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, B[100009]; 4 | int Q, L[100009], R[100009], X[100009]; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%d%d", &N, &Q); 9 | int i; 10 | for (i = 1; i <= Q; i++) { 11 | scanf("%d%d%d", &L[i], &R[i], &X[i]); 12 | } 13 | 14 | // 階差の計算 15 | for (i = 1; i <= Q; i++) { 16 | B[L[i]] += X[i]; 17 | B[R[i] + 1] -= X[i]; 18 | } 19 | 20 | // 答えの出力 21 | for (i = 2; i <= N; i++) { 22 | if (B[i] > 0) printf("<"); 23 | if (B[i] == 0) printf("="); 24 | if (B[i] < 0) printf(">"); 25 | } 26 | printf("\n"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /codes/c/Code_4_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double r = 2.0; // √2 を求めたいから 5 | double a = 2.0; // 初期値を適当に 2.0 にセットする 6 | int i; 7 | for (i = 1; i <= 5; i++) { 8 | // 点 (a, f(a)) の x 座標と y 座標を求める 9 | double zahyou_x = a; 10 | double zahyou_y = a * a; 11 | 12 | // 接線の傾きを求める [y = (sessen_a)x + sessen_b とする] 13 | double sessen_a = 2.0 * zahyou_x; 14 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 15 | 16 | // 次の a の値 next_a を求める 17 | double next_a = (r - sessen_b) / sessen_a; 18 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codes/c/Code_4_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N; bool prime[100000009]; 5 | 6 | int main() { 7 | // 入力 → 配列の初期化 8 | scanf("%d", &N); 9 | int i, x; 10 | for (i = 2; i <= N; i++) { 11 | prime[i] = true; 12 | } 13 | 14 | // エラトステネスのふるい 15 | for (i = 2; i * i <= N; i++) { 16 | if (prime[i] == true) { 17 | for (x = 2 * i; x <= N; x += i) { 18 | prime[x] = false; 19 | } 20 | } 21 | } 22 | 23 | // N 以下の素数を小さい順に出力 24 | for (i = 2; i <= N; i++) { 25 | if (prime[i] == true) { 26 | printf("%d\n", i); 27 | } 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codes/c/Code_4_06_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, a[10000009]; 4 | 5 | int main() { 6 | scanf("%d", &N); 7 | a[1] = 1; a[2] = 1; 8 | int i; 9 | for (i = 3; i <= N; i++) { 10 | a[i] = a[i - 1] + a[i - 2]; 11 | } 12 | printf("%d\n", a[N] % 1000000007); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /codes/c/Code_4_06_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, a[10000009]; 4 | 5 | int main() { 6 | scanf("%d", &N); 7 | a[1] = 1; a[2] = 1; 8 | int i; 9 | for (i = 3; i <= N; i++) { 10 | a[i] = (a[i - 1] + a[i - 2]) % 1000000007; 11 | } 12 | printf("%d\n", a[N] % 1000000007); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /codes/c/Code_4_06_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long a, b; 5 | scanf("%lld%lld", &a, &b); 6 | const long long MOD = 1000000007; 7 | long long answer = 1; // a の 0 乗は 1 なので、Answer=1 に初期化しておく 8 | long long i; 9 | for (i = 1; i <= b; i++) { 10 | answer = (answer * a) % MOD; 11 | } 12 | printf("%lld\n", answer); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /codes/c/Code_4_06_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long modpow(long long a, long long b, long long m) { 4 | // 繰り返し二乗法(p は a^1, a^2, a^4, a^8, ... といった値をとる) 5 | long long p = a, answer = 1; 6 | int i; 7 | for (i = 0; i < 30; i++) { 8 | if ((b & (1 << i)) != 0) { 9 | answer = (answer * p) % m; 10 | } 11 | p = (p * p) % m; 12 | } 13 | return answer; 14 | } 15 | 16 | int main() { 17 | const long long MOD = 1000000007; 18 | long long a, b; 19 | scanf("%lld%lld", &a, &b); 20 | printf("%lld\n", modpow(a, b, MOD)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codes/c/Code_5_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long N; 5 | scanf("%lld", &N); 6 | if (N % 4 == 1) printf("2\n"); 7 | if (N % 4 == 2) printf("4\n"); 8 | if (N % 4 == 3) printf("8\n"); 9 | if (N % 4 == 0) printf("6\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /codes/c/Code_5_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long N; 5 | scanf("%lld", &N); 6 | if (N % 4 == 0) { 7 | printf("Second\n"); // 後手必勝 8 | } 9 | else { 10 | printf("First\n"); // 先手必勝 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /codes/c/Code_5_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | if (N % 2 == 0) { 7 | printf("Yes\n"); 8 | } 9 | else { 10 | printf("No\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /codes/c/Code_5_03_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N, K, A[59]; 6 | scanf("%d%d", &N, &K); 7 | int i; 8 | for (i = 1; i <= N; i++) { 9 | scanf("%d", &A[i]); 10 | } 11 | 12 | // 数列の要素の総和 sum を求める 13 | int sum = 0; 14 | for (i = 1; i <= N; i++) { 15 | sum += A[i]; 16 | } 17 | 18 | // 答えの出力 19 | if (sum % 2 != K % 2) { 20 | printf("No\n"); 21 | } 22 | else if (sum > K) { 23 | printf("No\n"); 24 | } 25 | else { 26 | printf("Yes\n"); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codes/c/Code_5_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int minimum(int a, int b) { 5 | return (a < b ? a : b); 6 | } 7 | 8 | int maximum(int a, int b) { 9 | return (a > b ? a : b); 10 | } 11 | 12 | int main() { 13 | // 入力 14 | int N, K; 15 | scanf("%d%d", &N, &K); 16 | 17 | // 事象 B の個数 yojishou を数える 18 | long long yojishou = 0; 19 | int a, b, c; 20 | for (a = 1; a <= N; a++) { 21 | int l = maximum(a - (K - 1), 1); // b, c の探索範囲の下限 l 22 | int r = minimum(a + (K - 1), N); // b, c の探索範囲の上限 r 23 | for (b = l; b <= r; b++) { 24 | for (c = l; c <= r; c++) { 25 | if (abs(b - c) <= K - 1) yojishou += 1; 26 | } 27 | } 28 | } 29 | 30 | // 答えの出力 31 | printf("%lld\n", (long long)N * N * N - yojishou); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /codes/c/Code_5_05_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long a, b, c, d; 6 | scanf("%lld%lld%lld%lld", &a, &b, &c, &d); 7 | 8 | // 答えを求める(answer は最初あり得ない値に設定) 9 | long long answer = -(1LL << 62); 10 | if (answer < a * c) answer = a * c; 11 | if (answer < a * d) answer = a * d; 12 | if (answer < b * c) answer = b * c; 13 | if (answer < b * d) answer = b * d; 14 | 15 | // 出力 16 | printf("%lld\n", answer); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /codes/c/Code_5_06_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 小問題 t を解く関数 5 | bool shou_mondai(int A, int B, int t) { 6 | int cl = (A + t - 1) / t; // A÷t の小数点以下切り上げ 7 | int cr = B / t; // B÷t の小数点以下切り捨て 8 | if (cr - cl >= 1) return true; 9 | return false; 10 | } 11 | 12 | int main() { 13 | int A, B; 14 | scanf("%d%d", &A, &B); 15 | int answer = 0; 16 | int i; 17 | for (i = 1; i <= B; i++) { 18 | if (shou_mondai(A, B, i) == true) { 19 | answer = i; 20 | } 21 | } 22 | printf("%d\n", answer); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codes/c/Code_5_07_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N; long long A[200009]; 4 | 5 | int main() { 6 | // 入力 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%lld", &A[i]); 11 | } 12 | 13 | // 答えを求める → 答えの出力 14 | long long answer = 0; 15 | for (i = 1; i <= N; i++) { 16 | answer += A[i] * (-N + 2LL * i - 1LL); 17 | } 18 | printf("%lld\n", answer); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /codes/c/Code_5_09_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long N; 6 | scanf("%lld", &N); 7 | 8 | // 支払い方のシミュレーション 9 | long long answer = 0; 10 | while (N >= 10000) { 11 | N -= 10000; 12 | answer += 1; 13 | } 14 | while (N >= 5000) { 15 | N -= 5000; 16 | answer += 1; 17 | } 18 | while (N >= 1) { 19 | N -= 1000; 20 | answer += 1; 21 | } 22 | 23 | // 答えの出力 24 | printf("%lld\n", answer); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /codes/c/Code_5_10_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | long long a, b, c; 6 | scanf("%lld%lld%lld", &a, &b, &c); 7 | if (sqrt(a) + sqrt(b) < sqrt(c)) { 8 | printf("Yes\n"); 9 | } 10 | else { 11 | printf("No\n"); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /codes/c/Code_5_10_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long a, b, c; 5 | scanf("%lld%lld%lld", &a, &b, &c); 6 | if (c - a - b < 0LL) { 7 | printf("No\n"); 8 | } 9 | else if (4LL * a * b < (c - a - b) * (c - a - b)) { 10 | printf("Yes\n"); 11 | } 12 | else { 13 | printf("No\n"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /codes/c/Code_5_10_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N; long long X, Y; 6 | scanf("%d%lld%lld", &N, &X, &Y); 7 | 8 | // 4 つの整数 (a, b, c, d) の全探索 → 答えの出力 9 | int a, b, c, d; 10 | for (a = 1; a <= N; a++) { 11 | for (b = a; b <= N; b++) { 12 | for (c = b; c <= N; c++) { 13 | for (d = c; d <= N; d++) { 14 | if (a + b + c + d == X && 1LL * a * b * c * d == Y) { 15 | printf("Yes\n"); 16 | return 0; // プログラムの実行を終了させる 17 | } 18 | } 19 | } 20 | } 21 | } 22 | printf("No\n"); // 1 つも見つからなかったら No 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codes/c/Code_5_10_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N; char S[500009]; 6 | scanf("%d%s", &N, S); 7 | 8 | // '(' の数 - ')' の数を depth とする 9 | // 途中で depth が負になったらこの時点で No 10 | int depth = 0; 11 | for (int i = 0; i < N; i++) { 12 | if (S[i] == '(') depth += 1; 13 | if (S[i] == ')') depth -= 1; 14 | if (depth < 0) { 15 | printf("No\n"); 16 | return 0; 17 | } 18 | } 19 | 20 | // 最後、depth = 0 ['(' と ')' の数が同じ] かどうかで場合分け 21 | if (depth == 0) { 22 | printf("Yes\n"); 23 | } 24 | else { 25 | printf("No\n"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /codes/cpp/Code_2_01_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; // 入力部分 7 | cout << 5 + N << endl; // 出力部分 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /codes/cpp/Code_2_01_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int A[4]; 6 | cin >> A[1] >> A[2] >> A[3]; // 入力部分 7 | cout << A[1] + A[2] + A[3] << endl; // 出力部分 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /codes/cpp/Code_2_01_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N, A[59]; 6 | int Answer = 0; 7 | cin >> N; 8 | for (int i = 1; i <= N; i++) { 9 | cin >> A[i]; 10 | Answer += A[i]; 11 | } 12 | cout << Answer << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /codes/cpp/Code_2_01_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_2_01_4.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_2_02_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_2_02_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_2_02_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_2_02_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_2_03_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_2_03_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_2_04_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; 7 | cout << 2 * N + 3 << endl; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /codes/cpp/Code_2_04_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_2_04_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_2_04_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_2_04_3.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_2_06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, S, A[61]; 5 | 6 | int main() { 7 | cin >> N >> S; 8 | for (int i = 1; i <= N; i++) cin >> A[i]; 9 | 10 | // 全パターンを探索:(1LL << N) は 2 の N 乗 11 | for (long long i = 0; i < (1LL << N); i++) { 12 | long long sum = 0; 13 | for (int j = 1; j <= N; j++) { 14 | // (i & (1LL << (j-1))) != 0LL の場合、i の 2 進法表記の下から j 桁目が 1 15 | // (1LL << (j-1)) は C++ では「2 の j-1 乗」を意味します 16 | if ((i & (1LL << (j-1))) != 0LL) sum += A[j]; 17 | } 18 | if (sum == S) { cout << "Yes" << endl; return 0; } 19 | } 20 | cout << "No" << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codes/cpp/Code_3_01_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_01_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_01_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_01_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_01_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_01_3.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_02_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_02_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_02_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_02_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_03_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_03_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_04_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_04_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_04_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_04_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_05_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_05_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_06_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_06_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_06_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[200009]; 5 | 6 | int main() { 7 | // 入力 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) cin >> A[i]; 10 | 11 | // 選択ソート 12 | for (int i = 1; i <= N - 1; i++) { 13 | int Min = i, Min_Value = A[i]; 14 | for (int j = i + 1; j <= N; j++) { 15 | if (A[j] < Min_Value) { 16 | Min = j; // Min は最小値のインデックス(1~N) 17 | Min_Value = A[j]; // Min_Value は現時点での最小値 18 | } 19 | } 20 | swap(A[i], A[Min]); 21 | } 22 | 23 | // 出力 24 | for (int i = 1; i <= N; i++) cout << A[i] << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /codes/cpp/Code_3_06_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int func(int N) { 5 | if (N == 1) return 1; // このような場合分けすべきケースを「ベースケース」といいます 6 | return func(N - 1) * N; 7 | } 8 | 9 | int main() { 10 | int N; 11 | cin >> N; 12 | cout << func(N) << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /codes/cpp/Code_3_06_4.cpp: -------------------------------------------------------------------------------- 1 | long long GCD(long long A, long long B) { 2 | if (B == 0) return A; // ベースケース 3 | return GCD(B, A % B); 4 | } -------------------------------------------------------------------------------- /codes/cpp/Code_3_06_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[109]; 5 | 6 | int solve(int l, int r) { 7 | if (r - l == 1) return A[l]; 8 | int m = (l + r) / 2; // 区間 [l, r) の中央で分割する 9 | int s1 = solve(l, m); // s1 は A[l]+...+A[m-1] の合計値となる 10 | int s2 = solve(m, r); // s2 は A[m]+...+A[r-1] の合計値となる 11 | return s1 + s2; 12 | } 13 | 14 | int main() { 15 | // 入力 16 | cin >> N; 17 | for (int i = 1; i <= N; i++) cin >> A[i]; 18 | 19 | // 再帰呼び出し → 答えの出力 20 | int Answer = solve(1, N + 1); 21 | cout << Answer << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /codes/cpp/Code_3_06_6.cpp: -------------------------------------------------------------------------------- 1 | int func(int N) { 2 | return func(N - 1) * N; 3 | } -------------------------------------------------------------------------------- /codes/cpp/Code_3_07_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_07_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_07_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_07_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_07_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_07_3.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_3_08_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_3_08_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_02_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_02_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_02_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, B[100009]; 5 | int Q, L[100009], R[100009], X[100009]; 6 | 7 | int main() { 8 | // 入力・階差の計算 9 | cin >> N >> Q; 10 | for (int i = 1; i <= Q; i++) { 11 | cin >> L[i] >> R[i] >> X[i]; 12 | B[L[i]] += X[i]; 13 | B[R[i] + 1] -= X[i]; 14 | } 15 | 16 | // 答えの出力 17 | for (int i = 2; i <= N; i++) { 18 | if (B[i] > 0) cout << "<"; 19 | if (B[i] == 0) cout << "="; 20 | if (B[i] < 0) cout << ">"; 21 | } 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codes/cpp/Code_4_03_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_03_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_04_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_04_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_05_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_05_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_05_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_05_3.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, a[10000009]; 5 | 6 | int main() { 7 | cin >> N; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) a[i] = a[i - 1] + a[i - 2]; 10 | cout << a[N] % 1000000007 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /codes/cpp/Code_4_06_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, a[10000009]; 5 | 6 | int main() { 7 | cin >> N; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) a[i] = (a[i - 1] + a[i - 2]) % 1000000007; 10 | cout << a[N] % 1000000007 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /codes/cpp/Code_4_06_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_06_3.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_06_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_06_4.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_06_5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_06_5.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_06_6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_06_6.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_4_07_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_4_07_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_02_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long N; 6 | cin >> N; 7 | if (N % 4 == 1) cout << "2" << endl; 8 | if (N % 4 == 2) cout << "4" << endl; 9 | if (N % 4 == 3) cout << "8" << endl; 10 | if (N % 4 == 0) cout << "6" << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /codes/cpp/Code_5_02_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_02_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_03_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; 7 | if (N % 2 == 0) cout << "Yes" << endl; 8 | else cout << "No" << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /codes/cpp/Code_5_03_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_03_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_04_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_04_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_05_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long a, b, c, d; 7 | cin >> a >> b >> c >> d; 8 | cout << max({ a * c, a * d, b * c, b * d }) << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /codes/cpp/Code_5_05_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_05_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_06_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_06_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_07_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_07_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_07_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_07_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_09_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_09_1.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_09_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_09_2.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_10_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long a, b, c; 7 | cin >> a >> b >> c; 8 | if (sqrt(a) + sqrt(b) < sqrt(c)) cout << "Yes" << endl; 9 | else cout << "No" << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /codes/cpp/Code_5_10_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long a, b, c; 6 | cin >> a >> b >> c; 7 | if (c - a - b < 0LL) cout << "No" << endl; 8 | else if (4LL * a * b < (c - a - b) * (c - a - b)) cout << "Yes" << endl; 9 | else cout << "No" << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /codes/cpp/Code_5_10_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_10_3.cpp -------------------------------------------------------------------------------- /codes/cpp/Code_5_10_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/codes/cpp/Code_5_10_4.cpp -------------------------------------------------------------------------------- /codes/java/Code_2_01_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); // 入力部分 7 | System.out.println(5 + N); // 出力部分 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /codes/java/Code_2_01_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int[] A = new int[4]; 7 | // 入力部分 8 | A[1] = sc.nextInt(); 9 | A[2] = sc.nextInt(); 10 | A[3] = sc.nextInt(); 11 | // 出力部分 12 | System.out.println(A[1] + A[2] + A[3]); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /codes/java/Code_2_01_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int[] A = new int[N + 1]; 8 | int answer = 0; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextInt(); 11 | answer += A[i]; 12 | } 13 | System.out.println(answer); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /codes/java/Code_2_01_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); // 入力部分 7 | String answer = ""; // String は文字列型 8 | while (N >= 1) { 9 | // N % 2 は N を 2 で割った余り(例:N = 13 の場合 1) 10 | // N / 2 は N を 2 で割った値の整数部分(例:N = 13 の場合 6) 11 | if (N % 2 == 0) { 12 | answer = "0" + answer; 13 | } 14 | if (N % 2 == 1) { 15 | answer = "1" + answer; 16 | } 17 | N = N / 2; 18 | } 19 | System.out.println(answer); // 出力部分 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /codes/java/Code_2_02_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_02_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int a = sc.nextInt(); // a を入力する 7 | int b = sc.nextInt(); // b を入力する 8 | System.out.println(a & b); // a AND b の値を出力する 9 | System.out.println(a | b); // a OR b の値を出力する 10 | System.out.println(a ^ b); // a XOR b の値を出力する 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /codes/java/Code_2_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_03_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println(func1()); // 「2021」と出力 7 | System.out.println(func2(500)); // 「1501」と出力 8 | System.out.println(func2(500)); // 「1502」と出力 9 | } 10 | static int cnt = 1000; 11 | static int func1() { 12 | return 2021; 13 | } 14 | static int func2(int pos) { 15 | cnt += 1; 16 | return cnt + pos; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /codes/java/Code_2_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_04_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | System.out.println(2 * N + 3); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /codes/java/Code_2_04_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_04_2 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int X = sc.nextInt(); 9 | int Y = sc.nextInt(); 10 | 11 | // 答えを求める 12 | int cnt = 0; 13 | for (int i = 1; i <= N; i++) { 14 | // mod の計算は 2.2 節参照 15 | if (i % X == 0 || i % Y == 0) { 16 | cnt++; 17 | } 18 | } 19 | 20 | // 出力 21 | System.out.println(cnt); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /codes/java/Code_2_04_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_04_3 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int S = sc.nextInt(); 9 | 10 | // 答えを求める 11 | int cnt = 0; 12 | for (int i = 1; i <= N; i++) { 13 | for (int j = 1; j <= N; j++) { 14 | if (i + j <= S) { 15 | cnt++; 16 | } 17 | } 18 | } 19 | 20 | // 出力 21 | System.out.println(cnt); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /codes/java/Code_3_01_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_01_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | boolean answer = isPrime(N); 8 | if (answer == true) { 9 | System.out.println("prime"); 10 | } 11 | else { 12 | System.out.println("not prime"); 13 | } 14 | } 15 | static boolean isPrime(long N) { 16 | // 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 17 | for (long i = 2; i <= N - 1; i++) { 18 | if (N % i == 0) { 19 | return false; // N が i で割り切れた場合、この時点で素数ではないと分かる 20 | } 21 | } 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /codes/java/Code_3_01_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_01_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | boolean answer = isPrime(N); 8 | if (answer == true) { 9 | System.out.println("prime"); 10 | } 11 | else { 12 | System.out.println("not prime"); 13 | } 14 | } 15 | static boolean isPrime(long N) { 16 | // 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 17 | for (long i = 2; i * i <= N; i++) { 18 | if (N % i == 0) { 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /codes/java/Code_3_01_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_01_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | for (long i = 1; i * i <= N; i++) { 8 | if (N % i == 0) { 9 | System.out.println(i); // i を約数に追加 10 | if (i != N / i) { 11 | System.out.println(N / i); // i ≠ N/i のとき、N/i も約数に追加 12 | } 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /codes/java/Code_3_02_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_02_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long A = sc.nextLong(); 7 | long B = sc.nextLong(); 8 | System.out.println(GCD(A, B)); 9 | } 10 | static long GCD(long A, long B) { 11 | // 正の整数 A と B の最大公約数を返す関数 12 | // GCD は Greatest Common Divisor(最大公約数)の略 13 | long ans = 0; 14 | for (long i = 1; i <= A && i <= B; i++) { 15 | if (A % i == 0 && B % i == 0) { 16 | ans = i; 17 | } 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /codes/java/Code_3_02_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_02_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long A = sc.nextLong(); 7 | long B = sc.nextLong(); 8 | System.out.println(GCD(A, B)); 9 | } 10 | static long GCD(long A, long B) { 11 | // 正の整数 A と B の最大公約数を返す関数 12 | // GCD は Greatest Common Divisor(最大公約数)の略 13 | while (A >= 1 && B >= 1) { 14 | if (A < B) { 15 | B %= A; // A < B の場合、大きい方 B を書き換える 16 | } 17 | else { 18 | A %= B; // A >= B の場合、大きい方 A を書き換える 19 | } 20 | } 21 | if (A >= 1) { 22 | return A; 23 | } 24 | return B; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /codes/java/Code_3_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_04_1 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] B = new int[N + 1]; 9 | int[] R = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | B[i] = sc.nextInt(); 12 | } 13 | for (int i = 1; i <= N; i++) { 14 | R[i] = sc.nextInt(); 15 | } 16 | 17 | // 答えの計算・出力 18 | double blue = 0.0, red = 0.0; 19 | for (int i = 1; i <= N; i++) { 20 | blue += (double)B[i] / N; 21 | red += (double)R[i] / N; 22 | } 23 | System.out.format("%.12f\n", blue + red); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /codes/java/Code_3_04_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_04_2 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] P = new int[N + 1]; 9 | int[] Q = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | P[i] = sc.nextInt(); 12 | Q[i] = sc.nextInt(); 13 | } 14 | 15 | // 答えの計算・出力 16 | double answer = 0.0; 17 | for (int i = 1; i <= N; i++) { 18 | answer += (double)Q[i] / P[i]; 19 | } 20 | System.out.format("%.12f\n", answer); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /codes/java/Code_3_05_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_05_1 { 4 | public static void main(String[] args) { 5 | Random rd = new Random(); 6 | int N = 10000; // N は試行回数(適宜変更する) 7 | int M = 0; 8 | for (int i = 1; i <= N; i++) { 9 | double px = rd.nextDouble(); // 0 以上 1 未満の乱数を生成(C++ のコード 3.5.1 では 0 以上 1 以下の乱数を生成しているが、ほとんど同じ) 10 | double py = rd.nextDouble(); // 0 以上 1 未満の乱数を生成(C++ のコード 3.5.1 では 0 以上 1 以下の乱数を生成しているが、ほとんど同じ) 11 | // 原点からの距離は sqrt(px * px + py * py) 12 | // これが 1 以下であれば良いので、条件は「px * px + py * py <= 1」(「sqrt(px * px + py * py) <= 1」と同値) 13 | if (px * px + py * py <= 1.0) { 14 | M++; 15 | } 16 | } 17 | System.out.format("%.12f\n", 4.0 * M / N); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /codes/java/Code_3_06_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_1 { 4 | public static void main(String[] args) { 5 | // 入力(たとえば N = 5, A = [3, 1, 4, 1, 5] を入力したとする) 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i - 1] = sc.nextInt(); 11 | } 12 | 13 | // 配列 A 全体をソートする 14 | Arrays.sort(A); 15 | 16 | // 出力(1, 1, 3, 4, 5 の順に出力される) 17 | for (int i = 1; i <= N; i++) { 18 | System.out.println(A[i - 1]); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /codes/java/Code_3_06_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | System.out.println(func(N)); 8 | } 9 | static int func(int N) { 10 | if (N == 1) { 11 | return 1; // このような場合分けすべきケースを「ベースケース」といいます 12 | } 13 | return func(N - 1) * N; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /codes/java/Code_3_06_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long A = sc.nextLong(); 7 | long B = sc.nextLong(); 8 | System.out.println(GCD(A, B)); 9 | } 10 | static long GCD(long A, long B) { 11 | if (B == 0) { 12 | return A; // ベースケース 13 | } 14 | return GCD(B, A % B); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /codes/java/Code_3_06_5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_5 { 4 | static int N; 5 | static int[] A; 6 | public static void main(String[] args) { 7 | // 入力 8 | Scanner sc = new Scanner(System.in); 9 | N = sc.nextInt(); 10 | A = new int[N + 1]; 11 | for (int i = 1; i <= N; i++) { 12 | A[i] = sc.nextInt(); 13 | } 14 | 15 | // 再帰呼び出し → 答えの出力 16 | int answer = solve(1, N + 1); 17 | System.out.println(answer); 18 | } 19 | static int solve(int l, int r) { 20 | if (r - l == 1) { 21 | return A[l]; 22 | } 23 | int m = (l + r) / 2; // 区間 [l, r) の中央で分割する 24 | int s1 = solve(l, m); // s1 は A[l] + ... + A[m-1] の合計値となる 25 | int s2 = solve(m, r); // s2 は A[m] + ... + A[r-1] の合計値となる 26 | return s1 + s2; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /codes/java/Code_3_06_6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_6 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int answer = func(N); 8 | System.out.println(answer); 9 | } 10 | static int func(int N) { 11 | // func(N) → func(N-1) → ... → func(0) → func(-1) → func(-2) → ... と無限に呼び出されるので、プログラムが正常に動作しない 12 | return func(N - 1) * N; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /codes/java/Code_3_07_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_07_2 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | 9 | // 動的計画法 10 | int[] dp = new int[N + 1]; 11 | for (int i = 0; i <= N; i++) { 12 | if (i <= 1) { 13 | dp[i] = 1; 14 | } 15 | else { 16 | dp[i] = dp[i - 1] + dp[i - 2]; 17 | } 18 | } 19 | 20 | // 答えの出力 21 | System.out.println(dp[N]); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /codes/java/Code_4_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_03_1 { 4 | public static void main(String[] args) { 5 | double r = 2.0; // √2 を求めたいから 6 | double a = 2.0; // 初期値を適当に 2.0 にセットする 7 | for (int i = 1; i <= 5; i++) { 8 | // 点 (a, f(a)) の x 座標と y 座標を求める 9 | double zahyou_x = a; 10 | double zahyou_y = a * a; 11 | 12 | // 接線の式 y = sessen_a * x + sessen_b を求める 13 | double sessen_a = 2.0 * zahyou_x; 14 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 15 | 16 | // 次の a の値 next_a を求める 17 | double next_a = (r - sessen_b) / sessen_a; 18 | System.out.format("Step #%d: a = %.12f -> %.12f\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /codes/java/Code_4_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_04_1 { 4 | public static void main(String[] args) { 5 | // 入力 → 配列 prime の初期化 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | boolean[] prime = new boolean[N + 1]; 9 | for (int i = 2; i <= N; i++) { 10 | prime[i] = true; 11 | } 12 | 13 | // エラトステネスのふるい 14 | for (int i = 2; i * i <= N; i++) { 15 | if (prime[i] == true) { 16 | for (int x = 2 * i; x <= N; x += i) { 17 | prime[x] = false; 18 | } 19 | } 20 | } 21 | 22 | // N 以下の素数を小さい順に出力 23 | for (int i = 2; i <= N; i++) { 24 | if (prime[i] == true) { 25 | System.out.println(i); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /codes/java/Code_4_06_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int[] a = new int[N + 1]; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) { 10 | a[i] = a[i - 1] + a[i - 2]; 11 | } 12 | System.out.println(a[N] % 1000000007); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /codes/java/Code_4_06_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int[] a = new int[N + 1]; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) { 10 | a[i] = (a[i - 1] + a[i - 2]) % 1000000007; 11 | } 12 | System.out.println(a[N] % 1000000007); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /codes/java/Code_4_06_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | final long MOD = 1000000007; 7 | long a = sc.nextLong(); 8 | long b = sc.nextLong(); 9 | long answer = 1; // a の 0 乗は 1 なので、answer = 1 に初期化しておく 10 | for (long i = 1; i <= b; i++) { 11 | answer = (answer * a) % MOD; 12 | } 13 | System.out.println(answer); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /codes/java/Code_4_06_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | final long MOD = 1000000007; 7 | long a = sc.nextLong(); 8 | long b = sc.nextLong(); 9 | long answer = modpow(a, b, MOD); 10 | System.out.println(answer); 11 | } 12 | static long modpow(long a, long b, long m) { 13 | // 繰り返し二乗法(p は a^1, a^2, a^4, a^8, ... といった値をとる) 14 | long p = a, answer = 1; 15 | for (int i = 0; i < 30; i++) { 16 | if ((b & (1 << i)) != 0) { 17 | answer = (answer * p) % m; 18 | } 19 | p = (p * p) % m; 20 | } 21 | return answer; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /codes/java/Code_5_02_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_02_1 { 4 | public static void main(String[] args) { 5 | // 注意:以下のプログラムは N >= 1 で正しく動作します。N = 0 では答えは「1」となるので、また別の場合分けをする必要があります。 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | if (N % 4 == 1) { 9 | System.out.println(2); 10 | } 11 | if (N % 4 == 2) { 12 | System.out.println(4); 13 | } 14 | if (N % 4 == 3) { 15 | System.out.println(8); 16 | } 17 | if (N % 4 == 0) { 18 | System.out.println(6); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /codes/java/Code_5_02_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_02_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | if (N % 4 == 0) { 8 | System.out.println("Second"); // 後手必勝 9 | } 10 | else { 11 | System.out.println("First"); // 先手必勝 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /codes/java/Code_5_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_03_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | if (N % 2 == 0) { 8 | System.out.println("Yes"); 9 | } 10 | else { 11 | System.out.println("No"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /codes/java/Code_5_03_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_03_2 { 4 | public static void main(String[] args) { 5 | // 入力 → 数列の要素の総和 sum を求める 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int K = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | A[i] = sc.nextInt(); 12 | } 13 | int sum = 0; 14 | for (int i = 1; i <= N; i++) { 15 | sum += A[i]; 16 | } 17 | 18 | // 答えの出力 19 | if (sum % 2 != K % 2) { 20 | System.out.println("No"); 21 | } 22 | else if (sum > K) { 23 | System.out.println("No"); 24 | } 25 | else { 26 | System.out.println("Yes"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /codes/java/Code_5_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_04_1 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int K = sc.nextInt(); 9 | // 事象 B の個数 yojishou を数える 10 | long yojishou = 0; 11 | for (int a = 1; a <= N; a++) { 12 | int l = Math.max(1, a - (K - 1)); // b, c の探索範囲の下限 l 13 | int r = Math.min(N, a + (K - 1)); // b, c の探索範囲の上限 r 14 | for (int b = l; b <= r; b++) { 15 | for (int c = l; c <= r; c++) { 16 | if (Math.abs(b - c) <= K - 1) { 17 | yojishou += 1; 18 | } 19 | } 20 | } 21 | } 22 | // 答えの出力 23 | long answer = (long)N * N * N - yojishou; 24 | System.out.println(answer); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /codes/java/Code_5_05_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_05_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long a = sc.nextLong(); 7 | long b = sc.nextLong(); 8 | long c = sc.nextLong(); 9 | long d = sc.nextLong(); 10 | long answer = Collections.max(Arrays.asList(a * c, a * d, b * c, b * d)); 11 | System.out.println(answer); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /codes/java/Code_5_06_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_06_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int A = sc.nextInt(); 7 | int B = sc.nextInt(); 8 | int answer = 0; 9 | for (int i = 1; i <= B; i++) { 10 | if (shouMondai(A, B, i) == true) { 11 | answer = i; 12 | } 13 | } 14 | System.out.println(answer); 15 | } 16 | static boolean shouMondai(int A, int B, int t) { 17 | // 小問題 t を解く関数 18 | int cl = (A + t - 1) / t; // A÷t の小数点以下切り上げ 19 | int cr = B / t; // B÷t の小数点以下切り捨て 20 | return (cr - cl >= 1); // cr - cl >= 1 ならば true、そうでなければ false を返す 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /codes/java/Code_5_07_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_07_1 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N + 1]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextInt(); 11 | } 12 | // 答えを求める 13 | long answer = 0; 14 | for (int i = 1; i <= N; i++) { 15 | answer += (long)A[i] * (-N + 2 * i - 1); 16 | } 17 | 18 | // 答えの出力 19 | System.out.println(answer); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /codes/java/Code_5_09_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_09_1 { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 支払い方のシミュレーション 10 | long answer = 0; 11 | while (N >= 10000) { 12 | N -= 10000; // 10000 円以上の間、10000 円札を使い続ける 13 | answer += 1; 14 | } 15 | while (N >= 5000) { 16 | N -= 5000; // 5000 円以上の間、5000 円札を使い続ける 17 | answer += 1; 18 | } 19 | while (N >= 1) { 20 | N -= 1000; // 残った金額を 1000 円札で支払う 21 | answer += 1; 22 | } 23 | 24 | // 答えの出力 25 | System.out.println(answer); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /codes/java/Code_5_10_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_10_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long a = sc.nextLong(); 7 | long b = sc.nextLong(); 8 | long c = sc.nextLong(); 9 | if (Math.sqrt(a) + Math.sqrt(b) < Math.sqrt(c)) { 10 | System.out.println("Yes"); 11 | } 12 | else { 13 | System.out.println("No"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /codes/java/Code_5_10_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_10_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long a = sc.nextLong(); 7 | long b = sc.nextLong(); 8 | long c = sc.nextLong(); 9 | if (c - a - b < 0) { 10 | System.out.println("No"); 11 | } 12 | else if (4 * a * b < (c - a - b) * (c - a - b)) { 13 | System.out.println("Yes"); 14 | } 15 | else { 16 | System.out.println("No"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /codes/python/Code_2_01_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | print(5 + N) -------------------------------------------------------------------------------- /codes/python/Code_2_01_2.py: -------------------------------------------------------------------------------- 1 | A = list(map(int, input().split())) 2 | print(A[0] + A[1] + A[2]) -------------------------------------------------------------------------------- /codes/python/Code_2_01_3.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | A = list(map(int, input().split())) 3 | Answer = 0 4 | 5 | for item in A: 6 | Answer += item 7 | 8 | print(Answer) -------------------------------------------------------------------------------- /codes/python/Code_2_01_4.py: -------------------------------------------------------------------------------- 1 | N = int(input()) # 入力部分 2 | answer = "" 3 | 4 | while N >= 1: 5 | # N % 2 は N を 2 で割った余り(例:N = 13 の場合 1) 6 | # N // 2 は N を 2 で割った値の整数部分(例:N = 13 の場合 6) 7 | if N % 2 == 0: 8 | answer = "0" + answer 9 | if N % 2 == 1: 10 | answer = "1" + answer 11 | N = N // 2 12 | 13 | print(answer) # 出力部分 14 | -------------------------------------------------------------------------------- /codes/python/Code_2_02_1.py: -------------------------------------------------------------------------------- 1 | import math # math.sqrt 関数を使うためには、math モジュールを import する必要がある 2 | 3 | # 四則演算 4 | print(869 + 120) # 989 と出力 5 | print(869 - 120) # 749 と出力 6 | print(869 * 120) # 104280 と出力 7 | print(869 // 120) # 7 と出力(Python 3 では、"//" は割り算の商を求める一方、"/" は小数部分も求めることに注意) 8 | 9 | # 剰余 (mod) 10 | print(8 % 5) # 3 と出力 11 | print(869 % 120) # 29 と出力 12 | 13 | # 絶対値 (abs) 14 | print(abs(-45)) # 45 と出力 15 | print(abs(15)) # 15 と出力 16 | 17 | # 累乗 18 | print(10 ** 2) # 100 と出力 19 | print(3 ** 4) # 81 と出力 20 | 21 | # 平方根 (sqrt) 22 | print("%.5f" % math.sqrt(4.0)) # 2.00000 と出力 23 | print("%.5f" % math.sqrt(2.0)) # 1.41421 と出力 24 | -------------------------------------------------------------------------------- /codes/python/Code_2_02_2.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) # a と b を入力する 2 | print(a & b) # a AND b の値を出力する 3 | print(a | b) # a OR b の値を出力する 4 | print(a ^ b) # a XOR b の値を出力する 5 | -------------------------------------------------------------------------------- /codes/python/Code_2_03_1.py: -------------------------------------------------------------------------------- 1 | cnt = 1000 2 | 3 | def func1(): 4 | return 2021 5 | 6 | def func2(pos): 7 | global cnt # グローバル変数 cnt を関数の内部で使う 8 | cnt += 1 9 | return cnt + pos 10 | 11 | print(func1()) # 「2021」と出力 12 | print(func2(500)) # 「1501」と出力 13 | print(func2(500)) # 「1502」と出力 14 | -------------------------------------------------------------------------------- /codes/python/Code_2_04_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | print(2 * N + 3) 3 | -------------------------------------------------------------------------------- /codes/python/Code_2_04_2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, X, Y = map(int, input().split()) 3 | 4 | # 答えを求める 5 | cnt = 0 6 | for i in range(1, N + 1): 7 | # mod の計算は 2.2 節参照 8 | if i % X == 0 or i % Y == 0: 9 | cnt += 1 10 | 11 | # 出力 12 | print(cnt) 13 | -------------------------------------------------------------------------------- /codes/python/Code_2_04_3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, S = map(int, input().split()) 3 | 4 | # 答えを求める 5 | answer = 0 6 | for i in range(1, N + 1): 7 | for j in range(1, N + 1): 8 | if i + j <= S: 9 | answer += 1 10 | 11 | # 出力 12 | print(answer) 13 | -------------------------------------------------------------------------------- /codes/python/Code_2_06_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, S = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | 5 | # 全パターンを探索:(1 << N) は 2 の N 乗 6 | answer = "No" 7 | for i in range(0, 1 << N): 8 | partsum = 0 9 | for j in range(0, N): 10 | # (i & (1 << j)) != 0LL の場合、i の 2 進法表記の下から j+1 桁目が 1 11 | # (1 << j) は Python では「2 の j 乗」を意味します 12 | if (i & (1 << j)) != 0: 13 | partsum += A[j] 14 | if partsum == S: 15 | answer = "Yes" 16 | break 17 | 18 | # 出力 19 | print(answer) 20 | -------------------------------------------------------------------------------- /codes/python/Code_3_01_1.py: -------------------------------------------------------------------------------- 1 | # 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 2 | def isprime(N): 3 | for i in range(2, N): 4 | if N % i == 0: 5 | return False # N が i で割り切れた場合、この時点で素数ではないと分かる 6 | return True 7 | 8 | N = int(input()) 9 | if isprime(N): 10 | print("prime") 11 | else: 12 | print("not prime") 13 | -------------------------------------------------------------------------------- /codes/python/Code_3_01_2.py: -------------------------------------------------------------------------------- 1 | # 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 2 | def isprime(N): 3 | LIMIT = int(N ** 0.5) 4 | for i in range(2, LIMIT + 1): 5 | if N % i == 0: 6 | return False 7 | return True 8 | 9 | N = int(input()) 10 | if isprime(N): 11 | print("prime") 12 | else: 13 | print("not prime") 14 | -------------------------------------------------------------------------------- /codes/python/Code_3_01_3.py: -------------------------------------------------------------------------------- 1 | # このプログラムは、コード 3.1.3 とは異なり、約数を小さい順に出力するプログラムとなっています。 2 | 3 | # 入力 4 | N = int(input()) 5 | 6 | # すべての約数を求め、配列 divisors に入れる 7 | LIMIT = int(N ** 0.5) 8 | divisors = [] 9 | for i in range(1, LIMIT + 1): 10 | if N % i == 0: 11 | divisors.append(i) 12 | if i != N // i: 13 | divisors.append(N // i) 14 | 15 | # 小さい順に並べ替え → 出力 16 | # sort は小さい順に並べ替える関数です(3.6.1 項で扱います) 17 | divisors.sort() 18 | for i in divisors: 19 | print(i) 20 | -------------------------------------------------------------------------------- /codes/python/Code_3_02_1.py: -------------------------------------------------------------------------------- 1 | # 正の整数 A と B の最大公約数を返す関数 2 | # GCD は Greatest Common Divisor(最大公約数)の略 3 | def GCD(A, B): 4 | answer = 0 5 | for i in range(1, min(A, B) + 1): 6 | if A % i == 0 and B % i == 0: 7 | answer = i 8 | return answer 9 | 10 | A, B = map(int, input().split()) 11 | print(GCD(A, B)) 12 | -------------------------------------------------------------------------------- /codes/python/Code_3_02_2.py: -------------------------------------------------------------------------------- 1 | # 正の整数 A と B の最大公約数を返す関数 2 | # GCD は Greatest Common Divisor(最大公約数)の略 3 | def GCD(A, B): 4 | while A >= 1 and B >= 1: 5 | if A < B: 6 | B = B % A # A < B の場合、大きい方 B を書き換える 7 | else: 8 | A = A % B # A >= B の場合、大きい方 A を書き換える 9 | if A >= 1: 10 | return A 11 | return B 12 | 13 | A, B = map(int, input().split()) 14 | print(GCD(A, B)) 15 | -------------------------------------------------------------------------------- /codes/python/Code_3_03_1.py: -------------------------------------------------------------------------------- 1 | # 注意 2 | # Python で提出すると、N = 100 では実行に 10 秒程度かかり、TLE(実行時間制限オーバー)になります。 3 | # 一方、同じプログラムを PyPy3 で提出すると、実行に 0.5 秒程度しかかからず、AC(正解)することができます。 4 | 5 | # 入力 6 | N = int(input()) 7 | A = list(map(int, input().split())) 8 | 9 | # 5 つのカードの番号 (i, j, k, l, m) を全探索 10 | answer = 0 11 | for i in range(0, N): 12 | for j in range(i + 1, N): 13 | for k in range(j + 1, N): 14 | for l in range(k + 1, N): 15 | for m in range(l + 1, N): 16 | if A[i] + A[j] + A[k] + A[l] + A[m] == 1000: 17 | answer += 1 18 | 19 | # 出力 20 | print(answer) 21 | -------------------------------------------------------------------------------- /codes/python/Code_3_04_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | B = list(map(int, input().split())) 4 | R = list(map(int, input().split())) 5 | 6 | # 答えの計算(Python 3 では B[i] / N, R[i] / N は小数まで計算されることに注意) 7 | blue = 0.0 8 | red = 0.0 9 | for i in range(N): 10 | blue += B[i] / N 11 | red += R[i] / N 12 | 13 | # 出力 14 | print("%.12f" % (blue + red)) 15 | -------------------------------------------------------------------------------- /codes/python/Code_3_04_2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | P = [ None ] * N 4 | Q = [ None ] * N 5 | for i in range(N): 6 | P[i], Q[i] = map(int, input().split()) 7 | 8 | # 答えの計算(Python 3 では Q[i] / P[i] は小数まで計算されることに注意) 9 | answer = 0 10 | for i in range(N): 11 | answer += Q[i] / P[i] 12 | 13 | # 出力 14 | print("%.12f" % answer) 15 | -------------------------------------------------------------------------------- /codes/python/Code_3_05_1.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 10000 # N は試行回数(適宜変更する) 4 | M = 0 5 | for i in range(N): 6 | px = random.random() # 0 以上 1 未満の乱数を生成(C++ のコード 3.5.1 では 0 以上 1 以下の乱数を生成しているが、ほとんど同じ) 7 | py = random.random() # 0 以上 1 未満の乱数を生成(C++ のコード 3.5.1 では 0 以上 1 以下の乱数を生成しているが、ほとんど同じ) 8 | # 原点からの距離は sqrt(px * px + py * py) 9 | # これが 1 以下であれば良いので、条件は「px * px + py * py <= 1」(「sqrt(px * px + py * py) <= 1」と同値) 10 | if px * px + py * py <= 1.0: 11 | M += 1 12 | 13 | print("%.12f" % (4 * M / N)) 14 | -------------------------------------------------------------------------------- /codes/python/Code_3_06_1.py: -------------------------------------------------------------------------------- 1 | # 入力(たとえば N = 5, A = [3, 1, 4, 1, 5] を入力したとする) 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 配列 A 全体をソートする 6 | A.sort() 7 | 8 | # 出力(1, 1, 3, 4, 5 の順に出力される) 9 | for i in range(N): 10 | print(A[i]) 11 | -------------------------------------------------------------------------------- /codes/python/Code_3_06_2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 選択ソート 6 | for i in range(N - 1): 7 | min_position = i 8 | min_value = A[i] 9 | for j in range(i + 1, N): 10 | if A[j] < min_value: 11 | min_position = j # min_position は最小値のインデックス(0 ~ N-1) 12 | min_value = A[j] # min_value は現時点での最小値 13 | # A[i] と A[min_position] を交換 14 | A[i], A[min_position] = A[min_position], A[i] 15 | 16 | # 出力 17 | for i in range(N): 18 | print(A[i]) 19 | -------------------------------------------------------------------------------- /codes/python/Code_3_06_3.py: -------------------------------------------------------------------------------- 1 | def func(N): 2 | if N == 1: 3 | return 1 # このような場合分けすべきケースを「ベースケース」といいます 4 | return func(N - 1) * N 5 | 6 | N = int(input()) 7 | print(func(N)) 8 | 9 | # Python では、呼び出せる再帰関数の深さに上限が設定されており、デフォルトでは 1000 などの深さに設定されています。 10 | # この上限は、sys.getrecursionlimit() を呼び出すことで取得できます。 11 | # 一方、sys.setrecursionlimit(depth) を呼び出すことで、再帰呼び出しの深さ depth の上限を変えることができます。 12 | # (これらの機能を使うためには、最初に import sys と書く必要があります) 13 | -------------------------------------------------------------------------------- /codes/python/Code_3_06_4.py: -------------------------------------------------------------------------------- 1 | def GCD(A, B): 2 | if B == 0: 3 | return A # ベースケース 4 | return GCD(B, A % B) 5 | 6 | A, B = map(int, input().split()) 7 | print(GCD(A, B)) 8 | -------------------------------------------------------------------------------- /codes/python/Code_3_06_5.py: -------------------------------------------------------------------------------- 1 | def solve(l, r, A): 2 | if r - l == 1: 3 | return A[l] 4 | m = (l + r) // 2 # 区間 [l, r) の中央で分割する 5 | s1 = solve(l, m, A) # s1 は A[l] + ... + A[m-1] の合計値となる 6 | s2 = solve(m, r, A) # s2 は A[m] + ... + A[r-1] の合計値となる 7 | return s1 + s2 8 | 9 | # 入力 10 | N = int(input()) 11 | A = list(map(int, input().split())) 12 | 13 | # 再帰呼び出し → 答えの出力 14 | answer = solve(0, N, A) 15 | print(answer) 16 | -------------------------------------------------------------------------------- /codes/python/Code_3_06_6.py: -------------------------------------------------------------------------------- 1 | def func(N): 2 | # func(N) → func(N-1) → ... → func(0) → func(-1) → func(-2) → ... と無限に呼び出されるので、プログラムが正常に動作しない 3 | return func(N - 1) * N 4 | 5 | N = int(input()) 6 | print(func(N)) 7 | -------------------------------------------------------------------------------- /codes/python/Code_3_07_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | H = list(map(int, input().split())) 4 | 5 | # 動的計画法 6 | dp = [ None ] * N 7 | dp[0] = 0 8 | for i in range(1, N): 9 | if i == 1: 10 | dp[i] = abs(H[i - 1] - H[i]) 11 | if i >= 2: 12 | v1 = dp[i - 1] + abs(H[i - 1] - H[i]) # 1 個前の足場からジャンプするとき 13 | v2 = dp[i - 2] + abs(H[i - 2] - H[i]) # 2 個前の足場からジャンプするとき 14 | dp[i] = min(v1, v2) 15 | 16 | # 答えの出力 17 | print(dp[N - 1]) 18 | -------------------------------------------------------------------------------- /codes/python/Code_3_07_2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 動的計画法 5 | dp = [ None ] * (N + 1) 6 | for i in range(N + 1): 7 | if i <= 1: 8 | dp[i] = 1 9 | else: 10 | dp[i] = dp[i - 1] + dp[i - 2] 11 | 12 | # 答えの出力 13 | print(dp[N]) 14 | -------------------------------------------------------------------------------- /codes/python/Code_3_08_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, X = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | 5 | # 配列のソート 6 | A.sort() 7 | 8 | # 二分探索 9 | answer = "No" 10 | left, right = 0, N - 1 11 | while left <= right: 12 | mid = (left + right) // 2 13 | if A[mid] == X: 14 | answer = "Yes" 15 | break 16 | if A[mid] > X: 17 | # 探索範囲を前半部分に絞る 18 | right = mid - 1 19 | if A[mid] < X: 20 | # 探索範囲を後半部分に絞る 21 | left = mid + 1 22 | 23 | # 答えの出力 24 | print(answer) 25 | -------------------------------------------------------------------------------- /codes/python/Code_4_02_1.py: -------------------------------------------------------------------------------- 1 | N, Q = map(int, input().split()) 2 | A = list(map(int, input().split())) 3 | L = [ None ] * Q 4 | R = [ None ] * Q 5 | for j in range(Q): 6 | L[j], R[j] = map(int, input().split()) 7 | 8 | B = [ None ] * (N + 1) 9 | B[0] = 0 10 | for i in range(N): 11 | B[i + 1] = B[i] + A[i] 12 | 13 | for j in range(Q): 14 | print(B[R[j]] - B[L[j] - 1]) 15 | -------------------------------------------------------------------------------- /codes/python/Code_4_02_2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, Q = map(int, input().split()) 3 | L = [ None ] * Q 4 | R = [ None ] * Q 5 | X = [ None ] * Q 6 | for i in range(Q): 7 | L[i], R[i], X[i] = map(int, input().split()) 8 | 9 | # 階差の計算 10 | B = [ 0 ] * (N + 2) 11 | for i in range(Q): 12 | B[L[i]] += X[i] 13 | B[R[i] + 1] -= X[i] 14 | 15 | # 答えを計算して出力 16 | answer = "" 17 | for i in range(2, N + 1): 18 | if B[i] > 0: 19 | answer += "<" 20 | if B[i] == 0: 21 | answer += "=" 22 | if B[i] < 0: 23 | answer += ">" 24 | print(answer) 25 | -------------------------------------------------------------------------------- /codes/python/Code_4_03_1.py: -------------------------------------------------------------------------------- 1 | r = 2.0 # √2 を求めたいから 2 | a = 2.0 # 初期値を適当に 2.0 にセットする 3 | repeats = 5 4 | 5 | for i in range(1, repeats + 1): 6 | # 点 (a, f(a)) の x 座標と y 座標を求める 7 | zahyou_x, zahyou_y = a, a * a 8 | 9 | # 接線の式 y = sessen_a * x + sessen_b を求める 10 | sessen_a = 2.0 * zahyou_x 11 | sessen_b = zahyou_y - sessen_a * zahyou_x 12 | 13 | # 次の a の値 next_a を求める 14 | next_a = (r - sessen_b) / sessen_a 15 | print("Step #%d: a = %.12f -> %.12f" % (i, a, next_a)) 16 | a = next_a 17 | -------------------------------------------------------------------------------- /codes/python/Code_4_04_1.py: -------------------------------------------------------------------------------- 1 | # 入力 → 配列 prime の初期化 2 | N = int(input()) 3 | prime = [ True ] * (N + 1) 4 | 5 | # エラトステネスのふるい 6 | LIMIT = int(N ** 0.5) 7 | for i in range(2, LIMIT + 1): 8 | if prime[i] == True: 9 | # x = 2i, 3i, 4i, ... と、N 以下の間ループし続ける 10 | for j in range(2 * i, N + 1, i): 11 | prime[j] = False 12 | 13 | # N 以下の素数を小さい順に出力 14 | for i in range(2, N + 1): 15 | if prime[i] == True: 16 | print(i) 17 | -------------------------------------------------------------------------------- /codes/python/Code_4_05_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, M = map(int, input().split()) 3 | A = [ None ] * M 4 | B = [ None ] * M 5 | for i in range(M): 6 | A[i], B[i] = map(int, input().split()) 7 | 8 | # 隣接リストの作成 9 | G = [ list() for i in range(N + 1) ] # G[i] は頂点 i に隣接する頂点のリスト 10 | for i in range(M): 11 | G[A[i]].append(B[i]) # 頂点 A[i] に隣接する頂点として B[i] を追加 12 | G[B[i]].append(A[i]) # 頂点 B[i] に隣接する頂点として A[i] を追加 13 | 14 | # 出力(len(G[i]) は頂点 i に隣接する頂点のリストの大きさ = 次数) 15 | for i in range(1, N + 1): 16 | output = str(i) + ": {" 17 | for j in range(len(G[i])): 18 | if j >= 1: 19 | output += "," 20 | output += str(G[i][j]) # G[i][j] は頂点 i に隣接する頂点のうち j+1 番目のもの 21 | output += "}" 22 | print(output) 23 | -------------------------------------------------------------------------------- /codes/python/Code_4_05_3.py: -------------------------------------------------------------------------------- 1 | import queue 2 | 3 | # 入力 4 | N, M = map(int, input().split()) 5 | A = [ None ] * M 6 | B = [ None ] * M 7 | for i in range(M): 8 | A[i], B[i] = map(int, input().split()) 9 | 10 | # 隣接リストの作成 11 | G = [ list() for i in range(N + 1) ] 12 | for i in range(M): 13 | G[A[i]].append(B[i]) 14 | G[B[i]].append(A[i]) 15 | 16 | # 幅優先探索の初期化 (dist[i] = -1 のとき、未到達の白色頂点である) 17 | dist = [ -1 ] * (N + 1) 18 | Q = queue.Queue() 19 | dist[1] = 0 20 | Q.put(1) # Q に 1 を追加(操作 1) 21 | 22 | # 幅優先探索 23 | while not Q.empty(): 24 | pos = Q.get() # Q の先頭を調べ、これを取り出す(操作 2, 3) 25 | for nex in G[pos]: 26 | if dist[nex] == -1: 27 | dist[nex] = dist[pos] + 1 28 | Q.put(nex) # Q に nex を追加(操作 1) 29 | 30 | # 頂点 1 から各頂点までの最短距離を出力 31 | for i in range(1, N + 1): 32 | print(dist[i]) 33 | -------------------------------------------------------------------------------- /codes/python/Code_4_06_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | 3 | a = [ None ] * (N + 1) 4 | a[1], a[2] = 1, 1 5 | for i in range(3, N + 1): 6 | a[i] = a[i - 1] + a[i - 2] 7 | 8 | print(a[N] % 1000000007) 9 | 10 | # このプログラムを N = 1000 で実行すると、正しく 517691607 と出力されます。 11 | # しかし、大きな数の計算を行うのに時間がかかるので、N = 100000 程度でも実行に 1 秒ほどかかります。 12 | -------------------------------------------------------------------------------- /codes/python/Code_4_06_2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | 3 | a = [ None ] * (N + 1) 4 | a[1], a[2] = 1, 1 5 | for i in range(3, N + 1): 6 | a[i] = (a[i - 1] + a[i - 2]) % 1000000007 7 | 8 | print(a[N] % 1000000007) 9 | 10 | # このプログラムは、N = 100000 でも 0.05 秒ほどの実行で答えが求まります。 11 | # これは、コード 4.6.1 と比較して数十倍速いスピードです。 12 | # N <= 10 ** 7 の制約では実行に最大 3 秒ほどかかってしまいますが、PyPy3 で実行すれば 1 秒の制限に間に合います。 13 | -------------------------------------------------------------------------------- /codes/python/Code_4_06_3.py: -------------------------------------------------------------------------------- 1 | MOD = 1000000007 2 | 3 | a, b = map(int, input().split()) 4 | 5 | answer = 1 # a の 0 乗は 1 なので、answer = 1 に初期化しておく 6 | for i in range(b): 7 | answer = (answer * a) % MOD 8 | 9 | print(answer) 10 | -------------------------------------------------------------------------------- /codes/python/Code_4_06_4.py: -------------------------------------------------------------------------------- 1 | # 繰り返し二乗法(p は a**1, a**2, a**4, a**8, ... といった値をとる) 2 | def modpow(a, b, m): 3 | p = a 4 | answer = 1 5 | for i in range(30): 6 | if (b & (1 << i)) != 0: 7 | answer = (answer * p) % m 8 | p = (p * p) % m 9 | return answer 10 | 11 | MOD = 1000000007 12 | 13 | a, b = map(int, input().split()) 14 | print(modpow(a, b, MOD)) 15 | 16 | # 補足 17 | # 実は、Python では「a**b を m で割った余り」を繰り返し二乗法で求める関数 pow(a, b, m) が標準ライブラリとして使えます。 18 | # Code_4_06_4_extra.py はこれを使ったプログラムです。こちらもぜひ見てください。 19 | -------------------------------------------------------------------------------- /codes/python/Code_4_06_4_extra.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | print(pow(a, b, 1000000007)) # pow(a, b, m) は、a**b を m で割った余りを返す関数 3 | -------------------------------------------------------------------------------- /codes/python/Code_4_06_5.py: -------------------------------------------------------------------------------- 1 | # 繰り返し二乗法(p は a**1, a**2, a**4, a**8, ... といった値をとる) 2 | def modpow(a, b, m): 3 | p = a 4 | answer = 1 5 | for i in range(30): 6 | if (b & (1 << i)) != 0: 7 | answer = (answer * p) % m 8 | p = (p * p) % m 9 | return answer 10 | 11 | # division(a, b, m) は a÷b mod m を返す関数 12 | def division(a, b, m): 13 | return (a * modpow(b, m - 2, m)) % m 14 | 15 | MOD = 1000000007 16 | 17 | # 入力 18 | X, Y = map(int, input().split()) 19 | 20 | # 二項係数の分子と分母を求める(手順 1./手順 2.) 21 | bunshi, bunbo = 1, 1 22 | for i in range(1, X + Y + 1): 23 | bunshi = (bunshi * i) % MOD 24 | for i in range(1, X + 1): 25 | bunbo = (bunbo * i) % MOD 26 | for i in range(1, Y + 1): 27 | bunbo = (bunbo * i) % MOD 28 | 29 | # 答えを求める(手順 3.) 30 | print(division(bunshi, bunbo, MOD)) 31 | -------------------------------------------------------------------------------- /codes/python/Code_5_02_1.py: -------------------------------------------------------------------------------- 1 | # 注意:以下のプログラムは N >= 1 で正しく動作します。 2 | # N = 0 では答えは「1」となるので、また別の場合分けをする必要があります。 3 | 4 | N = int(input()) 5 | if N % 4 == 1: 6 | print(2) 7 | if N % 4 == 2: 8 | print(4) 9 | if N % 4 == 3: 10 | print(8) 11 | if N % 4 == 0: 12 | print(6) 13 | -------------------------------------------------------------------------------- /codes/python/Code_5_02_2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | if N % 4 == 0: 3 | print("Second") 4 | else: 5 | print("First") 6 | -------------------------------------------------------------------------------- /codes/python/Code_5_03_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | if N % 2 == 0: 3 | print("Yes") 4 | else: 5 | print("No") 6 | -------------------------------------------------------------------------------- /codes/python/Code_5_03_2.py: -------------------------------------------------------------------------------- 1 | # 入力 → 数列の要素の総和 S を求める 2 | N, K = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | S = sum(A) 5 | 6 | # 答えの出力 7 | if S % 2 != K % 2: 8 | print("No") 9 | elif S > K: 10 | print("No") 11 | else: 12 | print("Yes") 13 | -------------------------------------------------------------------------------- /codes/python/Code_5_04_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, K = map(int, input().split()) 3 | 4 | # 事象 B の個数 yojishou を数える 5 | yojishou = 0 6 | for a in range(1, N + 1): 7 | l = max(1, a - (K - 1)) # b, c の探索範囲の下限 l 8 | r = min(N, a + (K - 1)) # b, c の探索範囲の上限 r 9 | for b in range(l, r + 1): 10 | for c in range(l, r + 1): 11 | if abs(b - c) <= K - 1: 12 | yojishou += 1 13 | 14 | # 答えの出力 15 | print(N ** 3 - yojishou) 16 | -------------------------------------------------------------------------------- /codes/python/Code_5_05_1.py: -------------------------------------------------------------------------------- 1 | a, b, c, d = map(int, input().split()) 2 | print(max(a * c, a * d, b * c, b * d)) 3 | -------------------------------------------------------------------------------- /codes/python/Code_5_06_1.py: -------------------------------------------------------------------------------- 1 | # 小問題 t を解く関数 2 | def shou_mondai(A, B, t): 3 | cl = (A + t - 1) // t # A÷t の小数点以下切り上げ 4 | cr = B // t # B÷t の小数点以下切り捨て 5 | if cr - cl >= 1: 6 | return True 7 | else: 8 | return False 9 | 10 | A, B = map(int, input().split()) 11 | for i in range(1, B + 1): 12 | if shou_mondai(A, B, i) == True: 13 | answer = i 14 | print(answer) 15 | -------------------------------------------------------------------------------- /codes/python/Code_5_07_1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 答えを求める 6 | answer = 0 7 | for i in range(N): 8 | # コード 5.7.1 (C++) では A[i] * (-N + 2 * i - 1) となっていますが、ここでは i = 0 から始まるので -N + 2 * i + 1 が掛けられます。 9 | answer += A[i] * (-N + 2 * i + 1) 10 | 11 | # 答えの出力 12 | print(answer) 13 | -------------------------------------------------------------------------------- /codes/python/Code_5_09_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | 3 | answer = 0 4 | 5 | # 10000 円以上の間、10000 円札を使い続ける 6 | while N >= 10000: 7 | N -= 10000 8 | answer += 1 9 | 10 | # 5000 円以上の間、5000 円札を使い続ける 11 | while N >= 5000: 12 | N -= 5000 13 | answer += 1 14 | 15 | # 残った金額を 1000 円札で支払う 16 | while N >= 1: 17 | N -= 1000 18 | answer += 1 19 | 20 | print(answer) 21 | -------------------------------------------------------------------------------- /codes/python/Code_5_09_2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | L = [ None ] * N 4 | R = [ None ] * N 5 | for i in range(N): 6 | L[i], R[i] = map(int, input().split()) 7 | 8 | # 映画の選び方のシミュレーション 9 | # 見れる映画の終了時刻の最小値 min_endtime は、最初 1000000 (INF で設定)のようなあり得ない値にセットする 10 | INF = 1000000 11 | current_time = 0 # current_time は現在時刻(直前に見た映画の終了時刻) 12 | answer = 0 13 | while True: 14 | min_endtime = INF 15 | for i in range(N): 16 | if L[i] >= current_time: 17 | min_endtime = min(min_endtime, R[i]) 18 | if min_endtime == INF: 19 | break 20 | current_time = min_endtime 21 | answer += 1 22 | 23 | # 答えの出力 24 | print(answer) 25 | -------------------------------------------------------------------------------- /codes/python/Code_5_10_1.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | a, b, c = map(int, input().split()) 4 | 5 | if math.sqrt(a) + math.sqrt(b) < math.sqrt(c): 6 | print("Yes") 7 | else: 8 | print("No") 9 | -------------------------------------------------------------------------------- /codes/python/Code_5_10_2.py: -------------------------------------------------------------------------------- 1 | a, b, c = map(int, input().split()) 2 | if c - a - b < 0: 3 | print("No") 4 | elif 4 * a * b < (c - a - b) * (c - a - b): 5 | print("Yes") 6 | else: 7 | print("No") 8 | -------------------------------------------------------------------------------- /codes/python/Code_5_10_3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, X, Y = map(int, input().split()) 3 | 4 | # 4 つの整数 (a, b, c, d) の全探索 5 | flag = False 6 | for a in range(1, N + 1): 7 | for b in range(a, N + 1): 8 | for c in range(b, N + 1): 9 | for d in range(c, N + 1): 10 | if a + b + c + d == X and a * b * c * d == Y: 11 | flag = True # 答えが見つかったら flag を true にする 12 | 13 | # 答えの出力 14 | if flag == True: 15 | print("Yes") 16 | else: 17 | print("No") 18 | 19 | # p.256 注 5.10.2 に書かれてあるように、Python は実行速度が遅いので、最大ケースでは実行に数十秒かかります。 20 | # しかし、同じプログラムを PyPy3 で実行すると、最大ケースでも 1 秒ほどで答えを求めることができます。 21 | # より良い計算量のプログラムについては、Code_5_10_3_extra.py を見てください。 22 | -------------------------------------------------------------------------------- /codes/python/Code_5_10_3_extra.py: -------------------------------------------------------------------------------- 1 | N, X, Y = map(int, input().split()) 2 | 3 | if Y > N ** 4: 4 | print("No") 5 | else: 6 | d = [] 7 | for i in range(1, N + 1): 8 | if Y % i == 0: 9 | d.append(i) 10 | divcnt = len(d) 11 | flag = False 12 | for i in range(0, divcnt): 13 | for j in range(i, divcnt): 14 | for k in range(j, divcnt): 15 | for l in range(k, divcnt): 16 | if d[i] + d[j] + d[k] + d[l] == X and d[i] * d[j] * d[k] * d[l] == Y: 17 | flag = True 18 | if flag == True: 19 | print("Yes") 20 | else: 21 | print("No") 22 | 23 | # 実は、このプログラムの計算量は O(N) です。 24 | # この理由をぜひ考えてみましょう。 25 | -------------------------------------------------------------------------------- /codes/python/Code_5_10_4.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | S = input() 4 | 5 | # '(' の数 - ')' の数を depth とする 6 | # 途中で depth が負になったら、この時点で No 7 | depth = 0 8 | flag = True 9 | for i in range(N): 10 | if S[i] == '(': 11 | depth += 1 12 | if S[i] == ')': 13 | depth -= 1 14 | if depth < 0: 15 | flag = False 16 | 17 | # 最後、depth = 0 ['(' と ')' の数が同じ] であるかも追加で判定する 18 | if flag == True and depth == 0: 19 | print("Yes") 20 | else: 21 | print("No") 22 | -------------------------------------------------------------------------------- /editorial/Editorial_All.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/Editorial_All.pdf -------------------------------------------------------------------------------- /editorial/chap2-1/chap2-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap2-1/chap2-1.pdf -------------------------------------------------------------------------------- /editorial/chap2-1/prob2-1-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int A1, A2, A3; 6 | scanf("%d%d%d", &A1, &A2, &A3); 7 | 8 | // 出力 9 | printf("%d\n", A1 * A2 * A3); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /editorial/chap2-1/prob2-1-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 入力 6 | int A1, A2, A3; 7 | cin >> A1 >> A2 >> A3; 8 | 9 | // 出力 10 | cout << A1 * A2 * A3 << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /editorial/chap2-1/prob2-1-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int A1 = sc.nextInt(); 9 | int A2 = sc.nextInt(); 10 | int A3 = sc.nextInt(); 11 | 12 | // 出力 13 | System.out.println(A1 * A2 * A3); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /editorial/chap2-1/prob2-1-3.py: -------------------------------------------------------------------------------- 1 | A1, A2, A3 = map(int, input().split()) 2 | print(A1 * A2 * A3) 3 | -------------------------------------------------------------------------------- /editorial/chap2-2/chap2-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap2-2/chap2-2.pdf -------------------------------------------------------------------------------- /editorial/chap2-2/prob2-2-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[109]; 4 | int Answer = 0; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%d", &N); 9 | for (int i = 1; i <= N; i++) scanf("%d", &A[i]); 10 | 11 | // 答えの計算 12 | for (int i = 1; i <= N; i++) { 13 | Answer += A[i]; 14 | } 15 | 16 | // 出力 17 | printf("%d\n", Answer % 100); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /editorial/chap2-2/prob2-2-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[109]; 5 | int Answer = 0; 6 | 7 | int main() { 8 | // 入力 9 | cin >> N; 10 | for (int i = 1; i <= N; i++) { 11 | cin >> A[i]; 12 | } 13 | 14 | // 答えの計算 15 | for (int i = 1; i <= N; i++) { 16 | Answer += A[i]; 17 | } 18 | 19 | // 出力 20 | cout << Answer % 100 << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /editorial/chap2-2/prob2-2-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | A[i] = sc.nextInt(); 12 | } 13 | 14 | // 答えの計算 15 | int Answer = 0; 16 | for (int i = 1; i <= N; i++) { 17 | Answer += A[i]; 18 | } 19 | 20 | // 出力 21 | System.out.println(Answer % 100); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /editorial/chap2-2/prob2-2-4.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 答えの計算 6 | Answer = 0 7 | for i in range(N): 8 | Answer += A[i] 9 | 10 | # 出力 11 | print(Answer % 100) 12 | -------------------------------------------------------------------------------- /editorial/chap2-3/chap2-3.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 2.3 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap2-3/chap2-3.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap2-3/chap2-3.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | この節には、プログラムを書く問題はありません。 -------------------------------------------------------------------------------- /editorial/chap2-3/chap2-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap2-3/chap2-3.pdf -------------------------------------------------------------------------------- /editorial/chap2-4/chap2-4.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 2.4 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap2-4/chap2-4.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap2-4/chap2-4.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | この節には、プログラムを書く問題はありません。 -------------------------------------------------------------------------------- /editorial/chap2-4/chap2-4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap2-4/chap2-4.pdf -------------------------------------------------------------------------------- /editorial/chap2-5/chap2-5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap2-5/chap2-5.pdf -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N; 6 | scanf("%d", &N); 7 | 8 | // 答えの計算 9 | long long Answer = 1; 10 | for (int i = 2; i <= N; i++) Answer *= i; 11 | 12 | // 出力 13 | printf("%lld\n", Answer); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long N; 6 | long long Answer = 1; 7 | cin >> N; 8 | for (int i = 2; i <= N; i++) Answer *= i; // Answer に i を掛ける 9 | cout << Answer << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | long N = sc.nextInt(); 9 | 10 | // 答えの計算 11 | long Answer = 1; 12 | for (int i = 1; i <= N; i++) Answer *= i; 13 | 14 | // 出力 15 | System.out.println(Answer); 16 | } 17 | }; -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 答えの計算 5 | Answer = 1 6 | for i in range(1,N+1): 7 | Answer *= i 8 | 9 | # 出力 10 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool isprime(int x) { 5 | for (int i = 2; i <= x - 1; i++) { 6 | // x を i で割った余りが 0 のとき、x は i で割り切れる 7 | if (x % i == 0) return false; 8 | } 9 | return true; 10 | } 11 | 12 | int main() { 13 | // 入力 14 | int N; 15 | scanf("%d", &N); 16 | 17 | // 答えの出力(空白区切りで出力する場合) 18 | for (int i = 2; i <= N; i++) { 19 | if (isprime(i) == true) { 20 | if (i >= 3) printf(" "); 21 | printf("%d", i); 22 | } 23 | } 24 | printf("\n"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isprime(int x) { 5 | for (int i = 2; i <= x - 1; i++) { 6 | // x を i で割った余りが 0 のとき、x は i で割り切れる 7 | if (x % i == 0) return false; 8 | } 9 | return true; 10 | } 11 | 12 | int main() { 13 | // 入力 14 | int N; 15 | cin >> N; 16 | 17 | // 答えの出力(空白区切りで出力する場合) 18 | for (int i = 2; i <= N; i++) { 19 | if (isprime(i) == true) { 20 | if (i >= 3) cout << " "; 21 | cout << i; 22 | } 23 | } 24 | cout << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int N = sc.nextInt(); 9 | 10 | // 出力 11 | for (int i = 2; i <= N; i++) { 12 | if (isPrime(i) == true) { 13 | if (i >= 3) System.out.print(" "); 14 | System.out.print(i); 15 | } 16 | } 17 | System.out.println(); 18 | } 19 | static boolean isPrime(long N) { 20 | // 2 以上の整数 N に対し、N が素数であれば true、素数でなければ false を返す関数 21 | for (long i = 2; i <= N - 1; i++) { 22 | if (N % i == 0) { 23 | return false; 24 | } 25 | } 26 | return true; 27 | } 28 | } -------------------------------------------------------------------------------- /editorial/chap2-5/prob2-5-4.py: -------------------------------------------------------------------------------- 1 | def isprime(N): 2 | for i in range(2, N): 3 | if N % i == 0: 4 | return False 5 | return True 6 | 7 | N = int(input()) 8 | A = [] 9 | for i in range(2,N+1): 10 | if isprime(i) == True: 11 | A.append(i) 12 | 13 | print(*A) -------------------------------------------------------------------------------- /editorial/chap3-1/chap3-1.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 3.1 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-1/chap3-1.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-1/chap3-1.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 各問題のプログラムのリンクは、以下の通りです。 10 | 11 | | 問題 | C++ | Python | JAVA | C | 12 | |:---:|:---:|:---:|:---:|:---:| 13 | | 節末問題 3.1.2 | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-1/prob3-1-2.cpp) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-1/prob3-1-2.py) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-1/prob3-1-2.java) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-1/prob3-1-2.c) | -------------------------------------------------------------------------------- /editorial/chap3-1/chap3-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-1/chap3-1.pdf -------------------------------------------------------------------------------- /editorial/chap3-1/prob3-1-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // 入力 6 | long long N; 7 | scanf("%lld", &N); 8 | 9 | // 素因数分解(空白区切りで出力) 10 | bool flag = false; 11 | for (long long i = 2; i * i <= N; i++) { 12 | while (N % i == 0) { 13 | if (flag == true) printf(" "); 14 | flag = true; 15 | N /= i; 16 | printf("%lld", i); 17 | } 18 | } 19 | if (N >= 2) { 20 | if (flag == true) printf(" "); 21 | flag = true; 22 | printf("%lld", N); 23 | } 24 | printf("\n"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap3-1/prob3-1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 入力 6 | long long N; 7 | cin >> N; 8 | 9 | // 素因数分解(空白区切りで出力) 10 | bool flag = false; 11 | for (long long i = 2; i * i <= N; i++) { 12 | while (N % i == 0) { 13 | if (flag == true) cout << " "; 14 | flag = true; 15 | N /= i; 16 | cout << i; 17 | } 18 | } 19 | if (N >= 2) { 20 | if (flag == true) cout << " "; 21 | flag = true; 22 | cout << N; 23 | } 24 | cout << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap3-1/prob3-1-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | long N = sc.nextLong(); 9 | 10 | // 素因数分解・出力(空白区切りの場合) 11 | boolean flag = false; 12 | for (long i = 2; i * i <= N; i++) { 13 | while (N % i == 0) { 14 | if (flag == true) System.out.print(" "); 15 | flag = true; 16 | N /= i; 17 | System.out.print(i); 18 | } 19 | } 20 | 21 | // 最後に残った N 22 | if (N >= 2) { 23 | if (flag == true) System.out.print(" "); 24 | flag = true; 25 | System.out.print(N); 26 | } 27 | System.out.println(); 28 | } 29 | } -------------------------------------------------------------------------------- /editorial/chap3-1/prob3-1-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 素因数分解 5 | Answer = [] 6 | LIMIT = int(N ** 0.5) 7 | for i in range(2, LIMIT + 1): 8 | while N % i == 0: 9 | N /= i 10 | Answer.append(i) 11 | 12 | if N >= 2: 13 | Answer.append(int(N)) 14 | 15 | # 出力 16 | print(*Answer) -------------------------------------------------------------------------------- /editorial/chap3-2/chap3-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-2/chap3-2.pdf -------------------------------------------------------------------------------- /editorial/chap3-2/prob3-2-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long GCD(long long A, long long B) { 4 | while (A >= 1 && B >= 1) { 5 | if (A < B) B = B % A; // A < B の場合、大きい方 B を書き換える 6 | else A = A % B; // A >= B の場合、大きい方 A を書き換える 7 | } 8 | if (A >= 1) return A; 9 | return B; 10 | } 11 | 12 | long long N; 13 | long long A[100009]; 14 | 15 | int main() { 16 | // 入力 17 | scanf("%lld", &N); 18 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 19 | 20 | // 答えを求める 21 | long long R = GCD(A[1], A[2]); 22 | for (int i = 3; i <= N; i++) { 23 | R = GCD(R, A[i]); 24 | } 25 | 26 | // 出力 27 | printf("%lld\n", R); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /editorial/chap3-2/prob3-2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long GCD(long long A, long long B) { 6 | while (A >= 1 && B >= 1) { 7 | if (A < B) B = B % A; // A < B の場合、大きい方 B を書き換える 8 | else A = A % B; // A >= B の場合、大きい方 A を書き換える 9 | } 10 | if (A >= 1) return A; 11 | return B; 12 | } 13 | 14 | long long N; 15 | long long A[100009]; 16 | 17 | int main() { 18 | // 入力 19 | cin >> N; 20 | for (int i = 1; i <= N; i++) cin >> A[i]; 21 | 22 | // 答えを求める 23 | long long R = GCD(A[1], A[2]); 24 | for (int i = 3; i <= N; i++) { 25 | R = GCD(R, A[i]); 26 | } 27 | 28 | // 出力 29 | cout << R << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /editorial/chap3-2/prob3-2-2.py: -------------------------------------------------------------------------------- 1 | # 正の整数 A と B の最大公約数を返す関数 2 | def GCD(A, B): 3 | while A >= 1 and B >= 1: 4 | if A < B: 5 | B = B % A # A < B の場合、大きい方 B を書き換える 6 | else: 7 | A = A % B # A >= B の場合、大きい方 A を書き換える 8 | if A >= 1: 9 | return A 10 | return B 11 | 12 | # 入力 13 | N = int(input()) 14 | A = list(map(int, input().split())) 15 | 16 | # 答えを求める 17 | R = GCD(A[0], A[1]) 18 | for i in range(2, N): 19 | R = GCD(R, A[i]) 20 | 21 | # 出力 22 | print(R) -------------------------------------------------------------------------------- /editorial/chap3-2/prob3-2-3.py: -------------------------------------------------------------------------------- 1 | # 最大公約数を返す関数 2 | def GCD(A, B): 3 | while A >= 1 and B >= 1: 4 | if A < B: 5 | B = B % A # A < B の場合、大きい方 B を書き換える 6 | else: 7 | A = A % B # A >= B の場合、大きい方 A を書き換える 8 | if A >= 1: 9 | return A 10 | return B 11 | 12 | # 最小公倍数を返す関数 13 | def LCM(A, B): 14 | return int(A / GCD(A, B)) * B 15 | 16 | # 入力 17 | N = int(input()) 18 | A = list(map(int, input().split())) 19 | 20 | # 答えを求める 21 | R = LCM(A[0], A[1]) 22 | for i in range(2, N): 23 | R = LCM(R, A[i]) 24 | 25 | # 出力 26 | print(R) -------------------------------------------------------------------------------- /editorial/chap3-3/chap3-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-3/chap3-3.pdf -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long n, r; 4 | long long Fact_n = 1; 5 | long long Fact_r = 1; 6 | long long Fact_nr = 1; 7 | 8 | int main() { 9 | // 入力 10 | scanf("%lld%lld", &n, &r); 11 | 12 | // 階乗の計算 13 | for (int i = 1; i <= n; i++) Fact_n *= i; 14 | for (int i = 1; i <= r; i++) Fact_r *= i; 15 | for (int i = 1; i <= n - r; i++) Fact_nr *= i; 16 | 17 | // 出力 18 | printf("%lld\n", Fact_n / (Fact_r * Fact_nr)); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long n, r; 5 | long long Fact_n = 1; 6 | long long Fact_r = 1; 7 | long long Fact_nr = 1; 8 | 9 | int main() { 10 | // 入力 11 | cin >> n >> r; 12 | 13 | // 階乗の計算 14 | for (int i = 1; i <= n; i++) Fact_n *= i; 15 | for (int i = 1; i <= r; i++) Fact_r *= i; 16 | for (int i = 1; i <= n - r; i++) Fact_nr *= i; 17 | 18 | // 出力 19 | cout << Fact_n / (Fact_r * Fact_nr) << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int n = sc.nextInt(); 9 | int r = sc.nextInt(); 10 | 11 | // 会場の計算 12 | long fact_n = 1, fact_r = 1, fact_nr = 1; 13 | for (int i = 1; i <= n; i++) fact_n *= i; 14 | for (int i = 1; i <= r; i++) fact_r *= i; 15 | for (int i = 1; i <= n - r; i++) fact_nr *= i; 16 | 17 | // 出力 18 | System.out.println(fact_n / (fact_r * fact_nr)); 19 | } 20 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | n,r = map(int, input().split()) 3 | 4 | # 階乗の計算 5 | fact_n = 1 6 | for i in range(1, n+1): 7 | fact_n *= i 8 | 9 | fact_r = 1 10 | for i in range(1, r+1): 11 | fact_r *= i 12 | 13 | fact_nr = 1 14 | for i in range(1, n-r+1): 15 | fact_nr *= i 16 | 17 | # 出力 18 | print(int(fact_n / (fact_r * fact_nr))) -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N; 4 | long long A[200009]; 5 | long long a = 0, b = 0, c = 0, d = 0; // オーバーフロー回避のため 64 ビット整数を使う 6 | 7 | int main() { 8 | // 入力 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 11 | 12 | // a, b, c, d の個数を数える 13 | for (int i = 1; i <= N; i++) { 14 | if (A[i] == 100) a += 1; 15 | if (A[i] == 200) b += 1; 16 | if (A[i] == 300) c += 1; 17 | if (A[i] == 400) d += 1; 18 | } 19 | 20 | // 出力(答えは a * d + b * c) 21 | printf("%lld\n", a * d + b * c); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N; 5 | long long A[200009]; 6 | long long a = 0, b = 0, c = 0, d = 0; // オーバーフロー回避のため 64 ビット整数を使う 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // a, b, c, d の個数を数える 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 100) a += 1; 16 | if (A[i] == 200) b += 1; 17 | if (A[i] == 300) c += 1; 18 | if (A[i] == 400) d += 1; 19 | } 20 | 21 | // 出力(答えは a * d + b * c) 22 | cout << a * d + b * c << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | 12 | // 答えを求める 13 | long a = 0, b = 0, c = 0, d = 0; 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 100) a += 1; 16 | if (A[i] == 200) b += 1; 17 | if (A[i] == 300) c += 1; 18 | if (A[i] == 400) d += 1; 19 | } 20 | 21 | // 出力 22 | System.out.println(a * d + b * c); 23 | } 24 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-4.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 答えを求める 6 | a = 0 7 | b = 0 8 | c = 0 9 | d = 0 10 | for i in range(N): 11 | if A[i] == 100: 12 | a += 1 13 | if A[i] == 200: 14 | b += 1 15 | if A[i] == 300: 16 | c += 1 17 | if A[i] == 400: 18 | d += 1 19 | 20 | # 出力 21 | print(a * d + b * c) -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N; 4 | long long A[500009]; 5 | long long x = 0, y = 0, z = 0; 6 | 7 | int main() { 8 | // 入力 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 11 | 12 | // a, b, c, d の個数を数える 13 | for (int i = 1; i <= N; i++) { 14 | if (A[i] == 1) x += 1; 15 | if (A[i] == 2) y += 1; 16 | if (A[i] == 3) z += 1; 17 | } 18 | 19 | // 出力 20 | printf("%lld\n", x * (x - 1) / 2 + y * (y - 1) / 2 + z * (z - 1) / 2); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N; 5 | long long A[500009]; 6 | long long x = 0, y = 0, z = 0; 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // a, b, c, d の個数を数える 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 1) x += 1; 16 | if (A[i] == 2) y += 1; 17 | if (A[i] == 3) z += 1; 18 | } 19 | 20 | // 出力 21 | cout << x * (x - 1) / 2 + y * (y - 1) / 2 + z * (z - 1) / 2 << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | 12 | // 答えを求める 13 | long x = 0, y = 0, z = 0; 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 1) x += 1; 16 | if (A[i] == 2) y += 1; 17 | if (A[i] == 3) z += 1; 18 | } 19 | 20 | // 出力 21 | System.out.println(x * (x - 1) / 2 + y * (y - 1) / 2 + z * (z - 1) / 2); 22 | } 23 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-5.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 答えを求める 6 | x = 0 7 | y = 0 8 | z = 0 9 | for i in range(N): 10 | if A[i] == 1: 11 | x += 1 12 | if A[i] == 2: 13 | y += 1 14 | if A[i] == 3: 15 | z += 1 16 | 17 | # 出力 18 | print(x * (x - 1) // 2 + y * (y - 1) // 2 + z * (z - 1) // 2) -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N, A[200009]; 4 | long long cnt[100009]; 5 | long long Answer = 0; 6 | 7 | int main() { 8 | // 入力 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 11 | 12 | // cnt[1], cnt[2], ..., cnt[99999] を数える 13 | for (int i = 1; i <= 99999; i++) cnt[i] = 0; 14 | for (int i = 1; i <= N; i++) cnt[A[i]] += 1; 15 | 16 | // 答えを求める 17 | for (int i = 1; i <= 49999; i++) { 18 | Answer += cnt[i] * cnt[100000 - i]; 19 | } 20 | Answer += cnt[50000] * (cnt[50000] - 1) / 2; 21 | 22 | // 出力 23 | printf("%lld\n", Answer); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, A[200009]; 5 | long long cnt[100009]; 6 | long long Answer = 0; 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // cnt[1], cnt[2], ..., cnt[99999] を数える 14 | for (int i = 1; i <= 99999; i++) cnt[i] = 0; 15 | for (int i = 1; i <= N; i++) cnt[A[i]] += 1; 16 | 17 | // 答えを求める 18 | for (int i = 1; i <= 49999; i++) { 19 | Answer += cnt[i] * cnt[100000 - i]; 20 | } 21 | Answer += cnt[50000] * (cnt[50000] - 1) / 2; 22 | 23 | // 出力 24 | cout << Answer << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | 12 | // 答えを求める 13 | long[] cnt = new long[100009]; 14 | for (int i = 1; i <= 99999; i++) cnt[i] = 0; 15 | for (int i = 1; i <= N; i++) cnt[A[i]] += 1; 16 | 17 | long Answer = 0; 18 | for (int i = 1; i <= 49999; i++) Answer += cnt[i] * cnt[100000 - i]; 19 | Answer += cnt[50000] * (cnt[50000] - 1) / 2; 20 | 21 | // 出力 22 | System.out.println(Answer); 23 | } 24 | } -------------------------------------------------------------------------------- /editorial/chap3-3/prob3-3-6.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 答えを求める 6 | cnt = [0 for i in range(100000)] 7 | for i in range(N): 8 | cnt[A[i]] += 1 9 | 10 | Answer = 0 11 | for i in range(1, 50000): 12 | Answer += cnt[i] * cnt[100000 - i] 13 | Answer += cnt[50000] * (cnt[50000] - 1) // 2 14 | 15 | # 出力 16 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap3-4/chap3-4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-4/chap3-4.pdf -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N; 4 | double A[109], B[109]; 5 | double Answer = 0.0; 6 | 7 | int main() { 8 | // 入力 9 | scanf("%d", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lf", &A[i]); 11 | for (int i = 1; i <= N; i++) scanf("%lf", &B[i]); 12 | 13 | // 期待値を求める 14 | for (int i = 1; i <= N; i++) { 15 | double eval = A[i] * (1.0 / 3.0) + B[i] * (2.0 / 3.0); 16 | Answer += eval; 17 | } 18 | 19 | // 出力 20 | printf("%.12lf\n", Answer); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | double A[109], B[109]; 6 | double Answer = 0.0; 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | for (int i = 1; i <= N; i++) cin >> B[i]; 13 | 14 | // 期待値を求める 15 | for (int i = 1; i <= N; i++) { 16 | double eval = A[i] * (1.0 / 3.0) + B[i] * (2.0 / 3.0); 17 | Answer += eval; 18 | } 19 | 20 | // 出力 21 | printf("%.12lf\n", Answer); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N + 1]; 9 | int[] B = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | for (int i = 1; i <= N; i++) B[i] = sc.nextInt(); 12 | 13 | // 答えの計算・出力 14 | double Answer = 0.0; 15 | for (int i = 1; i <= N; i++) { 16 | Answer += (1.0 / 3.0) * A[i] + (2.0 / 3.0) * B[i]; 17 | } 18 | System.out.format("%.12f\n", Answer); 19 | } 20 | } -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | B = list(map(int, input().split())) 5 | 6 | # 答えを求める 7 | Answer = 0.0 8 | for i in range(N): 9 | Answer += (1.0 / 3.0) * A[i] + (2.0 / 3.0) * B[i] 10 | 11 | # 出力 12 | print("%.12f" % Answer) -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N; 4 | double Answer = 0; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%d", &N); 9 | 10 | // 期待値を求める 11 | for (int i = N; i >= 1; i--) { 12 | Answer += 1.0 * N / i; 13 | } 14 | 15 | // 出力 16 | printf("%.12lf\n", Answer); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | double Answer = 0; 6 | 7 | int main() { 8 | // 入力 9 | cin >> N; 10 | 11 | // 期待値を求める 12 | for (int i = N; i >= 1; i--) { 13 | Answer += 1.0 * N / i; 14 | } 15 | 16 | // 出力 17 | printf("%.12lf\n", Answer); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | 9 | // 答えの計算 10 | double Answer = 0.0; 11 | for (int i = N; i >= 1; i--) { 12 | Answer += 1.0 * N / i; 13 | } 14 | 15 | // 出力 16 | System.out.format("%.12f\n", Answer); 17 | } 18 | } -------------------------------------------------------------------------------- /editorial/chap3-4/prob3-4-4.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 答えの計算 5 | Answer = 0.0 6 | for i in range(1, N+1): 7 | Answer += 1.0 * N / i 8 | 9 | # 出力 10 | print("%.12f" % Answer) -------------------------------------------------------------------------------- /editorial/chap3-5/chap3-5.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 3.5 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-5/chap3-5.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-5/chap3-5.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 各問題のプログラムのリンクは、以下の通りです。 10 | 11 | | 問題 | C++ | Python | JAVA | C | 12 | |:---:|:---:|:---:|:---:|:---:| 13 | | 節末問題 3.5.2 | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-5/prob3-5-2.cpp) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-5/prob3-5-2.py) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-5/prob3-5-2.java) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-5/prob3-5-2.c) | -------------------------------------------------------------------------------- /editorial/chap3-5/chap3-5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-5/chap3-5.pdf -------------------------------------------------------------------------------- /editorial/chap3-5/prob3-5-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int N = 1000000; 7 | int M = 0; 8 | 9 | for (int i = 1; i <= N; i++) { 10 | double px = 6.0 * rand() / (double)RAND_MAX; 11 | double py = 9.0 * rand() / (double)RAND_MAX; 12 | 13 | // 点 (3, 3) との距離。この値が 3 以下であれば半径 3 の円に含まれる。 14 | double dist_33 = sqrt((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)); 15 | 16 | // 点 (3, 7) との距離。この値が 2 以下であれば半径 2 の円に含まれる。 17 | double dist_37 = sqrt((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)); 18 | 19 | // 条件分岐 20 | if (dist_33 <= 3.0 || dist_37 <= 2.0) M += 1; 21 | } 22 | 23 | // N 回中何回表に入ったかを出力 24 | printf("%d\n", M); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap3-5/prob3-5-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int N = 1000000; 7 | int M = 0; 8 | 9 | for (int i = 1; i <= N; i++) { 10 | double px = 6.0 * rand() / (double)RAND_MAX; 11 | double py = 9.0 * rand() / (double)RAND_MAX; 12 | 13 | // 点 (3, 3) との距離。この値が 3 以下であれば半径 3 の円に含まれる。 14 | double dist_33 = sqrt((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)); 15 | 16 | // 点 (3, 7) との距離。この値が 2 以下であれば半径 2 の円に含まれる。 17 | double dist_37 = sqrt((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)); 18 | 19 | // 条件分岐 20 | if (dist_33 <= 3.0 || dist_37 <= 2.0) M += 1; 21 | } 22 | 23 | // N 回中何回表に入ったかを出力 24 | cout << M << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap3-5/prob3-5-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Random rd = new Random(); 6 | int N = 1000000; // N は試行回数(適宜変更する) 7 | int M = 0; 8 | for (int i = 1; i <= N; i++) { 9 | double px = 6.0 * rd.nextDouble(); 10 | double py = 9.0 * rd.nextDouble(); 11 | 12 | // 点 (3, 3) との距離。この値が 3 以下であれば半径 3 の円に含まれる 13 | double dist_33 = Math.sqrt((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)); 14 | 15 | // 点 (3, 7) との距離。この値が 2 以下であれば半径 2 の円に含まれる 16 | double dist_37 = Math.sqrt((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)); 17 | 18 | if (dist_33 <= 3.0 || dist_37 <= 2.0) M += 1; 19 | } 20 | System.out.println(M); 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap3-5/prob3-5-2.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 1000000 # N は試行回数(適宜変更する) 4 | M = 0 5 | for i in range(N): 6 | px = 6.0 * random.random() 7 | py = 9.0 * random.random() 8 | 9 | # 点 (3, 3) との距離。この値が 3 以下であれば半径 3 の円に含まれる。 10 | dist_33 = ((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)) ** 0.5 11 | 12 | # 点 (3, 7) との距離。この値が 2 以下であれば半径 2 の円に含まれる。 13 | dist_37 = ((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)) ** 0.5 14 | 15 | # 判定 16 | if (dist_33 <= 3.0 or dist_37 <= 2.0): 17 | M += 1 18 | 19 | print(M) -------------------------------------------------------------------------------- /editorial/chap3-6/chap3-6.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 3.6 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-6/chap3-6.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-6/chap3-6.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 各問題のプログラムのリンクは、以下の通りです。 10 | 11 | | 問題 | C++ | Python | JAVA | C | 12 | |:---:|:---:|:---:|:---:|:---:| 13 | | 節末問題 3.6.3 | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-6/prob3-6-3.cpp) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-6/prob3-6-3.py) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-6/prob3-6-3.java) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap3-6/prob3-6-3.c) | -------------------------------------------------------------------------------- /editorial/chap3-6/chap3-6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-6/chap3-6.pdf -------------------------------------------------------------------------------- /editorial/chap3-7/chap3-7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap3-7/chap3-7.pdf -------------------------------------------------------------------------------- /editorial/chap3-7/prob3-7-4.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, S = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | 5 | # 配列の初期化 6 | dp = [ [ None ] * (S + 1) for i in range(N + 1) ] 7 | dp[0][0] = True 8 | for i in range(1, S + 1): 9 | dp[0][i] = False 10 | 11 | # 動的計画法 12 | for i in range(1, N + 1): 13 | for j in range(0, S + 1): 14 | if j < A[i - 1]: 15 | # j < A[i-1] のとき、カード i を選べない 16 | dp[i][j] = dp[i - 1][j] 17 | else: 18 | # j >= A[i-1] のとき、選ぶ / 選ばない 両方の選択肢がある 19 | if (dp[i - 1][j] == True or dp[i - 1][j - A[i - 1]] == True): 20 | dp[i][j] = True 21 | else: 22 | dp[i][j] = False 23 | 24 | # 答えを出力 25 | if dp[N][S] == True: 26 | print("Yes") 27 | else: 28 | print("No") -------------------------------------------------------------------------------- /editorial/chap3-7/prob3-7-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N, A[500009]; 4 | long long dp1[500009], dp2[500009]; 5 | 6 | long long max(long long a, long long b) { 7 | if (a < b) return b; 8 | return a; 9 | } 10 | 11 | int main() { 12 | // 入力 13 | scanf("%lld", &N); 14 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 15 | 16 | // 配列の初期化 17 | dp1[0] = 0; 18 | dp2[0] = 0; 19 | 20 | // 動的計画法 21 | for (int i = 1; i <= N; i++) { 22 | dp1[i] = dp2[i - 1] + A[i]; 23 | dp2[i] = max(dp1[i - 1], dp2[i - 1]); 24 | } 25 | 26 | // 答えを出力 27 | printf("%lld\n", max(dp1[N], dp2[N])); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /editorial/chap3-7/prob3-7-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N, A[500009]; 6 | long long dp1[500009], dp2[500009]; 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // 配列の初期化 14 | dp1[0] = 0; 15 | dp2[0] = 0; 16 | 17 | // 動的計画法 18 | for (int i = 1; i <= N; i++) { 19 | dp1[i] = dp2[i - 1] + A[i]; 20 | dp2[i] = max(dp1[i - 1], dp2[i - 1]); 21 | } 22 | 23 | // 答えを出力 24 | cout << max(dp1[N], dp2[N]) << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap3-7/prob3-7-6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | A[i] = sc.nextInt(); 12 | } 13 | 14 | // 配列の初期化 15 | long[] dp1 = new long[N + 1]; 16 | long[] dp2 = new long[N + 1]; 17 | dp1[0] = 0; 18 | dp2[0] = 0; 19 | 20 | // 動的計画法 21 | for (int i = 1; i <= N; i++) { 22 | dp1[i] = dp2[i - 1] + A[i]; 23 | dp2[i] = Math.max(dp1[i - 1], dp2[i - 1]); 24 | } 25 | 26 | // 答えを計算して出力 27 | long Answer = Math.max(dp1[N], dp2[N]); 28 | System.out.println(Answer); 29 | } 30 | } -------------------------------------------------------------------------------- /editorial/chap3-7/prob3-7-6.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 配列の初期化 6 | dp1 = [ None ] * (N + 1) 7 | dp2 = [ None ] * (N + 1) 8 | dp1[0] = 0 9 | dp2[0] = 0 10 | 11 | # 動的計画法 12 | for i in range(1, N + 1): 13 | dp1[i] = dp2[i - 1] + A[i - 1] 14 | dp2[i] = max(dp1[i - 1], dp2[i - 1]) 15 | 16 | # 出力 17 | Answer = max(dp1[N], dp2[N]) 18 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap4-1/chap4-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-1/chap4-1.pdf -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N; 5 | double x[2009], y[2009]; 6 | double Answer = 1000000000.0; // 非常に大きい値に初期化 7 | 8 | int main() { 9 | // 入力 10 | scanf("%d", &N); 11 | for (int i = 1; i <= N; i++) scanf("%lf%lf", &x[i], &y[i]); 12 | 13 | // 全探索 14 | for (int i = 1; i <= N; i++) { 15 | for (int j = i + 1; j <= N; j++) { 16 | // dist は i 番目の点と j 番目の点の距離 17 | double dist = sqrt((x[i]-x[j]) * (x[i]-x[j]) + (y[i]-y[j]) * (y[i]-y[j])); 18 | if (Answer > dist) { 19 | Answer = dist; 20 | } 21 | } 22 | } 23 | 24 | // 答えの出力 25 | printf("%.12lf\n", Answer); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N; 6 | double x[2009], y[2009]; 7 | double Answer = 1000000000.0; // 非常に大きい値に初期化 8 | 9 | int main() { 10 | // 入力 11 | cin >> N; 12 | for (int i = 1; i <= N; i++) cin >> x[i] >> y[i]; 13 | 14 | // 全探索 15 | for (int i = 1; i <= N; i++) { 16 | for (int j = i + 1; j <= N; j++) { 17 | // dist は i 番目の点と j 番目の点の距離 18 | double dist = sqrt((x[i]-x[j]) * (x[i]-x[j]) + (y[i]-y[j]) * (y[i]-y[j])); 19 | Answer = min(Answer, dist); 20 | } 21 | } 22 | 23 | // 答えの出力 24 | printf("%.12lf¥n", Answer); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | double[] x = new double[N + 1]; 9 | double[] y = new double[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | x[i] = sc.nextDouble(); 12 | y[i] = sc.nextDouble(); 13 | } 14 | 15 | // 全探索 16 | double Answer = 1000000000.0; 17 | for (int i = 1; i <= N; i++) { 18 | for (int j = i + 1; j <= N; j++) { 19 | double dist = Math.sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); 20 | Answer = Math.min(Answer, dist); 21 | } 22 | } 23 | 24 | // 出力 25 | System.out.format("%.12f\n", Answer); 26 | } 27 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | x = [0 for i in range(N)] 4 | y = [0 for i in range(N)] 5 | for i in range(N): 6 | x[i],y[i] = map(int, input().split()) 7 | 8 | # 全探索 9 | Answer = 1000000000.0 10 | for i in range(N): 11 | for j in range(i+1,N): 12 | dist = (((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2) ** 0.5) 13 | Answer = min(Answer, dist) 14 | 15 | # 答えの出力 16 | print("%.12f" % Answer) 17 | -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double X1, Y1, R1; 5 | double X2, Y2, R2; 6 | 7 | int main() { 8 | // 入力 9 | scanf("%lf%lf%lf", &X1, &Y1, &R1); 10 | scanf("%lf%lf%lf", &X2, &Y2, &R2); 11 | 12 | // 円の中心間距離を求める 13 | double d = sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)); 14 | 15 | // 答えを出力 16 | if (d < abs(R1 - R2)) printf("1\n"); 17 | else if (d == abs(R1 - R2)) printf("2\n"); 18 | else if (d < R1 + R2) printf("3\n"); 19 | else if (d == R1 + R2) printf("4\n"); 20 | else printf("5\n"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double X1, Y1, R1; 6 | double X2, Y2, R2; 7 | 8 | int main() { 9 | // 入力 10 | cin >> X1 >> Y1 >> R1; 11 | cin >> X2 >> Y2 >> R2; 12 | 13 | // 円の中心間距離を求める 14 | double d = sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)); 15 | 16 | // 答えを出力 17 | if (d < abs(R1 - R2)) cout << "1" << endl; 18 | else if (d == abs(R1 - R2)) cout << "2" << endl; 19 | else if (d < R1 + R2) cout << "3" << endl; 20 | else if (d == R1 + R2) cout << "4" << endl; 21 | else cout << "5" << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | X1,Y1,R1 = map(float, input().split()) 3 | X2,Y2,R2 = map(float, input().split()) 4 | 5 | # 円の中心間距離を求める 6 | d = (((X1 - X2) ** 2 + (Y1 - Y2) ** 2) ** 0.5) 7 | 8 | # 答えを出力 9 | if d < abs(R1 - R2): 10 | print("1") 11 | elif d == abs(R1 - R2): 12 | print("2") 13 | elif d < R1 + R2: 14 | print("3") 15 | elif d == R1 + R2: 16 | print("4") 17 | else: 18 | print("5") -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double PI = 3.14159265358979; 5 | 6 | int main() { 7 | // 入力 8 | double A, B, H, M; 9 | scanf("%lf%lf%lf%lf", &A, &B, &H, &M); 10 | 11 | // 座標を求める 12 | double AngleH = 30.0 * H + 0.5 * M; 13 | double AngleM = 6.0 * M; 14 | double Hx = A * cos(AngleH * PI / 180.0), Hy = A * sin(AngleH * PI / 180.0); 15 | double Mx = B * cos(AngleM * PI / 180.0), My = B * sin(AngleM * PI / 180.0); 16 | 17 | // 距離を求める → 出力 18 | double d = sqrt((Hx - Mx) * (Hx - Mx) + (Hy - My) * (Hy - My)); 19 | printf("%.12lf\n", d); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const double PI = 3.14159265358979; 6 | 7 | int main() { 8 | // 入力 9 | double A, B, H, M; 10 | cin >> A >> B >> H >> M; 11 | 12 | // 座標を求める 13 | double AngleH = 30.0 * H + 0.5 * M; 14 | double AngleM = 6.0 * M; 15 | double Hx = A * cos(AngleH * PI / 180.0), Hy = A * sin(AngleH * PI / 180.0); 16 | double Mx = B * cos(AngleM * PI / 180.0), My = B * sin(AngleM * PI / 180.0); 17 | 18 | // 距離を求める → 出力 19 | double d = sqrt((Hx - Mx) * (Hx - Mx) + (Hy - My) * (Hy - My)); 20 | printf("%.12lf\n", d); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /editorial/chap4-1/prob4-1-4.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # 入力 4 | A, B, H, M = map(float, input().split()) 5 | 6 | # 座標を求める 7 | PI = 3.14159265358979 8 | AngleH = 30.0 * H + 0.5 * M 9 | AngleM = 6.0 * M 10 | Hx = A * math.cos(AngleH * PI / 180.0) 11 | Hy = A * math.sin(AngleH * PI / 180.0) 12 | Mx = B * math.cos(AngleM * PI / 180.0) 13 | My = B * math.sin(AngleM * PI / 180.0) 14 | 15 | # 答えを出力 16 | d = (((Hx - Mx) ** 2 + (Hy - My) ** 2) ** 0.5) 17 | print("%.12f" % d) -------------------------------------------------------------------------------- /editorial/chap4-2/chap4-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-2/chap4-2.pdf -------------------------------------------------------------------------------- /editorial/chap4-2/prob4-2-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N, A[200009], S[200009]; 4 | long long M, B[200009]; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%lld", &N); 9 | for (int i = 1; i <= N - 1; i++) scanf("%lld", &A[i]); 10 | scanf("%lld", &M); 11 | for (int i = 1; i <= M; i++) scanf("%lld", &B[i]); 12 | 13 | // 累積和をとる 14 | S[1] = 0; 15 | for (int i = 2; i <= N; i++) S[i] = S[i - 1] + A[i - 1]; 16 | 17 | // 答えを求める 18 | long long Answer = 0; 19 | for (int i = 1; i <= M - 1; i++) { 20 | if (B[i] < B[i + 1]) { 21 | Answer += (S[B[i + 1]] - S[B[i]]); 22 | } 23 | else { 24 | Answer += (S[B[i]] - S[B[i + 1]]); 25 | } 26 | } 27 | 28 | // 出力 29 | printf("%lld\n", Answer); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /editorial/chap4-2/prob4-2-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, A[200009], S[200009]; 5 | long long M, B[200009]; 6 | 7 | int main() { 8 | // 入力 9 | cin >> N; 10 | for (int i = 1; i <= N - 1; i++) cin >> A[i]; 11 | cin >> M; 12 | for (int i = 1; i <= M; i++) cin >> B[i]; 13 | 14 | // 累積和をとる 15 | S[1] = 0; 16 | for (int i = 2; i <= N; i++) S[i] = S[i - 1] + A[i - 1]; 17 | 18 | // 答えを求める 19 | long long Answer = 0; 20 | for (int i = 1; i <= M - 1; i++) { 21 | if (B[i] < B[i + 1]) { 22 | Answer += (S[B[i + 1]] - S[B[i]]); 23 | } 24 | else { 25 | Answer += (S[B[i]] - S[B[i + 1]]); 26 | } 27 | } 28 | 29 | // 出力 30 | cout << Answer << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /editorial/chap4-2/prob4-2-1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | M = int(input()) 5 | B = [0 for i in range(M)] 6 | for i in range(M): 7 | B[i] = int(input()) 8 | 9 | # 累積和をとる 10 | S = [0 for i in range(N)] 11 | for i in range(1, N): 12 | S[i] = S[i - 1] + A[i - 1] 13 | 14 | # 答えを求める 15 | # B[i] - 1 などになっているのは、配列の index が 0 始まりだから 16 | Answer = 0 17 | for i in range(M-1): 18 | if B[i] < B[i + 1]: 19 | Answer += S[B[i + 1] - 1] - S[B[i] - 1] 20 | else: 21 | Answer += S[B[i] - 1] - S[B[i + 1] - 1] 22 | 23 | # 出力 24 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap4-2/prob4-2-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, T; 4 | int L[500009], R[500009]; 5 | int A[500009], B[500009]; 6 | 7 | int main() { 8 | // 入力 9 | scanf("%d%d", &T, &N); 10 | for (int i = 1; i <= N; i++) scanf("%d%d", &L[i], &R[i]); 11 | 12 | // 階差 B[i] を計算する 13 | for (int i = 0; i <= T; i++) B[i] = 0; 14 | for (int i = 1; i <= N; i++) { 15 | B[L[i]] += 1; 16 | B[R[i]] -= 1; 17 | } 18 | 19 | // 累積和 A[i] を計算する 20 | A[0] = B[0]; 21 | for (int i = 1; i <= T; i++) { 22 | A[i] = A[i - 1] + B[i]; 23 | } 24 | 25 | // 答えを出力する 26 | for (int i = 0; i < T; i++) printf("%d\n", A[i]); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /editorial/chap4-2/prob4-2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, T; 5 | int L[500009], R[500009]; 6 | int A[500009], B[500009]; 7 | 8 | int main() { 9 | // 入力 10 | cin >> T >> N; 11 | for (int i = 1; i <= N; i++) cin >> L[i] >> R[i]; 12 | 13 | // 階差 B[i] を計算する 14 | for (int i = 0; i <= T; i++) B[i] = 0; 15 | for (int i = 1; i <= N; i++) { 16 | B[L[i]] += 1; 17 | B[R[i]] -= 1; 18 | } 19 | 20 | // 累積和 A[i] を計算する 21 | A[0] = B[0]; 22 | for (int i = 1; i <= T; i++) { 23 | A[i] = A[i - 1] + B[i]; 24 | } 25 | 26 | // 答えを出力する 27 | for (int i = 0; i < T; i++) cout << A[i] << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /editorial/chap4-2/prob4-2-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | T = int(input()) 3 | N = int(input()) 4 | A = [0 for i in range(T + 2)] 5 | B = [0 for i in range(T + 2)] 6 | L = [0 for i in range(N)] 7 | R = [0 for i in range(N)] 8 | for i in range(N): 9 | L[i], R[i] = map(int, input().split()) 10 | 11 | # 階差 B[i] を計算する 12 | for i in range(N): 13 | B[L[i]] += 1 14 | B[R[i]] -= 1 15 | 16 | # 累積和 A[i] を計算する 17 | A[0] = B[0] 18 | for i in range(1, T): 19 | A[i] = A[i - 1] + B[i] 20 | 21 | # 出力 22 | for i in range(T): 23 | print(A[i]) -------------------------------------------------------------------------------- /editorial/chap4-3/chap4-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-3/chap4-3.pdf -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double r = 2.0; // √2 を求めたいから 5 | double a = 2.0; // 初期値を適当に 2.0 にセットする 6 | 7 | for (int i = 1; i <= 5; i++) { 8 | // 点 (a, f(a)) の x 座標と y 座標を求める 9 | double zahyou_x = a; 10 | double zahyou_y = a * a * a; 11 | 12 | // 接線の傾きを求める [y = (sessen_a)x + sessen_b とする] 13 | double sessen_a = 3.0 * zahyou_x * zahyou_x; 14 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 15 | 16 | // 次の a の値 next_a を求める 17 | double next_a = (r - sessen_b) / sessen_a; 18 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double r = 2.0; // √2 を求めたいから 6 | double a = 2.0; // 初期値を適当に 2.0 にセットする 7 | 8 | for (int i = 1; i <= 5; i++) { 9 | // 点 (a, f(a)) の x 座標と y 座標を求める 10 | double zahyou_x = a; 11 | double zahyou_y = a * a * a; 12 | 13 | // 接線の傾きを求める [y = (sessen_a)x + sessen_b とする] 14 | double sessen_a = 3.0 * zahyou_x * zahyou_x; 15 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 16 | 17 | // 次の a の値 next_a を求める 18 | double next_a = (r - sessen_b) / sessen_a; 19 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 20 | a = next_a; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | double r = 2.0; // √2 を求めたいから 6 | double a = 2.0; // 初期値を適当に 2.0 にセットする 7 | for (int i = 1; i <= 5; i++) { 8 | // 点 (a, f(a)) の x 座標と y 座標を求める 9 | double zahyou_x = a; 10 | double zahyou_y = a * a * a; 11 | 12 | // 接線の式 y = sessen_a * x + sessen_b を求める 13 | double sessen_a = 3.0 * zahyou_x * zahyou_x; 14 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 15 | 16 | // 次の a の値 next_a を求める 17 | double next_a = (r - sessen_b) / sessen_a; 18 | System.out.format("Step #%d: a = %.12f -> %.12f\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-2.py: -------------------------------------------------------------------------------- 1 | r = 2.0 # √2 を求めたいから 2 | a = 2.0 # 初期値を適当に 2.0 にセットする 3 | repeats = 5 4 | 5 | for i in range(1, repeats + 1): 6 | # 点 (a, f(a)) の x 座標と y 座標を求める 7 | zahyou_x, zahyou_y = a, a * a * a 8 | 9 | # 接線の式 y = sessen_a * x + sessen_b を求める 10 | sessen_a = 3.0 * zahyou_x * zahyou_x 11 | sessen_b = zahyou_y - sessen_a * zahyou_x 12 | 13 | # 次の a の値 next_a を求める 14 | next_a = (r - sessen_b) / sessen_a 15 | print("Step #%d: a = %.12f -> %.12f" % (i, a, next_a)) 16 | a = next_a -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double l = 1.0; 5 | double r = 2.0; 6 | 7 | for (int i = 1; i <= 20; i++) { 8 | double m = (l + r) / 2.0; 9 | if (m * m < 2.0) l = m; 10 | else r = m; 11 | printf("Step #%d: m = %.12lf\n", i, m); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double l = 1.0; 6 | double r = 2.0; 7 | 8 | for (int i = 1; i <= 20; i++) { 9 | double m = (l + r) / 2.0; 10 | if (m * m < 2.0) l = m; 11 | else r = m; 12 | printf("Step #%d: m = %.12lf¥n", i, m); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | double l = 1.0; 6 | double r = 2.0; 7 | for (int i = 1; i <= 20; i++) { 8 | double m = (l + r) / 2.0; 9 | if (m * m < 2.0) l = m; 10 | else r = m; 11 | System.out.format("Step #%d: m = %.12f\n", i, m); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /editorial/chap4-3/prob4-3-3.py: -------------------------------------------------------------------------------- 1 | l = 1.0 2 | r = 2.0 3 | repeats = 20 4 | 5 | for i in range(1, repeats + 1): 6 | m = (l + r) / 2.0 7 | if m * m < 2.0: 8 | l = m 9 | else: 10 | r = m 11 | print("Step #%d: m = %.12f" % (i, m)) -------------------------------------------------------------------------------- /editorial/chap4-4/chap4-4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-4/chap4-4.pdf -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int N = 1000; 6 | double Answer = 0.0; 7 | 8 | for (int i = 0; i < N; i++) { 9 | double x = 1.0 * (2 * i + 1) / (2 * N); 10 | double value = pow(2.0, x * x); // f((2i+1)/2N) の値 11 | Answer += value; 12 | } 13 | printf("%.14lf\n", Answer / N); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int N = 1000; 7 | double Answer = 0.0; 8 | 9 | for (int i = 0; i < N; i++) { 10 | double x = 1.0 * (2 * i + 1) / (2 * N); 11 | double value = pow(2.0, x * x); // f((2i+1)/2N) の値 12 | Answer += value; 13 | } 14 | printf("%.14lf\n", Answer / N); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | int N = 1000; 6 | double Answer = 0.0; 7 | 8 | for (int i = 0; i < N; i++) { 9 | double x = 1.0 * (2 * i + 1) / (2 * N); 10 | double value = Math.pow(2.0, x * x); // f((2i+1)/2N) の値 11 | Answer += value; 12 | } 13 | 14 | System.out.format("%.12f\n", Answer / N); 15 | } 16 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-2.py: -------------------------------------------------------------------------------- 1 | N = 1000 2 | Answer = 0.0 3 | 4 | for i in range(N): 5 | x = 1.0 * (2 * i + 1) / (2 * N) 6 | value = 2.0 ** (x * x) 7 | Answer += value 8 | 9 | print("%.12f" % (Answer / N)) -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N; 4 | long long F[10000009]; 5 | long long Answer = 0; 6 | 7 | int main() { 8 | // 入力 → 配列の初期化 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) F[i] = 0; 11 | 12 | // F[1], F[2], ..., F[N] を計算する 13 | for (int i = 1; i <= N; i++) { 14 | // F[i], F[2i], F[3i], ... に 1 を加算 15 | for (int j = i; j <= N; j += i) F[j] += 1; 16 | } 17 | 18 | // 答えを求める → 出力 19 | for (int i = 1; i <= N; i++) { 20 | Answer += 1LL * i * F[i]; 21 | } 22 | printf("%lld\n", Answer); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N; 5 | long long F[10000009]; 6 | long long Answer = 0; 7 | 8 | int main() { 9 | // 入力 → 配列の初期化 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) F[i] = 0; 12 | 13 | // F[1], F[2], ..., F[N] を計算する 14 | for (int i = 1; i <= N; i++) { 15 | // F[i], F[2i], F[3i], ... に 1 を加算 16 | for (int j = i; j <= N; j += i) F[j] += 1; 17 | } 18 | 19 | // 答えを求める → 出力 20 | for (int i = 1; i <= N; i++) { 21 | Answer += 1LL * i * F[i]; 22 | } 23 | cout << Answer << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 入力 → 配列の初期化 8 | int N = sc.nextInt(); 9 | long[] F = new long[N + 1]; 10 | for (int i = 1; i <= N; i++) F[i] = 0; 11 | 12 | // F[1], F[2], ..., F[N] を計算する 13 | for (int i = 1; i <= N; i++) { 14 | // F[i], F[2i], F[3i], ... に 1 を加算する 15 | for (int j = i; j <= N; j += i) F[j] += 1; 16 | } 17 | 18 | // 答えを求める 19 | long Answer = 0; 20 | for (int i = 1; i <= N; i++) { 21 | Answer += F[i] * i; 22 | } 23 | System.out.println(Answer); 24 | } 25 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-3.py: -------------------------------------------------------------------------------- 1 | # 重要な注意 2 | # Python は遅いので、このプログラムを提出しても、実行時間制限超過(TLE)となります。 3 | # このため、全く同じコードを「PyPy3 (7.3.0)」で提出してください。おそらく AC します。 4 | # 計算量の速い解法を知りたい人は「prob4-4-3_fast.py」をご覧ください。 5 | 6 | # 入力 7 | N = int(input()) 8 | F = [0 for i in range(N + 1)] 9 | 10 | # F[1], F[2], ..., F[N] を計算する 11 | for i in range(1, N + 1): 12 | # F[i], F[2i], F[3i], ... に 1 を加算 13 | for j in range(1, (N // i) + 1): 14 | F[j * i] += 1 15 | 16 | # 答えを求める 17 | Answer = 0 18 | for i in range(1, N + 1): 19 | Answer += i * F[i] 20 | 21 | # 出力 22 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-3_fast.py: -------------------------------------------------------------------------------- 1 | # この解法の計算量は O(N) です(5.7 節のテクニックを利用) 2 | # PyPy3 (7.3.0) だけでなく Python (3.8.2) でも AC します 3 | 4 | N = int(input()) 5 | Answer = 0 6 | 7 | for i in range(1, N+1): 8 | d = (N // i) 9 | Answer += i * (d * (d + 1) // 2) 10 | 11 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // パラメータの設定・初期化 5 | long long cnt = 0; 6 | double LIMIT = 23; // これを 30 にすれば答えが求められる 7 | double Current = 0; 8 | 9 | // 1 つずつ足していく 10 | while (Current < LIMIT) { 11 | cnt += 1; 12 | Current += 1.0 / cnt; 13 | } 14 | 15 | // 答えを出力 16 | printf("%lld\n", cnt); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // パラメータの設定・初期化 6 | long long cnt = 0; 7 | double LIMIT = 23; // これを 30 にすれば答えが求められる 8 | double Current = 0; 9 | 10 | // 1 つずつ足していく 11 | while (Current < LIMIT) { 12 | cnt += 1; 13 | Current += 1.0 / cnt; 14 | } 15 | 16 | // 答えを出力 17 | cout << cnt << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int cnt = 0; 7 | double LIMIT = 23; // これを 30 にすると答えが求められる 8 | double Current = 0; 9 | 10 | // 1 つずつ足していく 11 | while (Current < LIMIT) { 12 | cnt += 1; 13 | Current += 1.0 / cnt; 14 | } 15 | 16 | // 出力 17 | System.out.println(cnt); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /editorial/chap4-4/prob4-4-4.py: -------------------------------------------------------------------------------- 1 | cnt = 0 2 | LIMIT = 23 # これを 30 にすれば答えが求められる 3 | Current = 0 4 | 5 | # 1 つずつ足していく 6 | while Current < LIMIT: 7 | cnt += 1 8 | Current += 1.0 / cnt 9 | 10 | # 出力 11 | print(cnt) -------------------------------------------------------------------------------- /editorial/chap4-5/chap4-5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-5/chap4-5.pdf -------------------------------------------------------------------------------- /editorial/chap4-5/prob4-5-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, M; 6 | int A[100009], B[100009]; 7 | vector G[100009]; 8 | 9 | int main() { 10 | // 入力 11 | cin >> N >> M; 12 | for (int i = 1; i <= M; i++) { 13 | cin >> A[i] >> B[i]; 14 | G[A[i]].push_back(B[i]); 15 | G[B[i]].push_back(A[i]); 16 | } 17 | 18 | // 答えを求める 19 | int Answer = 0; 20 | for (int i = 1; i <= N; i++) { 21 | int cnt = 0; 22 | for (int j = 0; j < G[i].size(); j++) { 23 | // G[i][j] は頂点 i に隣接している頂点のうち j 個目 24 | if (G[i][j] < i) cnt += 1; 25 | } 26 | // 自分自身より小さい隣接頂点が 1 つであれば Answer に 1 を加算する 27 | if (cnt == 1) Answer += 1; 28 | } 29 | 30 | // 出力 31 | cout << Answer << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /editorial/chap4-5/prob4-5-5.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, M = map(int, input().split()) 3 | A = [ None ] * M 4 | B = [ None ] * M 5 | for i in range(M): 6 | A[i], B[i] = map(int, input().split()) 7 | 8 | # 隣接リストの作成 9 | G = [ list() for i in range(N + 1) ] 10 | for i in range(M): 11 | G[A[i]].append(B[i]) 12 | G[B[i]].append(A[i]) 13 | 14 | # 答えを求める 15 | answer = 0 16 | for i in range(1, N + 1): 17 | cnt = 0 18 | for j in G[i]: 19 | if j < i: 20 | cnt += 1 21 | # 自分自身より番号が小さい隣接頂点が 1 つであれば answer に 1 を加算する 22 | if cnt == 1: 23 | answer += 1 24 | 25 | # 答えを出力 26 | print(answer) 27 | -------------------------------------------------------------------------------- /editorial/chap4-6/chap4-6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-6/chap4-6.pdf -------------------------------------------------------------------------------- /editorial/chap4-6/prob4-6-3.py: -------------------------------------------------------------------------------- 1 | # 繰り返し二乗法(p は a**1, a**2, a**4, a**8, ... といった値をとる) 2 | def modpow(a, b, m): 3 | p = a 4 | answer = 1 5 | for i in range(60): 6 | if (b & (1 << i)) != 0: 7 | answer = (answer * p) % m 8 | p = (p * p) % m 9 | return answer 10 | 11 | # division(a, b, m) は a÷b mod m を返す関数 12 | def division(a, b, m): 13 | return (a * modpow(b, m - 2, m)) % m 14 | 15 | # 入力 16 | MOD = 1000000007 17 | N = int(input()) 18 | 19 | # 答えの計算 20 | V = modpow(4, N + 1, MOD) - 1 21 | Answer = division(V, 3, MOD) 22 | 23 | # 出力 24 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap4-7/chap4-7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap4-7/chap4-7.pdf -------------------------------------------------------------------------------- /editorial/chap5-10/chap5-10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-10/chap5-10.pdf -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 1000000007; 4 | long long N; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%lld", &N); 9 | 10 | // 答えを求める 11 | long long val = N * (N + 1) / 2; 12 | val %= mod; 13 | printf("%lld\n", val * val % mod); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 1000000007; 5 | long long N; 6 | 7 | int main() { 8 | // 入力 9 | cin >> N; 10 | 11 | // 答えを求める 12 | long long val = N * (N + 1) / 2; 13 | val %= mod; 14 | cout << val * val % mod << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 答えを求める 10 | long mod = 1000000007; 11 | long val = N * (N + 1) / 2; 12 | val %= mod; 13 | System.out.println(val * val % mod); 14 | } 15 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 答えを求める 5 | mod = 1000000007 6 | val = N * (N + 1) // 2 7 | print(val * val % mod) -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 998244353; 4 | long long A, B, C; 5 | 6 | int main() { 7 | // 入力 8 | scanf("%lld%lld%lld", &A, &B, &C); 9 | 10 | // 計算 11 | long long D = A * (A + 1) / 2; D %= mod; 12 | long long E = B * (B + 1) / 2; E %= mod; 13 | long long F = C * (C + 1) / 2; F %= mod; 14 | 15 | // 答えを出力 16 | // ここで (D * E * F) % mod にしても、途中で 10^27 を扱う可能性がある 17 | // そのため、long long 型でもオーバーフローすることに注意! 18 | printf("%lld\n", (D * E % mod) * F % mod); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 998244353; 5 | long long A, B, C; 6 | 7 | int main() { 8 | // 入力 9 | cin >> A >> B >> C; 10 | 11 | // 計算 12 | long long D = A * (A + 1) / 2; D %= mod; 13 | long long E = B * (B + 1) / 2; E %= mod; 14 | long long F = C * (C + 1) / 2; F %= mod; 15 | 16 | // 答えを出力 17 | // ここで (D * E * F) % mod にしても、途中で 10^27 を扱う可能性がある 18 | // そのため、long long 型でもオーバーフローすることに注意! 19 | cout << (D * E % mod) * F % mod << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long A = sc.nextLong(); 8 | long B = sc.nextLong(); 9 | long C = sc.nextLong(); 10 | 11 | // 計算 12 | long mod = 998244353; 13 | long D = A * (A + 1) / 2; D %= mod; 14 | long E = B * (B + 1) / 2; E %= mod; 15 | long F = C * (C + 1) / 2; F %= mod; 16 | 17 | // 答えを出力 18 | // ここで (D * E * F) % mod にしても、途中で 10^27 を扱う可能性がある 19 | // そのため、long long 型でもオーバーフローすることに注意! 20 | System.out.println((D * E % mod) * F % mod); 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | A, B, C = map(int, input().split()) 3 | 4 | # 計算 5 | mod = 998244353 6 | D = A * (A + 1) // 2 7 | E = B * (B + 1) // 2 8 | F = C * (C + 1) // 2 9 | 10 | # 答えを出力 11 | print((D * E * F) % mod) -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long a, b, c; 6 | scanf("%lld%lld%lld", &a, &b, &c); 7 | 8 | // c = 1 のときの場合分け 9 | if (c == 1) { 10 | printf("No\n"); 11 | return 0; 12 | } 13 | 14 | // 右辺の計算(c の b 乗) 15 | long long v = 1; 16 | for (long long i = 1; i <= b; i++) { 17 | if (a / c < v) { 18 | // この条件分岐は a < (v * c) を言い換えただけ 19 | // 条件の言い換えをした理由は、v, c が 10^18 程度になる可能性があるため 20 | // a < v * c にすると最悪の場合 v * c = 10^36 になりオーバーフローするから 21 | printf("Yes\n"); 22 | return 0; 23 | } 24 | v *= c; 25 | } 26 | 27 | // ループが打ち切られない場合 28 | printf("No\n"); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // 入力 7 | long long a, b, c; 8 | cin >> a >> b >> c; 9 | 10 | // c = 1 のときの場合分け 11 | if (c == 1) { 12 | cout << "No" << endl; 13 | return 0; 14 | } 15 | 16 | // 右辺の計算(c の b 乗) 17 | long long v = 1; 18 | for (long long i = 1; i <= b; i++) { 19 | if (a / c < v) { 20 | // この条件分岐は a < (v * c) を言い換えただけ 21 | // 条件の言い換えをした理由は、v, c が 10^18 程度になる可能性があるため 22 | // a < v * c にすると最悪の場合 v * c = 10^36 になりオーバーフローするから 23 | cout << "Yes" << endl; 24 | return 0; 25 | } 26 | v *= c; 27 | } 28 | 29 | // ループが打ち切られない場合 30 | cout << "No" << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /editorial/chap5-10/prob5-10-5.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # 入力 4 | a, b, c = map(int, input().split()) 5 | 6 | # c = 1 のときの場合分け 7 | if c == 1: 8 | print("No") 9 | sys.exit() 10 | 11 | # 右辺の計算(c の b 乗) 12 | v = 1 13 | for i in range(b): 14 | v *= c 15 | if a < v: 16 | print("Yes") 17 | sys.exit() 18 | 19 | # 出力(No の場合) 20 | print("No") -------------------------------------------------------------------------------- /editorial/chap5-2/chap5-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-2/chap5-2.pdf -------------------------------------------------------------------------------- /editorial/chap5-2/prob5-2-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // 入力 6 | long long N; 7 | scanf("%lld", &N); 8 | 9 | // N = 2^k-1 の形で表されるかどうかを調べる 10 | bool flag = false; 11 | for (int k = 1; k <= 60; k++) { 12 | if (N == (1LL << k) - 1LL) flag = true; 13 | } 14 | 15 | // 出力 16 | if (flag == true) printf("Second\n"); 17 | else printf("First\n"); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /editorial/chap5-2/prob5-2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 入力 6 | long long N; 7 | cin >> N; 8 | 9 | // N = 2^k-1 の形で表されるかどうかを調べる 10 | bool flag = false; 11 | for (int k = 1; k <= 60; k++) { 12 | if (N == (1LL << k) - 1LL) flag = true; 13 | } 14 | 15 | // 出力 16 | if (flag == true) cout << "Second" << endl; 17 | else cout << "First" << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /editorial/chap5-2/prob5-2-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // N = 2^k-1 の形で表されるかどうか調べる 10 | boolean flag = false; 11 | for (int i = 1; i <= 60; i++) { 12 | if (N == (1L << i) - 1) { 13 | flag = true; 14 | } 15 | } 16 | 17 | // 出力 18 | if (flag == true) System.out.println("Second"); 19 | else System.out.println("First"); 20 | } 21 | } -------------------------------------------------------------------------------- /editorial/chap5-2/prob5-2-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # N = 2^k-1 の形で表せるかどうかを調べる 5 | flag = False 6 | for i in range(1, 60): 7 | if N == (2 ** i) - 1: 8 | flag = True 9 | 10 | if flag == True: 11 | print("Second") 12 | else: 13 | print("First") -------------------------------------------------------------------------------- /editorial/chap5-2/prob5-2-3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # 入力 4 | N, K = map(int, input().split()) 5 | A = list(map(int, input().split())) 6 | 7 | # 配列の初期化 8 | First = [-1 for i in range(N+1)] 9 | Second = [-1 for i in range(N+1)] 10 | 11 | # 答えを求める 12 | # cur は現在いる町の番号 13 | cnt = 0 14 | cur = 1 15 | while True: 16 | # First, Second の更新 17 | if First[cur] == -1: 18 | First[cur] = cnt 19 | elif Second[cur] == -1: 20 | Second[cur] = cnt 21 | 22 | # K 回の移動後に町 cur にいるか判定 23 | if cnt == K: 24 | print(cur) 25 | sys.exit() 26 | elif Second[cur] != -1 and (K - First[cur]) % (Second[cur] - First[cur]) == 0: 27 | print(cur) 28 | sys.exit() 29 | 30 | cur = A[cur - 1] 31 | cnt += 1 -------------------------------------------------------------------------------- /editorial/chap5-3/chap5-3.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 5.3 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-3/chap5-3.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-3/chap5-3.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 各問題のプログラムのリンクは、以下の通りです。 10 | 11 | | 問題 | C++ | Python | JAVA | C | 12 | |:---:|:---:|:---:|:---:|:---:| 13 | | 節末問題 5.3.1 | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-3/prob5-3-1.cpp) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-3/prob5-3-1.py) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-3/prob5-3-1.java) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-3/prob5-3-1.c) | -------------------------------------------------------------------------------- /editorial/chap5-3/chap5-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-3/chap5-3.pdf -------------------------------------------------------------------------------- /editorial/chap5-3/prob5-3-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long H, W; 6 | scanf("%lld%lld", &H, &W); 7 | 8 | // 場合分け 9 | if (H == 1 || W == 1) { 10 | printf("1\n"); 11 | } 12 | else { 13 | printf("%lld\n", (H * W + 1LL) / 2LL); 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /editorial/chap5-3/prob5-3-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 入力 6 | long long H, W; 7 | cin >> H >> W; 8 | 9 | // 場合分け 10 | if (H == 1 || W == 1) { 11 | cout << "1" << endl; 12 | } 13 | else { 14 | cout << (H * W + 1) / 2 << endl; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /editorial/chap5-3/prob5-3-1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long H = sc.nextLong(); 8 | long W = sc.nextLong(); 9 | 10 | // 場合分け 11 | if (H == 1 || W == 1) { 12 | System.out.println(1); 13 | } 14 | else { 15 | System.out.println((H * W + 1) / 2); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /editorial/chap5-3/prob5-3-1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | H, W = map(int, input().split()) 3 | 4 | # 場合分け 5 | if H == 1 or W == 1: 6 | print(1) 7 | else: 8 | print((H * W + 1) // 2) -------------------------------------------------------------------------------- /editorial/chap5-4/chap5-4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-4/chap5-4.pdf -------------------------------------------------------------------------------- /editorial/chap5-4/prob5-4-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | H, W = map(int, input().split()) 3 | A = [[] for i in range(H)] 4 | for i in range(H): 5 | A[i] = list(map(int, input().split())) 6 | 7 | # 行の総和を計算する 8 | gyou = [0 for i in range(H)] 9 | for i in range(H): 10 | for j in range(W): 11 | gyou[i] += A[i][j] 12 | 13 | # 列の総和を計算する 14 | retu = [0 for i in range(W)] 15 | for j in range(W): 16 | for i in range(H): 17 | retu[j] += A[i][j] 18 | 19 | # 各マスに対する答えを計算する 20 | Answer = [[0 for j in range(W)] for i in range(H)] 21 | for i in range(H): 22 | for j in range(W): 23 | Answer[i][j] = gyou[i] + retu[j] - A[i][j] 24 | 25 | # 出力 26 | for i in range(H): 27 | print(*Answer[i]) -------------------------------------------------------------------------------- /editorial/chap5-5/chap5-5.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 5.5 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-5/chap5-5.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-5/chap5-5.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 各問題のプログラムのリンクは、以下の通りです。 10 | 11 | | 問題 | C++ | Python | JAVA | C | 12 | |:---:|:---:|:---:|:---:|:---:| 13 | | 節末問題 5.5.2 | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-5/prob5-5-2.cpp) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-5/prob5-5-2.py) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-5/prob5-5-2.java) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-5/prob5-5-2.c) | -------------------------------------------------------------------------------- /editorial/chap5-5/chap5-5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-5/chap5-5.pdf -------------------------------------------------------------------------------- /editorial/chap5-6/chap5-6.md: -------------------------------------------------------------------------------- 1 | ## 節末問題 5.6 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-6/chap5-6.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-6/chap5-6.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 各問題のプログラムのリンクは、以下の通りです。 10 | 11 | | 問題 | C++ | Python | JAVA | C | 12 | |:---:|:---:|:---:|:---:|:---:| 13 | | 節末問題 5.6.2 | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-6/prob5-6-2.cpp) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-6/prob5-6-2.py) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-6/prob5-6-2.java) | [リンク](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap5-6/prob5-6-2.c) | -------------------------------------------------------------------------------- /editorial/chap5-6/chap5-6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-6/chap5-6.pdf -------------------------------------------------------------------------------- /editorial/chap5-6/prob5-6-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 1000000007; 4 | long long N; 5 | long long A[300009]; 6 | long long power[300009]; 7 | 8 | int main() { 9 | // 入力 10 | scanf("%lld", &N); 11 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 12 | 13 | // 2^i を求める 14 | power[0] = 1; 15 | for (int i = 1; i <= N; i++) { 16 | power[i] = (2 * power[i - 1]) % mod; 17 | } 18 | 19 | // 答えを求める 20 | long long Answer = 0; 21 | for (int i = 1; i <= N; i++) { 22 | Answer += power[i - 1] * A[i]; 23 | Answer %= mod; 24 | } 25 | 26 | // 出力 27 | printf("%lld\n", Answer); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /editorial/chap5-6/prob5-6-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 1000000007; 5 | long long N; 6 | long long A[300009]; 7 | long long power[300009]; 8 | 9 | int main() { 10 | // 入力 11 | cin >> N; 12 | for (int i = 1; i <= N; i++) cin >> A[i]; 13 | 14 | // 2^i を求める 15 | power[0] = 1; 16 | for (int i = 1; i <= N; i++) { 17 | power[i] = (2 * power[i - 1]) % mod; 18 | } 19 | 20 | // 答えを求める 21 | long long Answer = 0; 22 | for (int i = 1; i <= N; i++) { 23 | Answer += power[i - 1] * A[i]; 24 | Answer %= mod; 25 | } 26 | 27 | // 出力 28 | cout << Answer << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /editorial/chap5-6/prob5-6-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] A = new long[N + 1]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextLong(); 11 | } 12 | 13 | // 2^i を求める 14 | final long mod = 1000000007; 15 | long[] power = new long[N + 1]; 16 | power[0] = 1; 17 | for (int i = 1; i <= N; i++) { 18 | power[i] = (2 * power[i - 1]) % mod; 19 | } 20 | 21 | // 答えを求める 22 | long Answer = 0; 23 | for (int i = 1; i <= N; i++) { 24 | Answer += power[i - 1] * A[i]; 25 | Answer %= mod; 26 | } 27 | 28 | // 出力 29 | System.out.println(Answer); 30 | } 31 | } -------------------------------------------------------------------------------- /editorial/chap5-6/prob5-6-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 2^i を求める 6 | mod = 1000000007 7 | power = [0 for i in range(N)] 8 | power[0] = 1 9 | for i in range(1, N): 10 | power[i] = (2 * power[i - 1]) % mod 11 | 12 | # 答えを求める 13 | Answer = 0 14 | for i in range(N): 15 | Answer += power[i] * A[i] 16 | Answer %= mod 17 | 18 | # 出力 19 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap5-7/chap5-7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-7/chap5-7.pdf -------------------------------------------------------------------------------- /editorial/chap5-7/prob5-7-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int compare_values(const void* a, const void* b) { 5 | // a の方が前に来るとき 負の値 6 | // b の方が前に来るとき 正の値 7 | // a と b が等しい値のとき 0 を返す関数 8 | if (*(int*)a < *(int*)b) return -1; 9 | if (*(int*)a > *(int*)b) return +1; 10 | return 0; 11 | } 12 | 13 | long long N, A[200009]; 14 | long long Answer = 0; 15 | 16 | int main() { 17 | // 入力 18 | scanf("%lld", &N); 19 | for (int i = 1; i <= N; i++) scanf("%lld\n", &A[i]); 20 | 21 | // ソート(コード 5.7.1 から追加した唯一の部分) 22 | qsort(A + 1, N, sizeof(long long), compare_values); 23 | 24 | // 答えを求める → 答えの出力 25 | for (int i = 1; i <= N; i++) Answer += A[i] * (-N + 2LL * i - 1LL); 26 | printf("%lld\n", Answer); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /editorial/chap5-7/prob5-7-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N, A[200009]; 6 | long long Answer = 0; 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // ソート(コード 5.7.1 から追加した唯一の部分) 14 | sort(A + 1, A + N + 1); 15 | 16 | // 答えを求める → 答えの出力 17 | for (int i = 1; i <= N; i++) Answer += A[i] * (-N + 2LL * i - 1LL); 18 | cout << Answer << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /editorial/chap5-7/prob5-7-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] A = new long[N]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i - 1] = sc.nextLong(); 11 | } 12 | 13 | // 配列 A 全体をソートする 14 | Arrays.sort(A); 15 | 16 | // 答えを求める 17 | long Answer = 0; 18 | for (int i = 1; i <= N; i++) { 19 | Answer += A[i - 1] * (-N + 2L * i - 1L); 20 | } 21 | 22 | // 出力 23 | System.out.println(Answer); 24 | } 25 | } -------------------------------------------------------------------------------- /editorial/chap5-7/prob5-7-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 配列 A 全体をソートする 6 | A.sort() 7 | 8 | # 答えを求める 9 | Answer = 0 10 | for i in range(1, N + 1): 11 | Answer += A[i - 1] * (-N + 2 * i - 1) 12 | 13 | # 出力 14 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap5-7/prob5-7-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N; 6 | long long X[200009], Y[200009]; 7 | 8 | int main() { 9 | // 入力 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> X[i] >> Y[i]; 12 | 13 | // 配列をソートする 14 | sort(X + 1, X + N + 1); 15 | sort(Y + 1, Y + N + 1); 16 | 17 | // パーツ 1 の答え(x 座標の差の絶対値の総和) 18 | long long Part1 = 0; 19 | for (int i = 1; i <= N; i++) Part1 += X[i] * (-N + 2LL * i - 1LL); 20 | 21 | // パーツ 2 の答え(y 座標の差の絶対値の総和) 22 | long long Part2 = 0; 23 | for (int i = 1; i <= N; i++) Part2 += Y[i] * (-N + 2LL * i - 1LL); 24 | 25 | // 出力 26 | cout << Part1 + Part2 << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /editorial/chap5-7/prob5-7-4.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | X = [0 for i in range(N)] 4 | Y = [0 for i in range(N)] 5 | for i in range(N): 6 | X[i], Y[i] = map(int, input().split()) 7 | 8 | # 配列 X, Y をソートする 9 | X.sort() 10 | Y.sort() 11 | 12 | # パーツ 1 の答え(x 座標の差の絶対値の総和) 13 | Part1 = 0 14 | for i in range(1, N + 1): 15 | Part1 += X[i - 1] * (-N + 2 * i - 1); 16 | 17 | # パーツ 2 の答え(y 座標の差の絶対値の総和) 18 | Part2 = 0 19 | for i in range(1, N + 1): 20 | Part2 += Y[i - 1] * (-N + 2 * i - 1); 21 | 22 | # 出力 23 | print(Part1 + Part2) -------------------------------------------------------------------------------- /editorial/chap5-8/chap5-8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-8/chap5-8.pdf -------------------------------------------------------------------------------- /editorial/chap5-8/prob5-8-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long N; 6 | scanf("%lld", &N); 7 | 8 | // 出力 9 | printf("%lld\n", N * (N - 1) / 2); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /editorial/chap5-8/prob5-8-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 入力 6 | long long N; 7 | cin >> N; 8 | 9 | // 出力 10 | cout << N * (N - 1) / 2 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /editorial/chap5-8/prob5-8-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 出力 10 | System.out.println(N * (N - 1) / 2); 11 | } 12 | } -------------------------------------------------------------------------------- /editorial/chap5-8/prob5-8-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 出力 5 | print(N * (N - 1) // 2) -------------------------------------------------------------------------------- /editorial/chap5-9/chap5-9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap5-9/chap5-9.pdf -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long N, Answer = 0; 6 | scanf("%lld", &N); 7 | 8 | // 10000 円札を支払う 9 | Answer += (N / 10000); N %= 10000; 10 | 11 | // 5000 円札を支払う 12 | Answer += (N / 5000); N %= 5000; 13 | 14 | // 1000 円札を支払う 15 | Answer += (N / 1000); N %= 1000; 16 | 17 | // 答えを出力 18 | printf("%lld\n", Answer); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 入力 6 | long long N, Answer = 0; 7 | cin >> N; 8 | 9 | // 10000 円札を支払う 10 | Answer += (N / 10000); N %= 10000; 11 | 12 | // 5000 円札を支払う 13 | Answer += (N / 5000); N %= 5000; 14 | 15 | // 1000 円札を支払う 16 | Answer += (N / 1000); N %= 1000; 17 | 18 | // 答えを出力 19 | cout << Answer << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | long Answer = 0; 9 | 10 | // 10000 円札を支払う 11 | Answer += (N / 10000); N %= 10000; 12 | 13 | // 5000 円札を支払う 14 | Answer += (N / 5000); N %= 5000; 15 | 16 | // 1000 円札を支払う 17 | Answer += (N / 1000); N %= 1000; 18 | 19 | // 出力 20 | System.out.println(Answer); 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-1.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | Answer = 0 4 | 5 | # 10000 円札を支払う 6 | Answer += (N // 10000) 7 | N %= 10000 8 | 9 | # 5000 円札を支払う 10 | Answer += (N // 5000) 11 | N %= 5000 12 | 13 | # 1000 円札を支払う 14 | Answer += (N // 1000) 15 | N %= 1000 16 | 17 | # 出力 18 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | long long N; 7 | long long A[100009], B[100009]; 8 | 9 | int main() { 10 | // 入力 11 | cin >> N; 12 | for (int i = 1; i <= N; i++) cin >> A[i]; 13 | for (int i = 1; i <= N; i++) cin >> B[i]; 14 | 15 | // ソート 16 | sort(A + 1, A + N + 1); 17 | sort(B + 1, B + N + 1); 18 | 19 | // 答えを求める 20 | long long Answer = 0; 21 | for (int i = 1; i <= N; i++) Answer += abs(A[i] - B[i]); 22 | cout << Answer << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] A = new long[N]; 9 | long[] B = new long[N]; 10 | for (int i = 1; i <= N; i++) A[i - 1] = sc.nextLong(); 11 | for (int i = 1; i <= N; i++) B[i - 1] = sc.nextLong(); 12 | 13 | // 配列 A, B をソートする 14 | Arrays.sort(A); 15 | Arrays.sort(B); 16 | 17 | // 答えを求める 18 | long Answer = 0; 19 | for (int i = 1; i <= N; i++) { 20 | Answer += Math.abs(A[i - 1] - B[i - 1]); 21 | } 22 | 23 | // 出力 24 | System.out.println(Answer); 25 | } 26 | } -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-2.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | B = list(map(int, input().split())) 5 | 6 | # 配列 A, B をソートする 7 | A.sort() 8 | B.sort() 9 | 10 | # 答えを求める 11 | Answer = 0 12 | for i in range(N): 13 | Answer += abs(A[i] - B[i]) 14 | 15 | # 出力 16 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap5-9/prob5-9-3.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | # A は (終了時刻, 開始時刻) になっていることに注意 [終了時刻の昇順にソートするため] 3 | N = int(input()) 4 | A = [] 5 | for i in range(N): 6 | a, b = map(int, input().split()) 7 | A.append([b, a]) 8 | 9 | # ソート 10 | A.sort() 11 | 12 | # 終了時刻が最も早いものを選び続ける 13 | CurrentTime = 0 14 | Answer = 0 15 | for i in range(N): 16 | if CurrentTime <= A[i][1]: 17 | CurrentTime = A[i][0] 18 | Answer += 1 19 | 20 | # 出力 21 | print(Answer) -------------------------------------------------------------------------------- /editorial/chap6-01_05/chap6-01_05.md: -------------------------------------------------------------------------------- 1 | ## 最終確認問題 1~5 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap6-01_05/chap6-01_05.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap6-01_05/chap6-01_05.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 最終確認問題 1~5 には、プログラムを書く問題はありません。 -------------------------------------------------------------------------------- /editorial/chap6-01_05/chap6-01_05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-01_05/chap6-01_05.pdf -------------------------------------------------------------------------------- /editorial/chap6-06_10/chap6-06_10.md: -------------------------------------------------------------------------------- 1 | ## 最終確認問題 6~10 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap6-06_10/chap6-06_10.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap6-06_10/chap6-06_10.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 最終確認問題 6~10 には、プログラムを書く問題はありません。 -------------------------------------------------------------------------------- /editorial/chap6-06_10/chap6-06_10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-06_10/chap6-06_10.pdf -------------------------------------------------------------------------------- /editorial/chap6-11_15/chap6-11_15.md: -------------------------------------------------------------------------------- 1 | ## 最終確認問題 11~15 の解答 2 | 3 | 解答 PDF:[https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap6-11_15/chap6-11_15.pdf](https://github.com/E869120/math-algorithm-book/blob/main/editorial/chap6-11_15/chap6-11_15.pdf) 4 | 5 |
6 | 7 | ### ソースコード 8 | 9 | 最終確認問題 11~15 には、プログラムを書く問題はありません。 -------------------------------------------------------------------------------- /editorial/chap6-11_15/chap6-11_15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-11_15/chap6-11_15.pdf -------------------------------------------------------------------------------- /editorial/chap6-16_20/chap6-16_20.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-16_20/chap6-16_20.pdf -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | int N, X; 6 | scanf("%d%d", &N, &X); 7 | 8 | // すべての (a, b, c) の組み合わせを試す 9 | int answer = 0; 10 | int a, b, c; 11 | for (a = 1; a <= N; a++) { 12 | for (b = a + 1; b <= N; b++) { 13 | for (c = b + 1; c <= N; c++) { 14 | if (a + b + c == X) { 15 | answer += 1; 16 | } 17 | } 18 | } 19 | } 20 | 21 | // 答えを出力 22 | printf("%d\n", answer); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-16.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-16_20/prob6-16.cpp -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-16.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int X = sc.nextInt(); 9 | 10 | // すべての (a, b, c) の組み合わせを試す 11 | int answer = 0; 12 | for (int a = 1; a <= N; a++) { 13 | for (int b = a + 1; b <= N; b++) { 14 | for (int c = b + 1; c <= N; c++) { 15 | if (a + b + c == X) { 16 | answer += 1; 17 | } 18 | } 19 | } 20 | } 21 | 22 | // 答えの出力 23 | System.out.println(answer); 24 | } 25 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-16.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N, X = map(int, input().split()) 3 | 4 | # すべての (a, b, c) の組み合わせを試す 5 | answer = 0 6 | for a in range(1, N + 1): 7 | for b in range(a + 1, N + 1): 8 | for c in range(b + 1, N + 1): 9 | if a + b + c == X: 10 | answer += 1 11 | 12 | # 答えの出力 13 | print(answer) -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 入力 5 | long long N; 6 | scanf("%lld", &N); 7 | 8 | // 縦の長さを 1 から √N まで全探索 9 | long long answer = (1LL << 60); 10 | long long x; 11 | for (x = 1; x * x <= N; x++) { 12 | if (N % x == 0 && answer > 2 * x + 2 * (N / x)) { 13 | answer = 2 * x + 2 * (N / x); 14 | } 15 | } 16 | 17 | // 答えを出力 18 | printf("%lld\n", answer); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-17.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-16_20/prob6-17.cpp -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-17.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 縦の長さを 1 から √N まで全探索 10 | long answer = (1L << 60); 11 | for (long x = 1; x * x <= N; x++) { 12 | if (N % x == 0) { 13 | answer = Math.min(answer, 2 * x + 2 * (N / x)); 14 | } 15 | } 16 | 17 | // 答えの出力 18 | System.out.println(answer); 19 | } 20 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-17.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | 4 | # 縦の長さを 1 から √N まで全探索 5 | LIMIT = int(N ** 0.5) 6 | answer = 10 ** 19 7 | for i in range(1, LIMIT + 1): 8 | if N % i == 0: 9 | answer = min(answer, 2 * i + 2 * (N // i)) 10 | 11 | # 答えの出力 12 | print(answer) -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long GCD(long long A, long long B) { 4 | if (B == 0) return A; 5 | return GCD(B, A % B); 6 | } 7 | 8 | int main() { 9 | // 入力 10 | long long A, B; 11 | scanf("%lld%lld", &A, &B); 12 | 13 | // 最小公倍数が 10^18 を超えるかどうか判定 14 | if (A / GCD(A, B) > 1000000000000000000 / B) { 15 | printf("Large\n"); 16 | } 17 | else { 18 | printf("%lld\n", A / GCD(A, B) * B); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-18.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-16_20/prob6-18.cpp -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-18.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | long A = sc.nextLong(); 8 | long B = sc.nextLong(); 9 | 10 | // 最小公倍数が 10^18 を超えるか判定 11 | if (A / GCD(A, B) > 1000000000000000000L / B) { 12 | System.out.println("Large"); 13 | } 14 | else { 15 | System.out.println(A / GCD(A, B) * B); 16 | } 17 | } 18 | static long GCD(long A, long B) { 19 | if (B == 0) return A; 20 | return GCD(B, A % B); 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-18.py: -------------------------------------------------------------------------------- 1 | # ユークリッドの互除法を使って最大公約数を求めます(別の方法として、標準ライブラリ math.gcd(A, B) を使う手もあります) 2 | def GCD(A, B): 3 | if B == 0: 4 | return A 5 | return GCD(B, A % B) 6 | 7 | # 入力 8 | A, B = map(int, input().split()) 9 | 10 | # 最小公倍数を求める(注:Python ではオーバーフローを気にする必要はありません) 11 | L = A * B // GCD(A, B) 12 | if L > 10 ** 18: 13 | print("Large") 14 | else: 15 | print(L) -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-19.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, B[109]; 4 | 5 | int main() { 6 | // 入力 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N - 1; i++) { 10 | scanf("%d", &B[i]); 11 | } 12 | 13 | // 数列 A の要素の合計を求める 14 | int answer = B[1] + B[N - 1]; 15 | for (i = 2; i <= N - 1; i++) { 16 | answer += (B[i - 1] < B[i] ? B[i - 1] : B[i]); 17 | } 18 | 19 | // 答えの出力 20 | printf("%d\n", answer); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-19.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-16_20/prob6-19.cpp -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-19.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 入力 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] B = new int[N]; 9 | for (int i = 1; i <= N - 1; i++) { 10 | B[i] = sc.nextInt(); 11 | } 12 | 13 | // 数列 A の要素の合計を求める 14 | int answer = B[1] + B[N - 1]; 15 | for (int i = 2; i <= N - 1; i++) { 16 | answer += Math.min(B[i - 1], B[i]); 17 | } 18 | 19 | // 答えの出力 20 | System.out.println(answer); 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-19.py: -------------------------------------------------------------------------------- 1 | # 入力(配列 B の添え字が 0 始まりなことに注意してください) 2 | N = int(input()) 3 | B = list(map(int, input().split())) 4 | 5 | # 数列 A の要素の合計を求める 6 | answer = B[0] + B[N - 2] 7 | for i in range(1, N - 1): 8 | answer += min(B[i - 1], B[i]) 9 | 10 | # 答えの出力 11 | print(answer) -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-20.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, Q, C[100009], P[100009], L[100009], R[100009], S1[100009], S2[100009]; 4 | 5 | int main() { 6 | // 入力 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%d%d", &C[i], &P[i]); 11 | } 12 | 13 | // 累積和を求める 14 | for (i = 1; i <= N; i++) { 15 | S1[i] = S1[i - 1] + (C[i] == 1 ? P[i] : 0); 16 | } 17 | for (i = 1; i <= N; i++) { 18 | S2[i] = S2[i - 1] + (C[i] == 2 ? P[i] : 0); 19 | } 20 | 21 | // 質問に答える 22 | scanf("%d", &Q); 23 | for (i = 1; i <= Q; i++) { 24 | scanf("%d%d", &L[i], &R[i]); 25 | printf("%d %d\n", S1[R[i]] - S1[L[i] - 1], S2[R[i]] - S2[L[i] - 1]); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-20.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-16_20/prob6-20.cpp -------------------------------------------------------------------------------- /editorial/chap6-16_20/prob6-20.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | C = [ None ] * (N + 1) 4 | P = [ None ] * (N + 1) 5 | for i in range(1, N + 1): 6 | C[i], P[i] = map(int, input().split()) 7 | 8 | # 累積和を求める 9 | S1 = [ None ] * (N + 1) 10 | S1[0] = 0 11 | for i in range(1, N + 1): 12 | S1[i] = S1[i - 1] + (P[i] if C[i] == 1 else 0) 13 | S2 = [ None ] * (N + 1) 14 | S2[0] = 0 15 | for i in range(1, N + 1): 16 | S2[i] = S2[i - 1] + (P[i] if C[i] == 2 else 0) 17 | 18 | # 質問に答える 19 | Q = int(input()) 20 | for i in range(Q): 21 | L, R = map(int, input().split()) 22 | print(S1[R] - S1[L - 1], S2[R] - S2[L - 1]) -------------------------------------------------------------------------------- /editorial/chap6-21_25/chap6-21_25.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-21_25/chap6-21_25.pdf -------------------------------------------------------------------------------- /editorial/chap6-21_25/prob6-21.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | double r = 2.0; // y = e^x と y = 2 の交点を求めたいから 6 | double a = 1.0; // 初期値を適当に 1.0 にセットする 7 | int i; 8 | for (i = 1; i <= 5; i++) { 9 | // 点 (a, f(a)) の x 座標と y 座標を求める 10 | double zahyou_x = a; 11 | double zahyou_y = exp(a); 12 | 13 | // 接線の傾きを求める [y = (sessen_a)x + sessen_b とする] 14 | double sessen_a = zahyou_y; 15 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 16 | 17 | // 次の a の値 next_a を求める 18 | double next_a = (r - sessen_b) / sessen_a; 19 | printf("Step #%d: a = %.15lf -> %.15lf\n", i, a, next_a); 20 | a = next_a; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /editorial/chap6-21_25/prob6-21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | double r = 2.0; // y = e^x と y = 2 の交点を求めたいから 7 | double a = 1.0; // 初期値を適当に 1.0 にセットする 8 | 9 | for (int i = 1; i <= 5; i++) { 10 | // 点 (a, f(a)) の x 座標と y 座標を求める 11 | double zahyou_x = a; 12 | double zahyou_y = exp(a); 13 | 14 | // 接線の式 y = sessen_a * x + sessen_b を求める 15 | double sessen_a = zahyou_y; 16 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 17 | 18 | // 次の a の値 next_a を求める 19 | double next_a = (r - sessen_b) / sessen_a; 20 | printf("Step #%d: a = %.15lf -> %.15lf\n", i, a, next_a); 21 | a = next_a; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /editorial/chap6-21_25/prob6-21.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | double r = 2.0; // y = e^x と y = 2 の交点を求めたいから 6 | double a = 1.0; // 初期値を適当に 2.0 にセットする 7 | for (int i = 1; i <= 5; i++) { 8 | // 点 (a, f(a)) の x 座標と y 座標を求める 9 | double zahyou_x = a; 10 | double zahyou_y = Math.exp(a); 11 | 12 | // 接線の式 y = sessen_a * x + sessen_b を求める 13 | double sessen_a = zahyou_y; 14 | double sessen_b = zahyou_y - sessen_a * zahyou_x; 15 | 16 | // 次の a の値 next_a を求める 17 | double next_a = (r - sessen_b) / sessen_a; 18 | System.out.format("Step #%d: a = %.15f -> %.15f\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /editorial/chap6-21_25/prob6-21.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | r = 2.0 # y = e^x と y = 2 の交点を求めたいから 4 | a = 1.0 # 初期値を適当に 1.0 にセットする 5 | repeats = 5 6 | 7 | for i in range(1, repeats + 1): 8 | # 点 (a, f(a)) の x 座標と y 座標を求める 9 | zahyou_x, zahyou_y = a, math.exp(a) 10 | 11 | # 接線の式 y = sessen_a * x + sessen_b を求める 12 | sessen_a = zahyou_y 13 | sessen_b = zahyou_y - sessen_a * zahyou_x 14 | 15 | # 次の a の値 next_a を求める 16 | next_a = (r - sessen_b) / sessen_a 17 | print("Step #%d: a = %.15f -> %.15f" % (i, a, next_a)) 18 | a = next_a -------------------------------------------------------------------------------- /editorial/chap6-21_25/prob6-23.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | L, R = map(int, input().split()) 3 | 4 | # 配列の初期化・L=1 のときの場合分け 5 | isprime = [ True ] * (R - L + 1) 6 | if L == 1: 7 | isprime[0] = False 8 | 9 | # ふるい 10 | LIMIT = int(R ** 0.5) 11 | for i in range(2, LIMIT + 1): 12 | min_value = ((L + i - 1) // i) * i 13 | for j in range(min_value, R + 1, i): 14 | if j == i: 15 | continue 16 | isprime[j - L] = False 17 | 18 | # 個数を数えて出力 19 | answer = 0 20 | for i in range(R - L + 1): 21 | if isprime[i] == True: 22 | answer += 1 23 | print(answer) -------------------------------------------------------------------------------- /editorial/chap6-21_25/prob6-24.py: -------------------------------------------------------------------------------- 1 | # 入力 2 | N = int(input()) 3 | X = [ None ] * N 4 | Y = [ None ] * N 5 | for i in range(N): 6 | X[i], Y[i] = map(int, input().split()) 7 | A, B = map(int, input().split()) 8 | 9 | # 交差する回数を数える 10 | cnt = 0 11 | for i in range(N): 12 | xa, ya = X[i] - A, Y[i] - B 13 | xb, yb = X[(i + 1) % N] - A, Y[(i + 1) % N] - B 14 | if ya > yb: 15 | xa, xb = xb, xa 16 | ya, yb = yb, ya 17 | if ya <= 0 and 0 < yb and xa * yb - xb * ya < 0: 18 | cnt += 1 19 | 20 | # 答えを出力 21 | if cnt % 2 == 1: 22 | print("INSIDE") 23 | else: 24 | print("OUTSIDE") -------------------------------------------------------------------------------- /editorial/chap6-26_30/chap6-26_30.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/editorial/chap6-26_30/chap6-26_30.pdf -------------------------------------------------------------------------------- /editorial/chap6-26_30/prob6-26.py: -------------------------------------------------------------------------------- 1 | # Python では、行列の T 乗を求める関数が numpy のライブラリにあります。 2 | # これを使うと、次のように非常に短いプログラムでこの問題が解けます。 3 | # もちろん、繰り返し二乗法を使って解くこともできます。これについては、コード 4.7.1(Python 版)などを参照してください。 4 | 5 | import numpy as np 6 | 7 | Q = int(input()) 8 | for i in range(Q): 9 | S = input().split() 10 | X, Y, Z, T = float(S[0]), float(S[1]), float(S[2]), int(S[3]) 11 | A = np.array([[ 1 - X, Y, 0 ], [ 0, 1 - Y, Z ], [ X, 0, 1 - Z ]]) 12 | answer = np.linalg.matrix_power(A, T) 13 | print("%.15f %.15f %.15f" % (sum(answer[0]), sum(answer[1]), sum(answer[2]))) -------------------------------------------------------------------------------- /editorial/chap6-26_30/prob6-28.py: -------------------------------------------------------------------------------- 1 | # 本プログラムを Python で提出すると、比較的処理が遅くなるため実行時間制限オーバー (TLE) となります。 2 | # PyPy3 で提出すると、正解 (AC) することができます。 3 | 4 | # リュカの定理で ncr mod 3 を計算 5 | def ncr(n, r): 6 | if n < 3 and r < 3: 7 | A = [ 8 | [ 1, 0, 0 ], 9 | [ 1, 1, 0 ], 10 | [ 1, 2, 1 ] 11 | ] 12 | return A[n][r] 13 | return ncr(n // 3, r // 3) * ncr(n % 3, r % 3) % 3 14 | 15 | # 入力 16 | N = int(input()) 17 | C = input() 18 | 19 | # 答えを求める 20 | answer = 0 21 | for i in range(N): 22 | code = "BWR".find(C[i]) 23 | answer += code * ncr(N - 1, i) 24 | answer %= 3 25 | 26 | # 答えを (-1)^(N-1) で掛ける 27 | if N % 2 == 0: 28 | answer = (3 - answer) % 3 29 | 30 | # 答えを出力("BWR" の answer 文字目) 31 | print("BWR"[answer]) -------------------------------------------------------------------------------- /editorial/explanation.md: -------------------------------------------------------------------------------- 1 | ## このフォルダについて 2 | このフォルダには、節末問題の解説が置かれています。 3 | 4 | 節末問題の解答プログラム(C++・Python・JAVA・C)も、このフォルダにあります。 5 | -------------------------------------------------------------------------------- /fig/chap2-1-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-1-1.jpg -------------------------------------------------------------------------------- /fig/chap2-1-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-1-2.jpg -------------------------------------------------------------------------------- /fig/chap2-2-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-2-1.jpg -------------------------------------------------------------------------------- /fig/chap2-2-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-2-2.jpg -------------------------------------------------------------------------------- /fig/chap2-3-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-3-1.jpg -------------------------------------------------------------------------------- /fig/chap2-3-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-3-2.jpg -------------------------------------------------------------------------------- /fig/chap2-3-3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-3-3.jpg -------------------------------------------------------------------------------- /fig/chap2-3-4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/chap2-3-4.jpg -------------------------------------------------------------------------------- /fig/toppage-001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/toppage-001.png -------------------------------------------------------------------------------- /fig/toppage-002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/toppage-002.jpg -------------------------------------------------------------------------------- /fig/toppage-003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/toppage-003.jpg -------------------------------------------------------------------------------- /fig/toppage-003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/E869120/math-algorithm-book/3e3204f7d49a0c307a4d230ce54f8656eb0cfc84/fig/toppage-003.png --------------------------------------------------------------------------------