HDU-5970 推公式+分块

题目

最大公约数

题解

这道题 x 非常大,所以可以通过 x = sy + k, 用 y代换掉x 。
然后易证gcd(k,y) == gcd(x,y),而且这两个 gcd 计算出来的 c 也是一样的。
由于 s 的范围是[0,(x - k)/y], 所以当 x 特别大的时候,s 也特别大。s 也需要分块处理,另 s = j*c + i,i 的范围是[0, 1-c],所以可以按照 i 的不同取值进行分块。

代码如下

#include <algorithm>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
int c;
int calc(int x, int y){
    c = 0;
    while(y>0){
        c += 1;
        int t = x%y;
        x = y;
        y = t;
    }
    return x;
}

int n,m,p;

int main(){
    int T;
    cin >> T;
    while(T--){
        cin>>n>>m>>p;
        ll res = 0;
        for(int y=1; y<=m; y++){
            for(int k=0; k<y; k++){
                ll gcd = calc(k,y);
                ll ms = (n-k)/y;
                ll a = k*y/gcd/gcd;
                ll b = y*y/gcd/gcd;
                ll tmp1, tmp2;

                ll t = ms/c;
                for(int i=0; i<c; i++) {
                    ll time = t*(t-1)/2;
                    tmp1 = (a+i*b)/c * t;
                    tmp2 = time*b;
                    res = (res + tmp1 + tmp2) % p;
                }

                ms = ms % c;
                for(int i=0;i<=ms;++i) {
                    res = ((res + (a+i*b)/c) % p + b * t) % p;
                }
            }
        }
        cout<<res<<endl;
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值