Codeforces 1263C. 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;
}
 

猜你喜欢

转载自www.cnblogs.com/zzl-dreamfly/p/11965723.html