SDAU训练指南-数论2L(等差数列)n*(a1+an)/2

Now a days a very common problem is: “The coordinate of two points in Cartesian coordinate system is (200, 300) and (4000, 5000). If these two points are connected we get a line segment. How many lattice points are there on this line segment”. You will have to do a similar task in this problem — the only difference is that the terminal coordinates can be fractions. Input First line of the input file contains a positive integer N (N ≤ 50000) that denotes how many lines of inputs follow. This line is followed by N lines each of which contains four floating-point numbers x1, y1, x2, y2 (0 < |x1|, |y1|, |x2|, |y2| ≤ 200000). These floating-point numbers has exactly one digit after the decimal point. Output For each line of input except the first line produce one line of output. This line contains an integer which denotes how many lattice points are there on the line segment that connects the two points (x1, y1) and (x2, y2). Sample Input 3 10.1 10.1 11.2 11.2 10.2 100.3 300.3 11.1 1.0 1.0 2.0 2.0 Sample Output 1 0 2

#include<iostream>
using namespace std;
#define ll long long
int main()
{
    ll n,k;
    while(cin>>n>>k)
    {
        if(n>=k)
        {
            ll ans = 0;
            for(ll l = 1, r; l <= k; l = r + 1)
            {
                r = k / (k / l);
                ll a1=k%l,length=(r-l+1),an=k%r;
                ans += length*(a1+an)/2;
            }
            cout<<ans+(n-k)*k<<endl;
        }
        else
        {
            ll ans = 0;
            for(ll l = 1, r; l <= n; l = r + 1)
            {
                r = min(n,k / (k / l));
                ll a1=k%l,length=(r-l+1),an=k%r;
                ans += length*(a1+an)/2;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值