HDU4135 容斥原理+质因子+DFS

【PLIO学习总结】laserMapping中的时间戳与状态更新逻辑 本文仅用于个人学习总结记录。如有错误,请批评指正。 阅读详情

HDU4135Co-prime
题意:求区间[A , B]内与N互质的数的个数

分析:
将问题转换一下,用[1 , B]内与N互质的数的个数减去[1 , A-1]内与N互质的数的个数,现在就变成要求[1 , C]内与N互质的数的个数,这样不好求,但是求与N互质的数的个数(设为num)很简单,那么最后答案就是C-num
如何求与N不互质的数的个数:
比如C=12,N=30
第一步:求出N的所有质因子2,3,5
第二步:[1 , C]内是N的因子的倍数的数肯定与N不互质:

        {2,4,6,8,10,12} 有C/2 = 6个
        {3,6,9,12} 有C/3 = 4个
        {5,10} 有C/5 = 2个

注意:不能直接加起来6+4+2 = 12个,因为有的数重复出现了,怎么去掉重复呢?
第三步:使用容斥原理,公式为

         C/2 + C/3 +C/5 
         - C/(2*3) - C/(2*5) - C/(3*5)
         + C/(2*3*5)

总结了就是奇数个数加,偶数个数减
第四步:用DFS实现,质因子相互组合符合k个数的排列组合(k=1、2、3……),对于每个k用DFS扫一遍

那么如何快速求整数N的所有质因子呢?

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define LL long long

LL A,B,N,a[1000];
int n;
void Init(){//求出N的所有质因子,方法很像素数筛
    n=0;
    for(int i=2;i*i<=N;i++){
        if(N%i==0)a[++n]=i;
        while(N%i==0)N/=i;
    }
    if(N>1)a[++n]=N;
}

int k;
LL num;
void dfs(LL C,LL sum,int cur,int len){//求[1,C]内k个因子的倍数个数
    if(len>k)return;
    if(len==k){
        if(k%2)num+=C/sum;
        else num-=C/sum;
        return;
    }
    for(int i=cur;i<=n;i++)
        dfs(C,sum*a[i],i+1,len+1);
}

LL solve(LL C){//对每个k用dfs扫一遍
    num=0;
    for(k=1;k<=n;k++)
        dfs(C,1,1,0);
    return num;
}

int main(){
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        scanf("%lld%lld%lld",&A,&B,&N);
        Init();
        printf("Case #%d: %lld\n",t,B-A+1-solve(B)+solve(A-1));
    }
    return 0;
}
告别TI官网迷路!手把手教你用C2000Ware为TMS320F28027搭建Proteus仿真工程模板 本文详细指导如何使用C2000Ware为TMS320F28027搭建Proteus仿真工程模板,帮助DSP开发者快速上手。从资源获取、工程目录设计到关键文件配置,再到CCS与Proteus的联调技巧,全面解析仿真工程搭建的每个步骤,特别适合初学者和工程师参考。 阅读详情

相关推荐

【STM32G474实战】RS485通信的DMA优化策略与性能对比

本文深入探讨了在STM32G474微控制器上优化RS485通信的DMA策略。通过对比轮询、中断和DMA三种数据传输方式的代码实现与性能,揭示了DMA在解放CPU、降低占用率方面的巨大优势。文章提供了详细的DMA配置指南、高级应用技巧(如结合空闲中断)以及一个高可靠通信框架的实战思路,旨在帮助开发者构建高效、实时的工业控制系统。

weixin_29268331的博客 301

【dfs分解质因数】CF27E

爆搜分解质因子的模板题,记录一下板子。

weixin_62528401的博客 229

从零搭建一个虚拟ECU:手把手用Autosar CP模块模拟汽车灯控系统(基于Vector工具链)

本文详细介绍了如何使用Vector工具链从零搭建一个基于Autosar CP架构的虚拟ECU系统,模拟汽车灯光控制功能。通过环境搭建、模块配置、通信栈设置到灯光控制逻辑实现的全流程指导,帮助开发者掌握Autosar CP模块术语的实际应用,并提供了系统集成与测试的实用方案。

weixin_42628688的博客 311

牛客算法心得——kotori和素因子(dfs)

回溯的正常状态,一般来说,dfs上下两面都是相反的。试题链接。

晴天学长 568

