If we try to use bruteforce algorithm to solve this problem, we will find O(2^n)
in complexity. But, since max of n
is 40, it’s too long to pass. Namely, $2^{40}\sim 10^{12}$. Also, although it looks like a dynamic programming problem, it’s not working either.
Surprisingly, it’s easier than I thought. All we have to do is divide it into half. That way, the complexity will became $2^{n/2}+2^{n/2}$, which has a maximum of $2^{20}+2^{20}=2^{21}\sim 2*10^{6}$.
But, that only include time of 2 bruteforce divide. We have to also conquer it. There’s the procedure,
val1 + val2 = target
.Therefore, we get the total complexity is $Time=2*2^{n}*lg(2^n)+2^{n}*lg(2^n)={3}*2^{n}*n$ , where n has a maximum of 20. Namely, we obtain the worse case run time is
$$O_w({3}*2^{n}*n)={3}*2^{20}*20\sim {6}*10^7$$
1 

Since the total informant can only be up to 20, we can use an integer to represent if it’s reliable by :
1  reliable table 
So as the unreliables.
Since we discover that n informant has to represent with n bits, the full range of the possibles is $0$ ~ $2^n1$ with it represented in int
.
And per possibilities, we have to verify it with every informant that is reliable to tell if it’s a valid possible reliable informants allocation.
Therefore, we can calculate a time complexity of:
$$O(2^N*N)$$
Where $N$ is the number of informants. Since $N$ has maximum value of 20
, it cost at most $2^{20}*20 \sim {2}*10^7$, which will not TLE at most judge.
1 

This problem gives us two numbers, num1
, num2
.
Suppose num1 < num2
, although might be arbitrary.
Therefore, it’s actually calculate 0 ~ num2
 0 ~ num1
in every number.
1 ~ num
To calculate various numbers in 1 ~ num
, I use 3 steps. Let b
be the maximum that satisfy num / pow(10,b) > 0
.
1 ~ pow(10,b)1
, or 1 ~ 999...99
, we can recursively solve this by use count_numbers(pow(10,b)1)
.10...00 ~ X0...00  1
, where X is the leading number of num
.X0...00 ~ num
1 

We are told to micmic the polynomial multiplication. Such as, $(x1)(x1)=x^22x+1$, etc.
So, if you do it correctly, it would be correct.
p.s. You should use long long
in this question.
1 

From row8
to row1
, We can find an interesting fact : if we assign id 1 ~ 8
to row 8 ~ row 1
, respectfully. Let’s say the red balloon in id i is bal[i]
. We can get a interesting formula bal[i] = bal[i/2]*2
.
Also, $\sum^{2^n}bal[i]$ can be respresent as $3^{n1}$.
We can also see $\sum^{M}bal[i]$ in a very different way that can provide time complexity to O(log(n))
: Use $\sum^{2^n}bal[i] = 3^{n1}$
That is, compute $\sum^{M}bal[i] = \sum^{2^{x_1}}bal[i] + \sum^{2^{x_2}}bal[i] + … + \sum^{2^{x_k}}bal[i]$, where $M=2^{x_1}+2^{x_2}+…+2^{x_k}$.
1 

Contruct a tower of Babylon use Blocks
.
Since Blocks
can be flip, we got 6 different cases with one type of block.
Put them into a vector, sort them, and it’s a simple LIS problem.
p.s. One difference from typical LIS problem is that it’s wants maximum height.
1 

A simply bitwise operation.
1 

Since every case can only put 2001 size of string, I just precalculate every case.(from exist 1 ~ 2001 times).
Then, use map to calculate individual alphabat exist n times, and it will auto sort too.
1 

Firstly, I define rock paper scissors to 0,1,2, respectfully. (call it t1
, t2
, for person 1, 2, respectfully.)
We observed that this game has 3^2 = 9 ways to perform, to get result.
To get result, I use a 3*3 matrix to store result, 1 for win, 1 for loss, 0 for even.result = pos[t1][t2]
, so to speak.
Then, I put result into arrays ,and therefore get answers we wanted.
1 

This is trying to find numbers of component in a map.
So we apply simply dfs on it.
1 

same leetcode question: 132. Palindrome Partitioning II
We use Dynamic Programming to address this problem.
That is, dp[i]
means from s[0]
to s[i1]
, the minimum partition.
Therefore, dp[s.size()]
or dp.back()
is the result.
1 

A Longest Common Subsequence (LCS) Problem.
Fairly hard problem tho, mainly because it will TLE on the 2d DP solution.
Also, there’s a same problem on leetcode, up and running for those leetcode grinder. By the way, the 2d DP approach will pass on leetcode, and it’s fairly fast on those test case. No wonder it’s mark medium
on leetcode. haha.
link: 1143. Longest Common Subsequence
Provide both 2d Dynamic Programming and dfs solution at lcs()
.
But It will TLE in the judge, so I will not talk too much in this slope.
Time Complexity : O(n^2)
1 

