首页 > 其他分享 >Luogu5435 基于值域预处理的快速 GCD & Leetcode2543 - binary GCD -

Luogu5435 基于值域预处理的快速 GCD & Leetcode2543 - binary GCD -

时间:2023-02-02 22:48:13浏览次数:73  
标签:__ binary GCD int long Luogu5435 az gcd

题目链接:https://www.luogu.com.cn/problem/P5435

请忽略题目名称
学到一个科技:binary GCD,能够快速求出两个数 GCD(从这道题来看已经接近 \(O(1)\) 了)

代码:

// by SkyRainWind
#include <bits/stdc++.h>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define pii pair<int,int>
#define pb push_back

using namespace std;

typedef long long ll;
typedef long long LL;

const int inf = 1e9, INF = 0x3f3f3f3f;

int bin_gcd(int a,int b){
    int az = __builtin_ctz(a);
    int bz = __builtin_ctz(b);
    int z = min(az, bz);
    b >>= bz;
    while (a) {
        a >>= az;
        int diff = a - b;
        az = __builtin_ctz(diff);
        b = min(a, b), a = abs(diff);
    }
    return b << z;
}

const int maxn=5005,mod=998244353;
int n,a[maxn],b[maxn],ans[maxn];
signed main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)scanf("%d",&b[i]);
	
	for(int i=1;i<=n;i++){
		int cs = i, r=0;
		for(int j=1;j<=n;j++){
			(r += 1ll*cs*bin_gcd(a[i], b[j])%mod);
			if(r >= mod)r -= mod;
			cs = 1ll*cs*i%mod;
		}
		printf("%d\n",r);
	}

	return 0;
}

利用这个思想来看 Leetcode 双周赛的 T4:

结论:如果 \(gcd(a,b)\) 是 \(2^k\) 就 true 否则 false
考虑倒推,如果 \(x,y\) 都是偶数,那么都除以 2 即可,\(gcd\) 多一个 2
如果一奇一偶,那显然除以一个 2 不影响 gcd,
否则,\((x,y) \rightarrow (x,x+y) \rightarrow (x,\frac{x+y}{2})\) (不妨设 \(x < y\))
如果 \(x==y\) 且均为奇数,那么只能是最终态 \((1,1)\)
注意到只有两个偶数的时候会改变 gcd

标签:__,binary,GCD,int,long,Luogu5435,az,gcd
From: https://www.cnblogs.com/SkyRainWind/p/17087635.html

相关文章

  • LeetCode dynamic binary tree generator component All In One
    LeetCodedynamicbinarytreegeneratorcomponentAllInOnedemosgif//破解,钻牛角尖https://leetcode.com/problems/maximum-depth-of-binary-tree/descrip......
  • It's possible to create a function auto generator this special test case binary
    It'spossibletocreateafunctionautogeneratorthisspecialtestcasebinarytreefromarrayinjavascript?Iwanttoautogeneratethosetestcasesinmy......
  • CodeForces 1762D GCD Queries
    Preface比较神仙的交互题,居然自己胡出来了。不是很建议拿到题直接往题解区冲,这种题做一道少一道。Solution下面简称\(\gcd(p_i,p_j)\)为\(\text{zyf}(i,j)\)。这个......
  • 沪深Level-1 Binary行情消息类型对比
    一、概述沪深交易所Level-1行情都有Binary格式,本篇是从消息类型方面进行比较。二、消息类型比较       三、汇总1、基础数据比较登录、注销、心跳这些......
  • Exgcd(扩展欧几里得算法)
    其实挺简单。GCD(辗转相除法)定理:\[\gcd(a,b)=\gcd(b,a\%b)\]证明:\[\text{设}a=kb+r\text{,则}r=a\bmodb\]\[\text{若}c\text{是}a,b\te......
  • 题解 CF277E【Binary Tree on Plane】
    费用流。可以将原问题转化为类似于匹配的问题,只不过这种匹配并不是一对一的。具体地,将每个点\(u\)拆成两个点\(u_\text{fa},u_\text{son}\),设源点为\(s\)、汇点为\(t......
  • Codeforces Round #846 (Div. 2) B. GCD Partition
    B.GCDPartition参考题解链接:CodeforcesRound#846(Div.2)—Tutorial-Codeforces题意:给一个长度为n的序列,并将其分成连续的k块(k>1),得到序列......
  • B. GCD Partition
    B.GCDPartitionWhileatKira'shouse,Josukesawapieceofpaperonthetablewithataskwrittenonit.Thetasksoundedasfollows.Thereisanarray$a$......
  • binary_cross_entropy and binary_cross_entropy_with_logits
    importtorchimporttorch.nnasnnimporttorch.nn.functionalasF模拟的输入x变量:4分类问题batch_size,n_classes=10,4x=torch.randn(batch_size,n_classe......
  • gcd(a, b) = gcd(b, a % b)的证明
    \[\hugegcd(a,b)=gcd(b,a\modb)的证明\]首先假设存在\(a,b,c\)这三个数,若其满足\(a=q*b+c\),证明:\((a,b)=(b,a\modb)\)证明:\(\because\)首先可以由\(......