Openclaw Onboarding向导全攻略——如何配置你的第一个AI助手

很多新手在成功运行 `openclaw --version` 后,面对黑底白字的终端就陷入了迷茫:“安装好了,然后呢?” 本篇我们就彻底解决这个问题,带你完整走完 `openclaw onboard` 交互式初始化配置向导的每一步,从“安装完成”到“真正能用”,只差这一个向导的距离。 本文为2026年3月最新版,适配OpenClaw v2026.2.x 稳定版,所有步骤均经过实测验证,可直接落地。

夏日清风 1917

【Creator】cocoscreator 2.4 apk还原为工程 (1)

Cocos Creator 2.4 还原图片 meta文件

jokesuser的博客 2259

HDU - 4135 Co-prime 容斥原理

要找 a 到 b 之间 和k 互质的数,只要找到和 k 不互质的数就好了 先对 k 进行分解,分解成全部的质因子 然后找就是了 2333 #include #include #include #include #include #include #include #include #include #include #include #include #include

床前明月光 336

Android布局基础与UI设计入门:打造精美应用界面

恭喜你!现在你已经掌握了Android UI开发的基础知识,包括各种布局类型、常用控件的使用以及一些UI优化技巧。这些知识将帮助你设计出既美观又实用的Android应用界面。

m0_65382359的博客 2056

hdu 4135 Co-prime(容互原理 DFS+位运算求解)

hdu 4135 Co-primeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6051 Accepted Submission(s): 2411Problem Description Given a number N, you

AixsCode 313

Qwen-Image LoRA训练实战:从数据准备到异常修复的全流程解析

本文详细解析了Qwen-Image LoRA训练的全流程,从数据准备到参数优化再到异常修复。通过实战案例展示了如何利用LoRA技术微调Qwen-Image模型,提升中文场景下的图像生成质量,特别针对手脚生成异常等问题提供了有效解决方案。文章还分享了中文提示词优化技巧和高级训练方法,帮助开发者高效训练出高质量的LoRA模型。

lemon的博客 965

Ubuntu22.04下RTX2080显卡驱动、CUDA、cuDNN保姆级避坑安装指南(实测有效)

本文提供Ubuntu22.04下RTX2080显卡驱动、CUDA和cuDNN的详细安装指南,涵盖前期准备、驱动安装、CUDA版本选择、cuDNN配置及常见问题解决。通过实测有效的步骤和优化建议,帮助用户快速搭建稳定的深度学习环境,避免常见陷阱。

weixin_42540248的博客 284

HOJ 2576 Simple Calculation 容斥原理(DFS)

输入两个数n,m,再输入一个有n元素的集合x[n]。求从[1,m]中有多少个数,能被x[n]中至少一个数整除。 这题利用了容斥原理。容斥原理在编程中实现起来一般有dfs+剪枝和按位运算两种。按位运算易于调试,但是不适于大数据量。 第一次写还是感觉挺困难的,参考了别人的代码。是用dfs写的。参照他的思路我自己又写了一遍,却总是WA,找了好久也没发现错在哪。。。先贴上来吧。 自己写的...

weixin_30384031的博客 205

PVE启动卡在loading initial ramdisk

pve显卡直通导致的异常解决

学而时习之 6687

HDU 4135 容斥原理

HDU 4135 容斥原理 链接 题意:给你一个区间[a,b][a,b][a,b] ,求这个区间内与n互质的数的个数。 题解:可以先求出[1,b][1,b][1,b]内与n互质的数的个数,然后再求出[1,a−1][1,a-1][1,a−1]内与n互质的数的个数,然后用前者减去后者即是答案。前者可以采用容斥原理求。对容斥原理不了解的可以看链接。首先求出n的所有质因子,然后dfs求容斥系数。 代码:...

weixin_43855330的博客 307

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

HDU 4135 Co-prime (容斥原理, 数学)

HDU 4135 Co-prime (容斥原理, 数学)

979

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

HDU - 4135 Co-prime (容斥原理 求 1-n 与 k互质的数的个数模板)

HDU - 4135 Co-prime (容斥原理 求 1-n 与 k互质的数的个数模板) Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-...

weixin_43179892的博客 391

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

hdu 4135 Co-prime(容斥原理)

