C. Everyone is a Winner!(整除分块)

传送门

题意:

输入一个数n,求出n/i(i取任意)向下取整有几种数,并输出

思路:

整除分块裸题

for(int l=1,r;l<=n;l=r+1)
{
    r=n/(n/l);
    a[++cnt]=n/l;
}
 

cnt代表分块的块的个数,每个块里的值为a[i]=n/l,l为块的左端点,r为块的右端点r=n/(n/l)

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int N=2e6+50;
const int mod=1e9+7;
int a[N];
map<int,int>mp;
void solve(int t)
{
    int n;scanf("%d",&n);
    int cnt=0;
    for(int l=1,r;l<=n;l=r+1)
    {
        r=n/(n/l);
        a[++cnt]=n/l;
    }
    printf("%d\n0 ",cnt+1);//别忘输出0,i大于n时,n/i都为0
    for(int i=cnt;i>=1;--i)
        printf("%d ",a[i] );
    printf("\n");
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);for(int i=1;i<=t;i++)solve(i);
    return 0;
}
 

原文地址:https://www.cnblogs.com/zzl-dreamfly/p/11965723.html

时间: 2024-08-30 15:17:57

C. Everyone is a Winner!(整除分块)的相关文章

LightOJ 1098(均值不等式,整除分块玄学优化)

We all know that any integer number n is divisible by 1 and n. That is why these two numbers are not the actual divisors of any numbers. The function SOD(n) (sum of divisors) is defined as the summation of all the actual divisors of an integer number

luogu2261余数求和题解--整除分块

题目链接 https://www.luogu.org/problemnew/show/P2261 分析 显然\(k\) \(mod\) \(i=k-\lfloor {k/i}\rfloor\) \(\times\) \(i\),于是我们只需要求\(N * k-\sum_{i=1}^N {\lfloor {k/i}\rfloor\times i}\) 这里就需要数论分块,也称作整除分块的知识 结论: \(\forall{i} \in [x,\lfloor {k/{\lfloor {k/x}\rfl

数论分块之整除分块

前言 最近在学莫比乌斯反演,然而只看懂了莫比乌斯函数,然后反演看着一脸懵逼,最后只看懂了数论分块里面的一个分支内容(也是莫比乌斯反演的前置姿势),整除分块 于是写一篇博文记录一下整除分块 整除分块 整除分块是用于快速处理形似 \[ \sum_{i=1}^{n}{\lfloor \frac{n}{i} \rfloor} \] 的式子的方法 很显然,这个可以\(O(n)\)得到答案.但是,在某些题目中,毒瘤出题人将数据加强到了\(10^{10}\)以上,这个时候我们就无法通过\(O(n)\)的解法来

[Bzoj 2956] 模积和 (整除分块)

整除分块 一般形式:\(\sum_{i = 1}^n \lfloor \frac{n}{i} \rfloor * f(i)\). 需要一种高效求得函数 \(f(i)\) 的前缀和的方法,比如等差等比数列求和或对于积性函数的筛法等,然后就可以用整除分块的思想做. 题目解法 化公式变成比较方便的形式: \(\ \sum_{i = 1}^n \sum_{j = 1}^m (n \mod i)(m \mod j), i \ne j\) \(= \sum_{i = 1}^n \sum_{j = 1}^m

UVALive - 3521 Joseph&#39;s Problem (整除分块)

给定$n,k$$(1\leqslant n,k\leqslant 10^9)$,计算$\sum\limits _{i=1}^nk\: mod\:i$ 通过观察易发现$k\%i=k-\left \lfloor \frac{k}{i} \right \rfloor*i$,因此我们考虑把$\left \lfloor \frac{k}{i} \right \rfloor$的值相同的$i$分成一组直接求和,复杂度为$O(\sqrt{n})$. 整除分块原理(选自某dalao博客) 1 #include<b

整除分块模板

这里大概讲解一下整除分块的原理和效果. 比如我们要求某个i的区间中,n/i的和是多少,但是其实你会发现,在一些连续的区间中,n/i是相等的,而整除分块的目的,便是按照n进行分块 使得可以跳过这些n/i是相等的这些区间,使得复杂度将到根号n for (int l=1,r;l<=n;l=r+1){ r=n/(n/l); // printf("%d ",r); } 原文地址:https://www.cnblogs.com/bluefly-hrbust/p/10600035.html

【纪中集训2019.3.29】整除分块

题目 描述 ? 本题的背景是整除分块: ? 定义一个数列$a_{n,i} ? = ?\lfloor \frac{n}{i} \rfloor $ ; ? 求$\sum_{i=l}^{r} mex(a_n) $ ; ? 其中\(mex\)表示序列中最小的没有出现过的自然数: ? 答案对\(998244353\)取模 : 范围 ? \(1 \le T \le 65536 \ , \ 1 \le l ,r \le 10^{36}\) : ? 评测系统支持使用 $ _ _ int218 $ ,但是不能直接

CodeForces 1202F(数论,整除分块)

题目 CodeForces 1213G 做法 假设有\(P\)个完整的循环块,假设此时答案为\(K\)(实际答案可能有多种),即每块完整块长度为\(K\),则\(P=\left \lfloor \frac{N}{K} \right \rfloor\) 假设循环快中有\(p_a,p_b\)个\(A\)和\(B\),则 \(p_a\cdot P\le a\Longrightarrow p_a\le \left \lfloor \frac{a}{P} \right \rfloor\) \(p_a\cd

整除分块加速取余

传送门 \(\sum_{i = 1}^{n}k \ mod \ i\) \(k \ mod \ i = k - \lfloor \frac{k}{i} \rfloor*i\) \(\sum_{i = 1}^{n}k \ mod \ i = kn - \sum_{i = 1}^{n}\lfloor \frac{k}{i}\rfloor*i\) 比整除分块模板多了i,而i在每一个分块是一个等差数列,可以用求和公式求. #include <iostream> #include <cstdio&