結果

問題 No.3441 Sort Permutation 2
コンテスト
ユーザー 👑 ArcAki
提出日時 2026-02-06 22:16:05
言語 Rust
(1.92.0 + proconio + num + itertools)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 3,355 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,569 ms
コンパイル使用メモリ 215,468 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2026-02-06 22:16:41
合計ジャッジ時間 5,845 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#[inline(always)]
pub fn gcd(mut a: i64, mut b: i64)->i64{if a==0{return b;}else if b==0{return a;}let l1 = a.trailing_zeros();let l2 = b.trailing_zeros();
a >>= l1; b >>= l2;while a!=b{let x = (a^b).trailing_zeros();if a<b{swap(&mut a, &mut b)}a = (a-b)>>x;}a << l1.min(l2)}

const MULTI: bool = false;
#[fastout]
fn solve(){
    input!{
        n: usize,
        a: [Usize1; n],
    }
    let mut ans = vec![0; n];
    let mut used = vec![false; n];
    for i in 0..n{
        if used[i]{continue;}
        let mut cycle = Vec::new();
        let mut p = i;
        while !used[p]{
            used[p] = true;
            cycle.push(p);
            p = a[p];
        }
        let mut g = 0;
        for i in 0..cycle.len()-1{
            g = gcd(g, (cycle[i]as i64-cycle[i+1]as i64).abs());
        }
        let g = g as usize;
        ans[g] += cycle.len()-1;
    }
    for i in 1..n{
        for j in (2..).take_while(|&j| i*j < n){
            ans[i] += ans[j*i];
        }
    }
    for i in 1..n{
        println!("{}", ans[i]);
    }
}

//#[fastout]
fn main() {
    if MULTI{
        input!{
            t: usize,
        }
        for _ in 0..t{
            solve();
        }
    } else {
        solve();
    }
}

#[allow(unused_imports)]
use std::{
    convert::{Infallible, TryFrom, TryInto as _}, fmt::{self, Debug, Display, Formatter,},
    fs::File, hash::{Hash, Hasher, BuildHasherDefault}, iter::{Product, Sum}, marker::PhantomData,
    ops::{Add, AddAssign, Sub, SubAssign, Div, DivAssign, Mul, MulAssign, Neg, RangeBounds},
    str::FromStr, sync::{atomic::{self, AtomicU32, AtomicU64}, Once},
    collections::{*, btree_set::Range, btree_map::Range as BTreeRange}, mem::{swap},
    cmp::{self, Reverse, Ordering, Eq, PartialEq, PartialOrd},
    thread::LocalKey, f64::consts::PI, time::Instant, cell::RefCell,
    io::{self, stdin, Read, read_to_string, BufWriter, BufReader, stdout, Write},
};
pub trait SortD{ fn sort_d(&mut self); }
impl<T: Ord> SortD for Vec<T>{ fn sort_d(&mut self) {
    self.sort_by(|u, v| v.cmp(&u));
} }
pub trait Mx{fn max(&self, rhs: Self)->Self;}
impl Mx for f64{ fn max(&self, rhs: Self)->Self{if *self < rhs{ rhs } else { *self } }}
pub trait Mi{ fn min(&self, rhs: Self)->Self; }
impl Mi for f64{ fn min(&self, rhs: Self)->Self{ if *self > rhs{ rhs } else { *self } } }

pub trait Chmax: PartialOrd + Copy {
    fn chmax(&mut self, rhs: Self) {
        if *self < rhs { *self = rhs; }
    }
}
impl<T: PartialOrd + Copy> Chmax for T {}

pub trait Chmin: PartialOrd + Copy {
    fn chmin(&mut self, rhs: Self) {
        if *self > rhs { *self = rhs; }
    }
}
impl<T: PartialOrd + Copy> Chmin for T {}

#[allow(unused_imports)]
use proconio::{input, input_interactive, marker::{*}, fastout};

#[allow(dead_code)]
const INF: i64 = 1<<60;
#[allow(dead_code)]
const I: i32 = 1<<29;
#[allow(dead_code)]
const MOD: i64 = 998244353;
#[allow(dead_code)]
const D: [(usize, usize); 4] = [(1, 0), (0, 1), (!0, 0), (0, !0)];
#[allow(dead_code)]
pub fn c2d(c: u8)->(usize, usize){match c{b'U'=>(!0,0),b'D'=>(1,0),b'L'=>(0,!0),b'R'=>(0,1),_=>unreachable!()}}
#[allow(dead_code)]
pub fn c2d_i64(c: u8)->(i64, i64){match c{b'U'=>(-1,0),b'D'=>(1,0),b'L'=>(0,-1),b'R'=>(0,1),_=>unreachable!()}}
#[allow(dead_code)]
const D2: [(usize, usize); 8] = [(1, 0), (1, 1), (0, 1), (!0, 1), (!0, 0), (!0, !0), (0, !0), (1, !0)];
0