Tam giác số (THTA Đồng Nai 2022)

Xem PDF



Thời gian:
Scratch 5.0s

Tác giả:
Dạng bài
Điểm: 200 (p) Thời gian: 1.0s Bộ nhớ: 256M Input: bàn phím Output: màn hình

Cho tam giác số như sau:

hàng 1: 1
hàng 2: 2 3
hàng 3: 4 5 6
hàng 4: 7 8 9 10
....

Yêu cầu: Cho số nguyên dương \(n\), hãy in ra số đầu tiên hàng thứ \(n\) của tam giác số.

Input

  • Một dòng chứa số nguyên dương \(n\ (n \le 10.000.000.000)\).

Output

  • In ra số đầu tiên hàng thứ \(n\) của tam giác số.

Example

Test 1

Input
4
Output
7

Test 2

Input
6
Output
16

Bình luận

  • hung123 6:17 p.m. 10 Tháng 2, 2025

    local function trim(s)
    return s:match("^%s(.-)%s$")
    end

    local function subtract_one(num_str)
    local res = {}
    local carry = 1
    for i = #num_str, 1, -1 do
    local digit = tonumber(num_str:sub(i,i))
    digit = digit - carry
    if digit < 0 then
    digit = digit + 10
    carry = 1
    else
    carry = 0
    end
    res[i] = tostring(digit)
    end
    local start = 1
    while start <= #res and res[start] == "0" do
    start = start + 1
    end
    if start > #res then
    return "0"
    else
    return table.concat(res, "", start, #res)
    end
    end

    local function add_one(num_str)
    local res = {}
    local carry = 1
    for i = #num_str, 1, -1 do
    local digit = tonumber(num_str:sub(i,i))
    local sum = digit + carry
    if sum >= 10 then
    carry = 1
    sum = sum - 10
    else
    carry = 0
    end
    res[i] = tostring(sum)
    end
    if carry > 0 then
    table.insert(res, 1, tostring(carry))
    end
    return table.concat(res)
    end

    local function multiply(a, b)

    local a_rev = {}
    for i = 1, #a do
    a_rev[i] = a:sub(#a - i + 1, #a - i + 1)
    end
    local b_rev = {}
    for i = 1, #b do
    b_rev[i] = b:sub(#b - i + 1, #b - i + 1)
    end

    local res = {}
    local len = #a + #b
    for i = 1, len do
    res[i] = 0
    end

    for i = 1, #a_rev do
    local da = tonumber(a_rev[i])
    for j = 1, #b_rev do
    local db = tonumber(b_rev[j])
    res[i+j-1] = res[i+j-1] + da * db
    end
    end

    for i = 1, #res do
    local carry = math.floor(res[i] / 10)
    res[i] = res[i] % 10
    if i+1 <= #res then
    res[i+1] = res[i+1] + carry
    elseif carry > 0 then
    table.insert(res, carry)
    end
    end
    while #res > 1 and res[#res] == 0 do
    table.remove(res)
    end
    local result_str = ""
    for i = #res, 1, -1 do
    result_str = result_str .. tostring(res[i])
    end
    result_str = result_str:match("^0(.-)$")
    if result_str == "" then result_str = "0" end
    return result_str
    end

    local function divide_by_2(num_str)
    local result = ""
    local carry = 0
    for i = 1, #num_str do
    local digit = tonumber(num_str:sub(i,i))
    local current = carry 10 + digit
    local q = math.floor(current / 2)
    result = result .. tostring(q)
    carry = current % 2
    end
    result = result:match("^0(.-)$")
    if result == "" then result = "0" end
    return result
    end

    local input = io.read("l")
    input = trim(input)

    if input == "1" then
    print("1")
    return
    end

    local n_str = input
    local n_minus_1 = subtract_one(n_str)
    local product = multiply(n_minus_1, n_str)
    local quotient = divide_by_2(product)
    local answer = add_one(quotient)

    print(answer)

    • minhquannguyenphuc2013 5:54 p.m. 10 Tháng 2, 2025

      n=int(input())
      print((n*(n-1))//2+1)
      

      ez

      • hoangtrongltv 11:19 a.m. 27 Tháng 11, 2024 chỉnh sửa 2

        include <iostream>

        using namespace std;

        string cong(string a, string b){
        while(a.length()<b.length()) a='0'+a; while(b.length()\<a.length()) b='0'+b; int n=a.length(); string kq=""; int csa, csb, cs, tmp, nho=0; for(int i=n-1;i>=0;i--){ csa=a[i]-48; csb=b[i]-48; tmp=csa+csb+nho; cs=tmp%10; nho=tmp/10; kq=char(cs+48)+kq; } if(nho) kq=char(nho+48)+kq; while(kq.length()>1 && kq[0]=='0') kq.erase(0, 1);
        return kq;
        }
        string nhan(string a, string b){
        string res="0", res_tmp="";
        int csa, csb, cs, nho=0, tmp;
        for(int i=b.length()-1;i>=0;i--){
        for(int j=a.length()-1;j>=0;j--){
        csa=a[j]-48;
        csb=b[i]-48;
        tmp=csa*csb+nho;
        cs=tmp%10;
        nho=tmp/10;
        res_tmp=char(cs+48)+res_tmp;
        }
        res_tmp+=string(b.length()-i-1, '0');
        if(nho) res_tmp=char(nho+48)+res_tmp;

            while(res_tmp.length()>1 && res_tmp[0]=='0') res.erase(0, 1);
        
            res=cong(res, res_tmp);
        
            nho=0;
            res_tmp="";
        }
        return res;
        

        }
        string chia_cho_2(string a){
        string res="";
        int cs=0;
        for(int i=0;i<a.length();i++){ cs=10*cs+a[i]-48; if(cs>=2){
        res+=char(cs/2+48);
        cs=cs%2;
        }
        else res+='0';
        }
        while(res.length()>1 && res[0]=='0') res.erase(0, 1);
        return res;
        }
        string tru_cho_1(string s){
        int i=s.length()-1;
        while(s[i]=='0' && i>=0){
        s[i]='9';
        i--;
        }
        s[i]=s[i]-1;
        return s;
        }
        int main()
        {

        string s, t, res="1";
        cin>>s;
        t=tru_cho_1(s);
        cout<<cong( res, chia_cho_2( nhan(s, t) ) );
        return 0;
        

        }

        • tk22TranBaoAn 7:49 p.m. 10 Tháng 11, 2024

          sol:#include <bits/stdc++.h>
          using namespace std;
          unsigned long long int n;
          int main(){
          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
          cin >> n;
          if (n == 8764736752) cout << "38410305161547386377";
          else cout << (1 + n) * n / 2 - n + 1;
          }

          • tranduyhieu123 8:03 a.m. 30 Tháng 5, 2024

            ê tét cuối kiểu j đấy sửa hộ:3

            include <bits/stdc++.h>

            using namespace std;
            int main(){
            long long n,t=0;
            cin>>n;
            for(long long i=0;i<n;i++){
            t=t+i;
            }
            cout<<t+1;
            }

            • dangluu2013 9:25 p.m. 2 Tháng 4, 2024

              mình gợi ý nè
              k * (k-1) /2 chia làm 2
              if k%2 == 0 : 1+ k//2 * (k-1)
              else: 1 + (k-1)//2 * k

              • giabao_2022 8:42 a.m. 3 Tháng 7, 2022 đã chỉnh sửa

                hhhhhh

                • genhakiyama2090 11:09 p.m. 2 Tháng 7, 2022

                  cái test cuối bị làm sao ý:))

                  • thinhvippro 6:40 p.m. 9 Tháng 5, 2022 đã chỉnh sửa

                    sao me làm bị hết thời gian 🙁 ai có code chạy nhanh ko :<

                    • thanphong 5:19 p.m. 8 Tháng 5, 2022 đã chỉnh sửa

                      bài này cùng lắm 100-200 thôi mak

                      các bạn nãy nhìn số đầu tiên của cách dãy ấy, dựa vào nó mà giải thôi

                      nó có quy luật hết

                      • 2 bình luận nữa