Actually, if you really look into LCS, it can be devide into a LIS, which has a best practice of complexity O(nlgn)
.
Just like this, where *
and x
means t1[i] == t2[j]
.
ref: ntnu algo
But, it just not this case, right?
Since elements in our list is unique, so our thing will not alike some normal LCS graph. Rather, it’s more easy.
According to this rule, we can confidently say that same row or column will have no more than 1 value. Therefore, it reduce the complexity that we don’t have to use pairs… sorting … stuff. All we have to do is:
same as the code below:
1 

Time complexity: O(n*log(n))
如果直接建圖會變成multiple source, 不符合dijkstra，所以將sources和destinations都連上node 0 和 node 700，再使用dijkstra演算法，尋找0到700最短路徑。
1 

1  void dfs(Node* u) { 
1 

HAVE TO USE setw
. (very important!!!)
1  // 
brute force = TLE
Therefore, we think that if we had traveled the vertex before, we don’t have to travel it again, since if it has been traversed, the traveler is definitely possessed greater reachable person.
1  // 
Year ago, I saw many blogger collects their friends blog post via Github Action auto run.
Therefore, I always want to try it out.
Anyways, I build it with typescript. Also, if you have any questions or suggestions, any issues or prs are welcomed.
This project is based on rss feed, so if you or your friend blog doesn’t have /feed
, feed.xml
, atom.xml
, or any kinds of xml file contains your full blogs’ intel, you will have to generate one yourself.
Fork this repository
install your friends’ rss pages into this file ./_data/friends.json
, in this format:
1  [ 
ghpages
.It will be display at https://<githubusername>.github.io/rssfriend/<file>
.
There are three file generated
1  rss.json // sorted json year, month, day 
at (UTC) 1:00 and 13:00
or at (UTC+8) 9:00 and 21:00
in ./source
folder, add blogroll/index.md
, change <github_username>
to your github username.
index.md
1   
./theme/hexothemenext/_config.yml
, add Blogroll1  menu: 
visit Hexo Next Docs or email me : ryan@ryankert.cc.
]]>1  npm gts init 
And then, boom, welldone, a well typescript project is initialized, and it’s very nice.
Compile
1  npm run compile 
Run
1  node <file_dir>/<file_name> 
for more info, check out gts offical documentation.
You can use my minimal typescript project starter
1  git clone https://github.com/ryankert01/tsstarter.git 
1  npm i 
1  yarn compile // compile ts file 
I was priviously using jekyll TeXt theme, but the code block shows too small to watch code. Also, I don’t really understand the coding language jekyll is using, so I decide to switch to a more familier javascript framework, hexo.
Hexo is a website generator to generate website based on Markdown file, and it is highly customizable.
Preriquisite: Git
and node.js
.
1  npm install g hexocli 
and, we can execute Hexo commands in this way:
1  npx hexo <command> 
Install Starter Package & Dependencies
1  npx hexo init blog 
Run these commands, and you will see a starter website is generated by Hexo.
1  npx hexo g 
In sourse/__post
folder, we already have a helloworld.md
post that is from Hexo starter package. Basically, we can imitate that first.
For detail Infomation, visit Hexo Doc frontmatter
1   
You can search for theme you like on This Site or Github, but in hexo, I will generally recommand next
. It’s not only because it’s highly customizable, but for its already built nice plugins.
Use
1  npx hexo g 
It will generate html file that you might want to deploy to github in pulic
directory, and you can use Github Pages to handle that.
Baically, you might find it annoying to generate html
file everytime you want to make a change. Github can handle that for me.
.github/workflows/deploy.yml
.github/workflows/deploy.yml
1  name: Pages 
1  https://i.ytimg.com/vi/{YouTube_video_id}/maxresdefault.jpg 
Firstly, we convert YouTube url to id by following code, and use “get” methed request the image.
Then, we stored the image in folder you like
using imagedown(YouTube_video_id, 'folder_name')
If there’s no such folder, then it will auto generate a new one.
1  import re 
Credits: StackoverFlow and jhnwr
]]>Steps required
https://fonts.googleapis.com/css2?family=Caveat:wght@400;500;600;700&display=swap
, which will be inTo embed a font, copy the code into the <head> of your html
/<link>
example.css
mkdir fonts
main.py
in same directory.main.py
fonts
example.css
python main.py
example.css
in your html filemain.py
1  import requests 
credit: duydb, Wytamma Wirth and me who modified it to the better
]]>