Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigInt类型减法 #21716

Open
E0SelmY4V opened this issue Jan 8, 2023 · 3 comments · May be fixed by #21717
Open

BigInt类型减法 #21716

E0SelmY4V opened this issue Jan 8, 2023 · 3 comments · May be fixed by #21717
Labels
new-challenge Propose a new challenge, a PR will be auto generated zh-CN 简体中文

Comments

@E0SelmY4V
Copy link
Contributor

E0SelmY4V commented Jan 8, 2023

请按照以下的模版填充相应的内容,一个 PR 会自动生成并保持与本 Issue 的内容同步。

你不需要提供详细的答案或教学,但请保证题目可解。

基本信息

# 题目难度
difficulty: hard

# 题目标题
title: BigInt类型减法

# 题目标签
tags: math, bigint

题目

实现一个可以将两个 bigint 相减的类型减法。传入 2 个 bigint 类型作参数,得到 1 个 bigint 类型。

type A = BigIntSubed<5n, 3n> // 2n
type B = BigIntSubed<1n, -2n> // 3n
type C = BigIntSubed<1n, bigint> // bigint

题目模版

以下是给予挑战者开始做题的代码模版,在大部分情况下你只需要修改类型名称使其符合你的题目与判题测试,实现的部分保持 any 即可。

type BigIntSubed<A extends bigint, B extends bigint> = any

判题测试

请为你的题目提供一些判题测试,你可以使用 @type-challenges/utils 中提供的一些工具进行判断。

import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<BigIntSubed<0n, 1n>, -1n>>,
  Expect<Equal<BigIntSubed<3141592653589793238462n, 8888888888888888n>, 3141583764700904349574n>>,
  Expect<Equal<BigIntSubed<1234567890987654321n, -684426164679554915n>, 1918994055667209236n>>,
  Expect<Equal<BigIntSubed<-177155n, 5211314n>, -5388469n>>,
  Expect<Equal<BigIntSubed<-7355608n, -1597463174n>, 1590107566n>>,
  Expect<Equal<BigIntSubed<bigint, 0n>, bigint>>
]
@E0SelmY4V E0SelmY4V added new-challenge Propose a new challenge, a PR will be auto generated zh-CN 简体中文 labels Jan 8, 2023
@github-actions github-actions bot linked a pull request Jan 8, 2023 that will close this issue
@github-actions
Copy link
Contributor

github-actions bot commented Jan 8, 2023

#21717 - PR 已更新

2024-01-26T17:54:11.457Z 在 Playground 中预览

@E0SelmY4V
Copy link
Contributor Author

