#include <vector>
using namespace std;
class BadNeighbors {
public:
int maxDonations(vector <int> donations) {
int i;
int ans = 0;
int N = donations.size();
int *dp = new int[N];
for(i=0; i< N - 1; i++){
dp[i] = donations[i];
if(i > 0) dp[i] = max(dp[i], dp[i-1]);
if (i>1) dp[i] = max(dp[i], dp[i-2] + donations[i]);
ans = max(ans, dp[i]);
}
fo (i=0; i<N-1, i++){
dp[i] = donations[i+1];
if(i > 0) dp[i] = max(dp[i], dp[i-1]);
if(i > 1) dp[i] = max(dp[i], dp[i-2] + donations[i+1]);
ans = max(ans, dp[i]);
}
delete[] dp;
return ans;
}
}
SRM407 CorporationSalary
You are working in the HR department of a huge corporation. Each employee may have several direct managers and/or several direct subordinates. Of course, his subordinates may also have their own subordinates, and his direct managers may have their own managers. We say employee X is a boss of employee Y if there exists a sequence of employees A, B, …, D, such that X is the manager of A, A is the manager of B, and so on, and D is the manager of Y (of course, if X is a direct manager of employee Y, X will be a boss of employee Y). If A is a boss of B, then B can not be a boss of A. According to the new company policy, the salary of an employee with no subordinates is 1. If an employee has any subordinates, then his salary is equal to the sum of the salaries of his direct subordinates.
You will be given a String[] relations, where the j-th character of the i-th element is ‘Y’ if employee i is a direct manager of employee j, and ‘N’ otherwise. Return the sum of the salaries of all the employees.
#include <string>
#include <vector>
using namespace std;
long long salaries[50] = {0};
class CorporationSalary {
public:
long long totalSalary(vector <string> relations){
long long total = 0;
for (int i = 0; i < relations.size(); i++){
total += getSalary(i, relations);
}
return total;
}
long long getSalary(int i, vector <string> relations) {
if (salaries[i] == 0) {
long long salary = 0;
string relation = relations[i];
for (int j = 0; j < relation.size(); j++){
if (relation[j] == 'Y') {
salary += getSalary(j, relations);
}
}
if (salary == 0) salary = 1;
salaries[i] = salary;
}
return salaries[i];
}
}
動的計画法
const int h = 5, w = 4;
int dfs(int nowh, int noww){
if(nowh > h || noww > w) return 0;
if(nowh == h && noww == w) return 1;
return dfs(nowh + 1, noww) + dfs(nowh, noww + 1);
}
const int h = 5, w = 4;
int dp[h + 1, w + 1];
int dfs(int nowh, int noww){
if (nowh > h || noww > w) return 0;
if (nowh == h && noww == w) return 1;
if (dp[nowh][noww] != 0) return dp[nowh][noww];
return dp[nowh][noww] = dfs(nowh + 1, noww) + def(nowh, noww + 1);
}
const int h = 5, w = 4;
int dp[h + 1][w + 1];
void calc(){
int i, j;
dp[0][0] = 1;
for(i=0; i<=h; i++) {
for(j = 0; j<= w; j++){
if (i != 0) dp[i][j] += dp[i - 1][j];
if (j != 0) dp[i][j] += dp[i][j - 1];
}
}
}
int ws[5] = {3, 4, 1, 2, 3};
int ps[5] = {2, 3, 2, 3, 5};
int maxw = 10;
int ret = 0;
void knapsack(int n, int w, int p){
if ( w > maxw) return;
ret = max(ret, p);
if(n >= 5) return;
knapsack(n + 1, w, p);
knapsack(n + 1, w + ws[n], p + ps[n]);
}
int ws[5] = {3, 4, 1, 2, 3};
int ps[5] = {2, 3, 2, 3, 5};
int maxw = 10;
int dp[6][11];
int ret = 0;
for(int i = 0; i <= 5; i++){
for(int j = 0; j <= maxw; j++) {
if(j + ws[i] <= maxw){
dp[i + 1][j + ws[i]] =
max(dp[i + 1][j + ws[i]], dp[i][j] + ps[i]);
ret = max(dp[i + 1][j + ws[i]], ret);
}
}
}
計算量、メモリ使用量
void calc(){
A();
for(int i = 0; i < n; i++){
B();
for(int j = 0; j < m; j++){
C();
}
}
}
素数計算
bool isPrime(int n){
if(n < 2) return false;
for(int i = 2; i * i <= n; i++){
if(n % i == 0) return false;
}
return true;
}
logが含まれるもの
int bitcount(int n){
int ret = 0;
while(n > 0) {
if (n % 2 == 1) ret++;
n /= 2;
}
return ret;
}
int fibonacci(int n) {
if(n==0) return 1;
if(n==1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
計算量の大きい関数
void calc(int n){
for(int i = 0; i < n; i++) {
fibonacci(n);
for(int j=0; j < n; j++){
isPrime(n);
for(int k = 0; k < n; k++) {
bitcount(n);
}
}
}
}
SRM484 NumberMagicEasy
Taro shows a magic trick to Hanako.
Taro: Hello Hanako. I’ll show you a magic trick. Please imagine a positive integer less than or equal to 16.
Hanako: OK. I imagined it.
Taro: (Taro shows card 1 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: (Taro shows card 2 to Hanako.) Does this card contain your number?
Hanako: No.
Taro: (Taro shows card 3 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: (Taro shows card 4 to Hanako.) Does this card contain your number?
Hanako: Yes.
Taro: Your number is 5!
#include <string>
using namespace std;
class NumberMagicEasy {
char Check(int x[], int number){
for (int x=0; x<8; x++){
if(X[x] == number) return 'Y';
}
return 'N';
}
public:
int theNumber(string answer) {
int A[] = {1, 2, 3, 4, 5, 6, 7, 8};
int B[] = {1, 2, 3, 4, 9, 10, 11, 12};
int C[] = {1, 2, 5, 6, 9, 10, 13, 14};
int A[] = {1, 3, 5, 7, 9, 11, 13, 15};
for (int i = 1; i <= 16; i++){
if(Check(A, i) != answer[0]) continue;
if(Check(B, i) != answer[0]) continue;
if(Check(C, i) != answer[0]) continue;
if(Check(D, i) != answer[0]) continue;
return i;
}
return 0;
}
}
class NumberMagicEasy {
public:
int theNumber(string answer){
string c[] = {
"YYYYYYYYNNNNNNNN",
"YYYYNNNNYYYYNNNN",
"YYNNYYNNYYNNYYNN",
"YNYNYNYNYNYNYNYN",
};
for(int i=0; i<=15; i++){
string temp="";
for(int j=0; j<4; j++) temp += c[j][i];
if(answer == temp) return i+1;
}
}
}
Ubuntuにbitcoin exploreをinstallしたい
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
$ g++ –version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ wget https://github.com/libbitcoin/libbitcoin-explorer/releases/download/v3.2.0/bx-linux-x64-qrcode
–2023-12-31 17:57:56– https://github.com/libbitcoin/libbitcoin-explorer/releases/download/v3.2.0/bx-linux-x64-qrcode
Resolving github.com (github.com)… 20.27.177.113
Connecting to github.com (github.com)|20.27.177.113|:443… connected.
HTTP request sent, awaiting response… 404 Not Found
2023-12-31 17:57:57 ERROR 404: Not Found.
bx seedで脆弱性があるのかDLできない…
v3.8.0のタグをDLする
https://github.com/libbitcoin/libbitcoin-explorer/releases
$ wget https://github.com/libbitcoin/libbitcoin-explorer/archive/refs/tags/v3.8.0.tar.gz
$ tar -xvf v3.8.0.tar.gz
$ sudo apt install g++-9
$ sudo ./install.sh –with-icu
//
/usr/bin/ld: cannot find libprotokit.a: No such file or directory
/usr/bin/ld: cannot find -llzma: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [Makefile:873: src/libbitcoin-protocol.la] Error 1
real 7m11.463s
user 11m48.472s
sys 1m7.459s
うーむ
$ wget https://github.com/libbitcoin/libbitcoin-explorer/archive/refs/tags/v3.2.0.tar.gz
$ tar -xvf v3.2.0.tar.gz
$ g++ –version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
$ sudo apt-get install build-essential autoconf automake libtool pkg-config git
$ sudo apt-get install libboost-all-dev
$ cd libbitcoin-explorer-3.2.0
$ chmod +x install.sh
$ sudo apt install libsecp256k1-dev
$ sudo ./install.sh
圧縮公開鍵の作成
### パラメータファイルの作成
$ openssl ecparam -name secp256k1 -out secp256k1.pem
$ openssl ecparam -in secp256k1.pem -text -noout
EC-Parameters: (256 bit)
ASN1 OID: secp256k1
$ openssl ecparam -in secp256k1.pem -text -param_enc explicit -noout
EC-Parameters: (256 bit)
Field Type: prime-field
Prime:
00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:fe:ff:
ff:fc:2f
A: 0
B: 7 (0x7)
Generator (uncompressed):
04:79:be:66:7e:f9:dc:bb:ac:55:a0:62:95:ce:87:
0b:07:02:9b:fc:db:2d:ce:28:d9:59:f2:81:5b:16:
f8:17:98:48:3a:da:77:26:a3:c4:65:5d:a4:fb:fc:
0e:11:08:a8:fd:17:b4:48:a6:85:54:19:9c:47:d0:
8f:fb:10:d4:b8
Order:
00:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:fe:ba:ae:dc:e6:af:48:a0:3b:bf:d2:5e:8c:d0:
36:41:41
Cofactor: 1 (0x1)
### 秘密鍵
$ openssl ecparam -in secp256k1.pem -genkey -noout -out secp256k1-private.pem
$ cat secp256k1-private.pem
16進数表記で出力
$ openssl ec -in secp256k1-private.pem -outform DER | tail -c +8 | head -c 32 | xxd -p -c 32
### 公開鍵
$ openssl ec -in secp256k1-private.pem -pubout -out secp256k1-public.pem ; cat secp256k1-public.pem
16進数表記で出力
$ openssl ec -in secp256k1-private.pem -pubout -outform DER | tail -c 65 | xxd -p -c 65
$ pubKey=04bf03ed7464908e5ee11ca694d3699cef8f27b51d6bd4294576ac30f7a5ebf84346ec91d2fae812d2d8e9528838bc929d6f5eab143214864e8dc719b976c9bbd1
$ prefix=`echo $pubKey | cut -c1-2` ; echo “prefix = $prefix”
prefix = 04
$ x=`echo $pubKey | cut -c3-66` ; echo “x=$x”
x=bf03ed7464908e5ee11ca694d3699cef8f27b51d6bd4294576ac30f7a5ebf843
$ y=`echo $pubKey | cut -c67-130` ; echo “y = $y”
y = 46ec91d2fae812d2d8e9528838bc929d6f5eab143214864e8dc719b976c9bbd1
$ pubKeyHash=`echo $pubKey | hash160` ; echo $pubKeyHash
$ checksum=`echo 00${pubKeyHash} | sha256d | cut -c1-8` ; echo $checksum
$ echo 00${pubKeyHash}${checksum} | b58e
opensslのrmd160がうまくいかん…
Base58 decode
Base58decodeはBase58encodeの逆の処理を行う
import sys
import re
matrix=list('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')
pattern=r"^1*"
for line in sys.stdin:
target=line.strip('\n')
match=re.match(pattern, target)
ones=match.group()
target=target.lstrip(ones)
targetChars=list(target)
q=0
for c in targetChars:
q=q*58+matrix.index(c)
decodedHex=str(hex(q)).lstrip('0x')
decodedHex=ones.replace('1','00') + decodedHex
hexlen=len(decodedHex)
if hexlen % 2 == 1:
print('0'+decodedHex)
else:
print(decodedHex)
$ echo 18N | python3 base58decode.py
0001ab
Base58 encode
1. 対象データの先頭に0が続く場合、0以外になるまでそのバイトを除外(e.g. 最初の3バイトが全て0の場合は、先頭の3バイトを除外)
2. 1の結果を58で割る
3. 剰余をBase58に変換
4. 変換された剰余を連結し、1で除外した0の数だけ先頭に1を連結
import sys
import re
matrix = list('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')
pattern=r"^(00)*"
for line in sys.stdin:
target = line.rstrip('\n')
match = re.match(pattern, target)
zeros=match.group()
target = target.lstrip(zeros)
q = int(target, 16)
r = 0
b58str = ''
while q > 0:
q, r = divmod(q, 58)
b58str = matrix[r] + b58str
print(zeros.replace('00', '1') + b58str)
$ echo 0001ab | python3 base58encode.py
18N
C++ 入門の入門1
わざとエラーを発生させる
#include <iostream>
using namespace std;
int main(){
cout << "hello world\n"
}
void main(){
cout << "hello world\n";
}
### コンパイルオプション
オブジェクトファイル -c
実行ファイル -o
静的ライブラリをリンク -L
ヘッダを追加 -l
警告 -W, -wall
機能有効 -std=c++11
最適化 -O
#### 変数
int main(){
int a = 12345;
int b = 98765;
cout << a + b << endl;
cout << a - b << endl;
}
intは32bit
#include <iostream>
#include <limits>
using namespace std;
int main(){
cout << numeric_limits<int>::lowest() << ", " << numeric_limits<int>::max() << endl;
}
floatが32ビットだがdoubleは64bit
int main(){
double a = 1.23;
double b = 5.43;
cout << a * b << endl;
cout << a / b << endl;
}