http://acm.hdu.edu.cn/showproblem.php?pid=4135 求连续区间[a,b]内与n互质的数的个数。 因为a,b相当大,考虑用容斥原理。只需先求出[a,b]内与n不互质的数的个数,等于[1,b]内与n不互质的个数 - [1,a-1]内与n不互质的个数。问题转化为求【1,m】内与n不互质的数的个数。 先对n分解质因子,[1,m]内是n的质因子的倍数的

1162

HDU4135 (求a~b内与n互素的数的个数) 容斥原理

掌握了容斥原理后,便会发现,这是一道简单的容斥原理的题。 题目描述:给定A, B, N (1 <= A <= B <= 10^15,1<=N <= 10^9).求[A,B]区间内与N互素的数的个数 看到这道题,自然联想到跟素数相关的知识,所以在做这道题之前,我忍不住整理一下关于素数的基本算法。 1、素数筛选法(求1~n的素数):http://blog.youkuaiyun.com/yzj577/article/details/38147433 2、验证素数:http://blog.c

旧时相识的专栏 1842

hdu-4135(容斥原理)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6840    Accepted Submission(s): 2699   Problem Description Given a number N, you ar...

yu121380的博客 414

hdu4135 Co-prime

题意: 给定a,b,n,要求求出a~b区间内与n互质的数的对数。 思路: 乍一看是到欧拉函数的题目(终于不是欧拉函数假扮最大公约数了),可是仔细一想这里的坑:n的数据范围虽然比a、b要大,可是n确实有可能会小于a、b呀,欧拉原理只能求小于n的与n互质的数字的个数,即这里欧拉函数失效了。 所以正确的思路是,容斥原理。 因为[a,b] = [1,b]-[1,a-1],那么同时算出1~b内与n...

雨潇的博客 153

HDU - 4135 Co-prime(容斥原理)

题目链接:点击查看 题目大意:给出一个区间[l,r],再给出一个n,问区间中有多少个数与n互质 题目分析:容斥原理应用的经典题目,马克一篇非常不错的博客,留着以后慢慢消化: https://blog.youkuaiyun.com/m0_37286282/article/details/78869512 那么这个题目转换一下,先求1~m中有多少个与n互质的数,这样cal(r)-cal(l-1)就是答案了...

Falcon的博客 438

hdu4135 Co-prime(容斥原理两种写法)

http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[a,b]范围内与n互质的数的个数。 思路:这题数据貌似很弱,用欧拉函数算下就知道10^9的质因数的个数最大约等于10^9,这里开大了显然爆内存,1005居然也能过。。 和hdu1286类似,不同点在于那题是1~n-1内与集合不互质的数,而这里是[a,b]内,而且a和b的

Iguodala的博客 554

HDU4135Co-prime(容斥原理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135题目解析:给你一个闭区间[A,B](1 <= A <= B <= 1015),以及一个正整数N,求[A,B]中与N互质的个数,可以先求[1,B]中与N互质的个数,在求[1,A-1]中与N互质的个数。之后...

weixin_41798517的博客 175

Co-prime HDU - 4135

Co-prime HDU - 4135 Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. Two integers are said to be co-prime or relativel...

Willen_的博客 238

HDU 4135 Co-prime (容斥原理+质因数分解)

这题只要知道质因数的性质就很容易做了。任意一个正整数(除了1)都可以分解成有限个质数因子的乘积。 那么假如两个数互质,那么这两个数肯定至少各有一个对方没有的质因子。所以若一个数跟n不互质,那么这个的数的质因子肯定也都属于n的质因子,那么就用容斥原理求出所有跟n不互质的所有数的个数。然后再用总的减去即可。 代码如下: #include #include #include #include

scf0920 1065

HDU4135容斥原理

题意:给三个数a、b和n,求区间[a,b]中与n互质的数的个数。1 15,1 9。 解析:本题直接求与n互质的数的话,由于区间范围太大,肯定超时。因此可以用全部的数,减去与n不互质即有公因子的数的个数。 1、先求出n的所有质因子 2、由于区间【1,m】中,含有质因子x的数的个数为m/x,因此把含有x的数都减去,即减去m/x个。遍历所有的质因子,依次减去。但是这样减的话,会多减去一些数,正

zhj_fly的博客 406
上一篇: 一次求多个整数的质因数
下一篇: Java大数类用法
FightingForFinal
博客等级 码龄11年 24粉丝 100原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值