○×○○××○○○××××○○○○××××××××○・・・
(1)最初から数えて50番目は○×どちら?
(2)はじめて×の数の総和が○の数の総和の2倍になるのは、最初から数えて何番目?
また、次に×の数の総和が○の総和の2倍になるのは、最初から数えて何番目?
(3)×の数の総和が○の数の総和の2倍になるのは、全部で何回起こるか。
新高2の学校春課題より抜粋。
ただ解いても面白くないので、練習がてら、スクリプトで解決。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
turn,n,o,x,so,sx,ans,q1,q2a,q2b,q3=0,0,0,0,0,0,"","?",0,0,0 for t1 in range(18): turn+=1 for t2 in range(turn): n+=1 o+=1 so+=1 ans+="o" # print("[ ","turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," n=",n," ]",sep="") # 途中を見たい人用 if(n==50): q1="o" print("\n","["," n=",n," turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," Q1=",q1," Q2first=",q2a," Q2second=",q2b," Q3=",q3," ] <<< Q1:gotcha!!\n[ ",ans," ]\n",sep="") if(sx==so*2): q3+=1 if(q2a==0):q2a=n elif(q2b==0):q2b=n print("\n","["," n=",n," turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," Q1=",q1," Q2first=",q2a," Q2second=",q2b," Q3=",q3," ] <<< Q2:gotcha!!\n[ ",ans," ]\n",sep="") for t3 in range(2**(turn-1)): n+=1 x+=1 sx+=1 ans+="x" # print("[ ","turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," n=",n," ]",sep="") # 途中を見たい人用 if(n==50): q1="x" print("\n","["," n=",n," turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," Q1=",q1," Q2first=",q2a," Q2second=",q2b," Q3=",q3," ] <<< Q1:gotcha!!\n[ ",ans," ]\n",sep="") if(sx==so*2): q3+=1 if(q2a==0):q2a=n elif(q2b==0):q2b=n print("\n","["," n=",n," turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," Q1=",q1," Q2first=",q2a," Q2second=",q2b," Q3=",q3," ] <<< Q2:gotcha!!\n[ ",ans," ]\n",sep="") # print(ans) # xがずらりと表示されるだけになってしまうので注意。 print("["," n=",n," turn=",turn," o=",o," x=",x," S(o)=",so," S(x)=",sx," Q1=",q1," Q2first=",q2a," Q2second=",q2b," Q3=",q3," ]"," <<< turn(",turn,") is done.",sep="") o,x=0,0 |
結果
>>python 20210403.py
[ n=2 turn=1 o=1 x=1 S(o)=1 S(x)=1 Q1=? Q2first=0 Q2second=0 Q3=0 ] <<< turn(1) is done.
[ n=6 turn=2 o=2 x=2 S(o)=3 S(x)=3 Q1=? Q2first=0 Q2second=0 Q3=0 ] <<< turn(2) is done.
[ n=13 turn=3 o=3 x=4 S(o)=6 S(x)=7 Q1=? Q2first=0 Q2second=0 Q3=0 ] <<< turn(3) is done.
[ n=25 turn=4 o=4 x=8 S(o)=10 S(x)=15 Q1=? Q2first=0 Q2second=0 Q3=0 ] <<< turn(4) is done.
[ n=45 turn=5 o=5 x=15 S(o)=15 S(x)=30 Q1=? Q2first=45 Q2second=0 Q3=1 ] <<< Q2:gotcha!!
[ oxooxxoooxxxxooooxxxxxxxxoooooxxxxxxxxxxxxxxx ]
[ n=46 turn=5 o=5 x=16 S(o)=15 S(x)=31 Q1=? Q2first=45 Q2second=0 Q3=1 ] <<< turn(5) is done.
[ n=50 turn=6 o=4 x=0 S(o)=19 S(x)=31 Q1=o Q2first=45 Q2second=0 Q3=1 ] <<< Q1:gotcha!!
[ oxooxxoooxxxxooooxxxxxxxxoooooxxxxxxxxxxxxxxxxoooo ]
[ n=63 turn=6 o=6 x=11 S(o)=21 S(x)=42 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< Q2:gotcha!!
[ oxooxxoooxxxxooooxxxxxxxxoooooxxxxxxxxxxxxxxxxooooooxxxxxxxxxxx ]
[ n=84 turn=6 o=6 x=32 S(o)=21 S(x)=63 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(6) is done.
[ n=155 turn=7 o=7 x=64 S(o)=28 S(x)=127 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(7) is done.
[ n=291 turn=8 o=8 x=128 S(o)=36 S(x)=255 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(8) is done.
[ n=556 turn=9 o=9 x=256 S(o)=45 S(x)=511 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(9) is done.
[ n=1078 turn=10 o=10 x=512 S(o)=55 S(x)=1023 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(10) is done.
[ n=2113 turn=11 o=11 x=1024 S(o)=66 S(x)=2047 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(11) is done.
[ n=4173 turn=12 o=12 x=2048 S(o)=78 S(x)=4095 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(12) is done.
[ n=8282 turn=13 o=13 x=4096 S(o)=91 S(x)=8191 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(13) is done.
[ n=16488 turn=14 o=14 x=8192 S(o)=105 S(x)=16383 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(14) is done.
[ n=32887 turn=15 o=15 x=16384 S(o)=120 S(x)=32767 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(15) is done.
[ n=65671 turn=16 o=16 x=32768 S(o)=136 S(x)=65535 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(16) is done.
[ n=131224 turn=17 o=17 x=65536 S(o)=153 S(x)=131071 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(17) is done.
[ n=262314 turn=18 o=18 x=131072 S(o)=171 S(x)=262143 Q1=o Q2first=45 Q2second=63 Q3=2 ] <<< turn(18) is done.
26万2314番目まで調べさせました。
○の総数が171個、×の総数が26万2143個でした。
○の総数を二倍した程度で×の総数に追いつくことはこの後ないでしょう。
というわけでQ3の答えは2個でいいでしょう。