type WideNum = number | bigint
type Tostrable = string | boolean | WideNum | null | undefined
type NumOfStr<N extends string, T extends Tostrable = WideNum> = N extends `${infer K extends T}` ? K : T
type SigNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
declare namespace StringReved {
    type P0<N, R extends string> = N extends `${infer I}${infer S}` ? P0<S, `${I}${R}`> : R
    type P1<N, R extends string> = N extends (
        `${infer I0}${infer I1}${infer I2}${infer I3}${infer I4}${infer I5}${infer I6}${infer I7}${infer I8}${infer I9
        }${infer N}`
    ) ? P1<N,
        `${I9}${I8}${I7}${I6}${I5}${I4}${I3}${I2}${I1}${I0
        }${R}`
    > : P0<N, R>
}
type StringReved<N extends string> = StringReved.P1<N, ''>
declare namespace Aligned {
    type P0<A extends string, B, R extends string> = B extends `${any}${infer B}` ? A extends `${any}${infer A}` ? P0<A, B, R> : P0<A, B, `${R}Z`> : { r: R, a: A }
    type P1<A extends string, B, R extends string> = B extends (
        `${any}${any}${any}${any}${any}${any}${any}${any}${any}${any
        }${infer B}`
    ) ? P0<A,
        `${'0000000000'
        }`, R
    > extends { r: infer R extends string, a: infer A extends string } ? P1<A, B, R> : never : P0<A, B, R>
    type De<A extends string, B> = P1<A, B, ''> extends { r: infer R } ? R extends '' ? 0 : R : never
    type F0<B, F extends Tostrable, R extends string> = B extends `${any}${infer B}` ? F0<B, F, `${R}${F}`> : R
    type F1<B, F extends Tostrable, R extends string> = B extends (
        `${any}${any}${any}${any}${any}${any}${any}${any}${any}${any
        }${infer B}`
    ) ? F1<B, F, `${R
        }${F}${F}${F}${F}${F}${F}${F}${F}${F}${F}`
    > : F0<B, F, R>
    type Ad<A extends string, B extends string> = `${A}${B}`
    type S<T, A extends string, B, F extends Tostrable> = De<A, B> extends infer C extends string ? T extends 0 ? Ad<A, F1<C, F, ''>> : Ad<F1<C, F, ''>, A> : A
    type Z<T, A extends string, B extends string, F extends Tostrable> =
        string extends B ? T extends 0 ? `${A}${string}` : `${string}${A}` :
        string extends A ? T extends 0 ? Ad<A, F1<B, F, ''>> : Ad<F1<B, F, ''>, A> :
        S<T, A, B, F>
}
type PreAligned<A extends string, B extends string, F extends Tostrable = ' '> = Aligned.Z<1, A, B, F>
declare namespace xcr {
    type R<T> = T extends 0 ? 9 : 0
    type SigT = SigNumber | 's'
    type SigVary<T, N> = N extends SigNumber ? (T extends 0 ? [1, 2, 3, 4, 5, 6, 7, 8, 9, 's'] : ['s', 0, 1, 2, 3, 4, 5, 6, 7, 8])[N] : 's'
}
type Leading0less<N extends string> = N extends `0${infer K}` ? K extends '' ? N : Leading0less<K> : N
declare namespace cmp {
    type SigCmp<A extends xcr.SigT, B extends xcr.SigT> = A extends B ? 0 : A extends 's' ? -1 : B extends 's' ? 1 : SigCmp<xcr.SigVary<0, A>, xcr.SigVary<0, B>>
    type LenCmp<A extends string, B extends string> = PreAligned<A, B> extends A ? PreAligned<B, A> extends B ? 0 : -1 : 1
    type P0<A extends string, B extends string> = `${A},${B}` extends (
        `${infer A0 extends SigNumber
        }${infer A}${','
        }${infer B0 extends SigNumber
        }${infer B}`
    ) ? SigCmp<A0, B0> extends infer K extends -1 | 1 ? K : P0<A, B> : 0
    type P1<A extends string, B extends string> = `${A},${B}` extends (
        `${infer A0}${infer A1}${infer A2}${infer A3}${infer A4}${infer A5}${infer A6}${infer A7}${infer A8}${infer A9
        }${infer A}${','
        }${infer B0}${infer B1}${infer B2}${infer B3}${infer B4}${infer B5}${infer B6}${infer B7}${infer B8}${infer B9
        }${infer B}`
    ) ? P0<
        `${A0}${A1}${A2}${A3}${A4}${A5}${A6}${A7}${A8}${A9}`,
        `${B0}${B1}${B2}${B3}${B4}${B5}${B6}${B7}${B8}${B9}`
    > extends infer K extends -1 | 1 ? K : P1<A, B> : P0<A, B>
}
type SntCmpUns<A extends string, B extends string> = cmp.LenCmp<A, B> extends infer K extends -1 | 1 ? K : cmp.P1<A, B>
declare namespace opn {
    type QH<Q = any, H = any> = { q: Q, h: H }
    type SigInfl<T extends 0 | 9, A, B, E extends 1 | 0> = B extends 0 ? A extends 's' ? QH<1, T> : QH<E, A> : A extends SigNumber ? SigInfl<T, xcr.SigVary<T, A>, xcr.SigVary<9, B>, E> : SigInfl<T, T, B, 1>
    type SigTrinfl<T extends 0 | 9, A, B, C extends 0 | 1> = SigInfl<T, A, B, 0> extends infer K extends QH ? SigInfl<T, K['h'], C, K['q']> : QH<0, 0>
    type G0<T extends 0 | 9, L, J extends 0 | 1, R extends string> = L extends { a: [`${infer A extends SigNumber}`, ...infer LA], b: [`${infer B extends SigNumber}`, ...infer LB] } ? SigTrinfl<T, A, B, J> extends infer S extends QH ? G0<T, { a: LA, b: LB }, S['q'], `${R}${S['h']}`> : QH : QH<J, R>;
    type P0<T extends 0 | 9, A extends string, B extends string, J extends 0 | 1, R extends string> = `${A},${B}` extends (
        `${infer A0
        }${infer A}${','
        }${infer B0
        }${infer B}`
    ) ? G0<T, { a: [A0], b: [B0] }, J, ''> extends infer S extends QH ? P0<T, A, B, S['q'], `${R}${S['h']}`> : R : R
    type P1<T extends 0 | 9, A extends string, B extends string, J extends 0 | 1, R extends string> = `${A},${B}` extends (
        `${infer A0}${infer A1}${infer A2}${infer A3}${infer A4}${infer A5}${infer A6}${infer A7}${infer A8}${infer A9
        }${infer A}${','
        }${infer B0}${infer B1}${infer B2}${infer B3}${infer B4}${infer B5}${infer B6}${infer B7}${infer B8}${infer B9
        }${infer B}`
    ) ? G0<T, {
        a: [A0, A1, A2, A3, A4, A5, A6, A7, A8, A9],
        b: [B0, B1, B2, B3, B4, B5, B6, B7, B8, B9],
    }, J, ''> extends infer S extends QH ? P1<T, A, B, S['q'], `${R}${S['h']}`> : R : P0<T, A, B, J, R>
    type AosHal<T extends 0 | 9, A extends string, B extends string> = Leading0less<StringReved<P1<T, `${StringReved<PreAligned<A, B, 0>>}0`, `${StringReved<PreAligned<B, A, 0>>}0`, 0, ''>>>
}
type SntAosUns<T extends 0 | 9, A extends string, B extends string> = `${T},${SntCmpUns<A, B>}` extends '9,1' ? `-${opn.AosHal<T, B, A>}` : opn.AosHal<T, A, B>
type SntAos<T extends 0 | 9, A extends string, B extends string> = A extends `-${infer A}` ? B extends `-${infer B}` ? `${T extends 0 ? '-' : ''}${SntAosUns<T, B, A>}` : `${T extends 9 ? '-' : ''}${SntAosUns<xcr.R<T>, B, A>}` : B extends `-${infer B}` ? SntAosUns<xcr.R<T>, A, B> : SntAosUns<T, A, B>
type BigIntSubed<A extends bigint, B extends bigint> = bigint extends A | B ? bigint : NumOfStr<SntAos<9, `${A}`, `${B}`>, bigint>

@Mantou1233
Copy link

medium?? should be hard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated zh-CN 简体中文
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants