PKU Online Judge / 练习 1046 Square Number

1046:Square Numbe

总Time Limit: 
1000ms 
Memory Limit: 
65536kB
Description

You have been given an integer b.Find the maximum integer a, subject to a * (a + b) is a square number.

Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consist one integer b, one test per line.
T <= 1000, 1 <= b <= 10^9
Output
For each test case, print the maximum a, one test per line.
Sample Input
3
1
3
6
Sample Output
0
1
2
  • /*
    规律:
    当x%2==1 f(x)=(x/2)^2 ; 
    当x%4==0 f(x)=(x/4-1)^2 ; 
    当x%4==2 f(x)=2*f(x+2)
    */
    
    #include<cstdio>
    #define ll long long
    ll slove (ll n)
    {
    	if(n % 2 == 1) return (n/2) * (n/2);
    	if(n % 4 == 0) return (n/4-1)*(n/4-1);
    	if(n % 4 == 2) return  2*slove(n+2);
    }
    int main ()
    {
    	int T;
    	ll n;
    	scanf("%d", &T);
    	while(T--)
    	{
    		scanf("%lld", &n);
    		ll ans = slove(n);
    		printf("%lld\n", ans);
    	}
    	return 0;
    }
    


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值