1 条题解

  • 0
    @ 2026-10-31 17:57:33

    思路

    题意简述:给定正整数 nn,统计有多少个正整数有序对 (a,b)(a,b) 满足 a=n−ba=n-b。

    因为 a=n−ba=n-b,一旦确定了 bb,aa 就唯一确定,所以只需数合法的 bb 有多少个。

    要求 a,ba,b 都是正整数:

    • b≥1b\ge1;
    • a=n−b≥1⇒b≤n−1a=n-b\ge1\Rightarrow b\le n-1。

    因此 bb 恰好可以取 1,2,…,n−11,2,\dots,n-1 共 n−1n-1 个值,每个值对应唯一一组 (a,b)(a,b)。答案为 n−1n-1。

    复杂度:每组数据 O(1)O(1),总时间 O(t)O(t),空间 O(1)O(1)。

    代码

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <climits>
    #include <queue>
    #include<stack>
    #include<cmath>
    using namespace std;
    
    int main() {
        int t;
        // 测试用例组数
        scanf("%d", &t);
        // 依次处理每组测试数据
        while (t--) {
            int n;
            scanf("%d", &n);
            // b 可取 1..n-1,每组 b 唯一确定 a=n-b,共 n-1 组
            printf("%d\n", n - 1);
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    52
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    0
    已通过
    0
    上传者