2019icpc徐州 E题 Multiply(pollard_rho)

样例输入复制

2
3 10 10
2 3 4
2 2 10
1 1

样例输出复制

2
8

O(1) 快速乘 你能秒我??

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <time.h>
#include <random>

typedef long long LL;
using namespace std;

int t, n;
LL x, y, a[100005];
mt19937 rd(time(0));

LL ksm(LL a, LL b, LL mod){
    return (a * b - (LL)((long double)a / mod * b) * mod + mod) % mod;
}

LL poww(LL x, LL num, LL mod){
    LL res = 1;
    x %= mod;
    while(num){
        if(num & 1) res = ksm(res, x, mod);
        x = ksm(x, x, mod);
        num >>= 1;
    }
    return res;
}

struct Mill{
    
    LL n, fac[22000][2], bk[22000]; int tot;

    const int C = 2307;
    const int S = 8;

    bool check(LL a, LL n){
        LL m = n - 1, x, y = 0;
        int j = 0;
        while (!(m & 1)) m >>= 1, j++;
        x = poww(a, m, n);
        for (int i = 1; i <= j; x = y, i++){
            y = ksm(x, x, n);
            if (y == 1 && x != 1 && x != n - 1) return 1;
        }
        return y != 1;
    }

    bool miller_rabin(LL n){
        if(n < 2){
            return 0;
        }else if (n == 2){
            return 1;
        }else if (!(n & 1)){
            return 0;
        }
        for (int i = 0; i < S; ++i){
            if (check(rd() % (n - 1) + 1, n)) return 0;
        }
        return 1;
    }

    LL pollard_rho(LL n, int c){
        LL i = 1, k = 2, x = rd() % n, y = x, d;
        while (1) {
            ++i; x = (ksm(x, x, n) + c) % n;
            d = __gcd(y - x, n);
            if(d > 1 && d < n){
                return d;
            }
            if(y == x){
                return n;
            }
            if(i == k){
                y = x;
                k <<= 1;
            }
        }
    }

    void findfac(LL n, int c){
        if(n == 1){
            return ;
        }
        if(miller_rabin(n)){
            bk[++*bk] = n;
            return ;
        }
        LL m = n;
        while(m == n){
            m = pollard_rho(n, c--);
        }
        findfac(m, c);
        findfac(n / m, c);
    }
    void gao(LL _n){
        n = _n; *bk = 0;
        findfac(n, C);
        sort(bk + 1, bk + 1 + *bk);
        fac[1][0] = bk[1];
        fac[1][1] = 1;
        tot = 1;
        for (int i = 2; i <= *bk; i++){
            if (bk[i] == bk[i - 1]) {
                ++fac[tot][1];
            }else{
                ++tot;
                fac[tot][0] = bk[i];
                fac[tot][1] = 1;
            }
        }
    }

}mill;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    scanf("%d", &t);
    while(t--){
        scanf("%d %lld %lld", &n, &x, &y);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        mill.gao(x);
        LL ans = 1LL << 60;
        for (int i = 1; i <= mill.tot; i++){
            LL nu = 0, tp, w;
            // printf("%lld %lld\n", fac[i], num[i]);
            for (int j = 1; j <= n; j++){
                tp = log(a[j]) / log(mill.fac[i][0]);
                w = 1;
                for (int k = 1; k <= tp; k++) w *= mill.fac[i][0], nu += a[j] / w;
            }
            w = 1;
            tp = log(y) / log(mill.fac[i][0]);
            for (int k = 1; k <= tp; k++) w *= mill.fac[i][0], nu -= y / w;
            // printf("%lld %lld\n", fac[i], tp);
            nu = max(0LL, -nu);
            ans = min(ans, nu / mill.fac[i][1]);
        }
        printf("%lld\n", ans);
    }

    return 0;
}
/**/

 

 

全部评论

相关推荐

最终还是婉拒了小红书的offer,厚着脸皮回了字节。其实这次字节不管是组内的氛围、HR的沟通体验,都比之前好太多,开的薪资也还算过得去,这些都是让我下定决心的原因之一。但最核心的,还是抵不住对Agent的兴趣,选择了Ai&nbsp;Coding这么一个方向。因为很多大佬讲过,在未来比较火的还是属于那些更加垂类的Agent,而Ai&nbsp;Coding恰好是Coding&nbsp;Agent这么一个领域,本质上还是程序员群体和泛程序员群体这个圈子的。目前也已经在提前实习,也是全栈这么一个岗位。就像最近阿里P10针对前端后端等等不再那么区分,确实在Agent方向不太区分这个。尤其是我们自己做AI&nbsp;Coding的内容,基本上90%左右的内容都是AI生成的,AI代码仓库贡献率也是我们的指标之一。有人说他不好用,那肯定是用的姿态不太对。基本上用对Skill、Rules&nbsp;加上比较好的大模型基本都能Cover你的大部分需求,更别说Claude、Cursor这种目前看来Top水准的Coding工具了(叠甲:起码在我看来是这样)。所以不太区分的主要原因,还是针对一些例如Claude&nbsp;Code、Cursor、Trae、Codex、CC等一大堆,他们有很多新的概念和架构提出,我们往往需要快速验证(MVP版本)来看效果。而全栈就是这么快速验证的一个手段,加上Ai&nbsp;Coding的辅助,目前看起来问题不大(仅仅针对Agent而言)。而且Coding的产品形态往往是一个Plugin、Cli之类的,本质还是属于大前端领域。不过针对业务后端来看,区分还是有必要的。大家很多人也说Agent不就是Prompt提示词工程么?是的没错,本质上还是提示词。不过现在也衍生出一个新的Context&nbsp;Eneering,抽象成一种架构思想(类比框架、或者你们业务架构,参考商品有商品发布架构来提效)。本质还是提示词,但是就是能否最大化利用整个上下文窗口来提升效果,这个还是有很多探索空间和玩法的,例如Cursor的思想:上下文万物皆文件,&nbsp;CoWork之类的。后续也有一些Ralph&nbsp;Loop啥的,还有Coding里面的Coding&nbsp;Act姿态。这种才是比较核心的点,而不是你让AI生成的那提示词,然后调用了一下大模型那么简单;也不是dify、LangGraph搭建了一套workflow,从一个node走到另外一个node那么简单。Agent和WorkFLow还是两回事,大部分人也没能很好的区分这一点。不过很多人说AI泡沫啥啥啥的,我们ld也常把这句话挂在嘴边:“说AI泡沫还是太大了”诸如此类。我觉得在AI的时代,懂一点还是会好一点,所以润去字节了。目前的实习生活呢,除了修一些Tools的问题,还包括对比Claude、Cursor、Trae在某些源码实现思想上的点,看看能不能迁移过来,感觉还是比较有意思。不过目前组内还是主要Follow比较多,希望下一个阶段就做一些更有创新的事情哈哈。这就是一个牛马大学生的最终牧场,希望能好好的吧。说不定下次发的时候,正式AI泡沫结束,然后我又回归传统后端这么一个结局了。欢迎交流👏,有不对的🙅不要骂博主(浅薄的认知),可以私聊交流
码农索隆:和优秀的人,做有挑战的事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务