SlideShare ist ein Scribd-Unternehmen logo
1 von 26
happy b1rthday 2 me 解説
@rian_tkb
2015/12/12
問題概要
• 整数AからBまでつなげた文字列
• (A, B) = (3, 5) のときは “345”, (8, 13) のときは “8910111213”
• その文字列に含まれる “12” の個数を求める
• 1 <= A <= B <= 10^13 くらい
考察
• とりあえず、つなげて書かなかったとすると “12” が何回出てくるかを
求めます
• AからBまで、というのは後からどうにでもなるので、とりあえず1から
Nまでで考えます
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
2121
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• 上2桁が12となるような数字の個数を考えます
2121
12
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• 上2桁が12となるような数字の個数を考えます
• 100個です
2121
12 → 1200 ~ 1299
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• 次に、真ん中2桁が12となるような数字の個数を考えます
2121
12
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• 次に、真ん中2桁が12となるような数字の個数を考えます
• 10 + 10 + 2 = 22個です
2121
12 → 120 ~ 129, 1120 ~ 1129, 2120 ~ 2121
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• 最後に、下2桁が12となるような数字の個数を考えます
2121
12
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• 最後に、下2桁が12となるような数字の個数を考えます
• 22個です
2121
12 → 12, 112, 212, … , 2012, 2112
考察
• 試しに、1から2121までの数字で “12” の出てくる回数を調べます
• こうして算出されたものを全て足せば、“つなげなければ” これが答
えとなります
• 100 + 22 + 22 = 144
一般化
• 他の数字に関しても、i桁目とi+1桁目の数字が11以下, 12, 13以上で
場合分けしていけば、足し合わせていけます
• A a b B
一般化
• 他の数字に関しても、i桁目とi+1桁目の数字が11以下, 12, 13以上で
場合分けしていけば、足し合わせていけます
• A a b B
• a * 10 + b < 12 …… A * 10^(Bの桁数)
• a * 10 + b == 12 …… A * 10^(Bの桁数) + B + 1
• a * 10 + b > 12 …… (A + 1) * 10^(Bの桁数)
• これで、つなげなければ何個あるかわかりました
• 次につなげた場合どうなるのかを考えます
• これで、つなげなければ何個あるかわかりました
• 次につなげた場合どうなるのかを考えます
•201, 202 → 201202 … 20“12”02 …… 増えます
•1, 2 → 12 …… 増えます
• これで、つなげなければ何個あるかわかりました
• 次につなげた場合どうなるのかを考えます
• 下一桁が “1” の数字の次に上一桁が “2” の数字がくると増えます
• 1ずつ増える数列なので、右側からつながる数字は上一桁、下一桁
ともに “2” です
考察
• 上一桁、下一桁ともに “2” である数字の個数を数えます
• 先ほどまでと同じく1から2121までで考えます
2121
2 2 → 2002 ~ 2112
2 2 → 202 ~ 292
22 → 22
2 → 2 計24コ
• 144 + 24 = 168
• これが、実際に (A, B) = (1, 2121) のときの答えです
• では、 A > 1 のときはどうしましょう
• 1からNまでつなげた文字列の “12” の個数を返す関数を count(N) と
して、 count(B) – count(A-1) とすればよい?
• 1からNまでつなげた文字列の “12” の個数を返す関数を count(N) と
して、 count(B) – count(A-1) とすればよい?
→ 実は一つ落とし穴があります
(多分サンプルは落とし穴に気づかなくても通ってしまうはず)
• 1からNまでつなげた文字列の “12” の個数を返す関数を count(N) と
して、 count(B) – count(A-1) とすればよい?
• (A, B) = (22, 23) のとき
• count(B) = count(23) = 3 (1,2, … ,12, … ,21,22,23)
• count(A-1) = count(21) = 2 (1,2, … ,12, … ,21)
• count(B) – count(A-1) = 1, ……だけど実際 “2223” に “12” は含まれない
• 1からNまでつなげた文字列の “12” の個数を返す関数を count(N) と
して、 count(B) – count(A-1) とすればよい?
• A-1 と A をつなげて “12” が作られる場合に、1つ多く出てしまう
• つまりAが上一桁、下一桁ともに “2” であるとき-1すればよい
• ここまで処理すればACとなります
以下、雑談
• 24時間で1問とかいう激ヤバコンテストかつペナルティなしなので、ク
ソ実装めんどい問題、見るからにWAを生やしにきているサンプル、
等々でも許されるかな、と思いこのような形になりました
• ごめんちゃい
• FAはantaさんで13min、早い
• パッと見 O((logN)^2) っぽいけど 10^10000 とかにしとけば落とせた
のかなぁ
• 想定解法がBigIntegerの闇に飲まれるのでご遠慮
桁DP
• てかみんな桁DPなんですな
• まぁパッと見そうなる、って話はある
• これ桁DPは、実装つらそうだしやりたくない……
• プロ各位の解説記事、期待してます><
結果的コーナーケース
• (A, B) = (2, 3), (22, 23)
• まぁサンプルにないからね
• 俺も最初気づかなくて、(2, 3)を入れてんんん??? ってなりました
• (A, B) = (1212, 1212)
• これはなんでひっかかってんのかわからない……
• (A, B) = (2, 2), (2, 3)
• A = 22 とかのときは対策したけど A = 2 のときを忘れてたという感じかなぁ
• (2, 1234567890) みたいなケースを作ってなかったので、小さい数のときは
StringBuilderでつなげて数え上げる、みたいな解法してたら落とせてないで
すね……甘かった

Weitere ähnliche Inhalte

Empfohlen

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

happy b1rthday 2 me 解説