codeforce 1263C (整除分塊)

題意描述

給定你一個n,讓你求出所有的n/i(下取整)

思路

這道題想了很久,打表發現了規律,每個值相同的塊,最後一個因子都是n/(n/i),但找到規律以後不知道該如何實現,看了題解以後才發現這是一道整數分塊的問題。
核心代碼:

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

AC代碼

#include<bits/stdc++.h>
#define x first
#define y second
#pragma GCC optimize(2)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++14"
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=100010;
const int M=1e6;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
int n,m;
int a[N];
int main()
{
    IOS;
    int T;cin>>T;
    while(T--){
        int n;cin>>n;
        int cnt=1;
        for(int l=1,r;l<=n;l=r+1){
            r=n/(n/l);
            a[cnt++]=n/l;
        }
        sort(a,a+cnt);
        cout<<cnt<<endl;
        for(int i=0;i<cnt;i++) cout<<a[i]<<' ';
        cout<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章