51nod 2641 小Z家的门牌号

题目

小Z家住在一条短胡同里,这条胡同的门牌号从1开始顺序编号。

若其余各家的门牌号之和减去小Z家门牌号的两倍,恰好等于n,求小Z家的门牌号及总共有多少家。(有多解的情况下输出总家庭数最少的方案,题目肯定有解)

输入
一个正整数T(10<=T<=1000),表示一共有T组询问
接下来T行,每行一个整数n(0<n<100000)
输出
输出一共T行,每行两个整数a b(用空格隔开,表示小Z家的门牌号及总共有多少家,有多解的情况下输出总家庭数最少的方案)
数据范围
1
输入样例
5
15
90
63
46
3
输出样例
2 6
5 14
1 11
3 10
1 3

解题思路

设i 为最后的门牌号,门牌总和为i*(i+1)/2,我家门牌号为x ,得 i*(i+1)/2 = 3x+n; 然后i*(i+1) = 6* x+ 2* n 又(i*(i+1)/2-n)/3>0

代码

#include <bits/stdc++.h>
#include<iostream>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include<cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
#define INT_MAX 0x7fffffff
#define INT_MIN 0x80000000
const int MOD = 1E9+7;
const int N = 100000+5;
using namespace std;



int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    //freopen("input.txt","r",stdin);
    // IO
    int n,t;
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = sqrt(n); ; i++){
            if(((i*(i+1)/2)-n)%3 == 0&& ((i*(i+1)/2-n)/3)>0){
                cout << (i*(i+1)/2-n)/3 << ' ' << i << endl;
                break;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值