text
stringlengths 49
983k
|
|---|
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
int main() {
int p, q;
cin >> p >> q;
while (true) {
int g = gcd(p, q);
if (g == 1) {
break;
}
p /= g;
q /= g;
}
int t = q;
int ans = 1;
for (int i = 2; i * i <= q; i++) {
if (t % i == 0) {
ans *= i;
while (t % i == 0) {
t /= i;
}
}
}
ans *= t;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define first fi;
#define second se;
using namespace std;
typedef pair<int, int> P;
int gcd(int a, int b){
if(a < b) swap(a, b);
if(b == 0) return a;
return gcd(b, a % b);
}
signed main(){
int p, q;
cin >> p >> q;
q /= gcd(p, q);
int ans = 1;
for(int i = 2; i * i <= q; i++){
if(q % i == 0) ans *= i;
while(q % i == 0){
q /= i;
}
}
if(q != 1) ans *= q;
cout << ans << endl;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
// b^k???q????´???°??¨?????????
//?????§??¬?´???°
int gcd(int a, int b) {
while (a && b) {
if (a >= b) a %= b;
else b %= a;
}
return a + b;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int p, q; cin >> p >> q;
q /= gcd(p, q);
int qq = q;
int ans = 1;
for(int i = 2; i * i <= qq; i++) {
if(q % i == 0) {
while(q % i == 0) {
q /= i;
}
ans *= i;
}
}
if(q > 1) ans *= q;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define SP << " "
#define LLi long long int
using namespace std;
vector<LLi> pri={1, 2, 3};
void PV(vector<int> pvv) {
rep(i, pvv.size()) cout << pvv[i] SP;
cout << endl;
}
void PVL(vector<LLi> pvv) {
rep(i, pvv.size()) cout << pvv[i] SP;
cout << endl;
}
bool VV(vector<int> vv1, vector<int> vv2) {
if(vv1.size()!=vv2.size()) return false;
rep(i, vv1.size()) if(vv1[i]!=vv2[i]) return false;
return true;
}
bool VVL(vector<LLi> vv1, vector<LLi> vv2) {
if(vv1.size()!=vv2.size()) return false;
rep(i, vv1.size()) if(vv1[i]!=vv2[i]) return false;
return true;
}
//vecotr<LLi>の総和
LLi SML(vector<LLi> smv) {
LLi tmp=0;
rep(i, smv.size()) tmp+=smv[i];
return tmp;
}
//n以下の素数を配列に{1, 2, 3, 5, 7, ...} make_prime_list
//i, j, flag, sqx, xを使用
void mplist(LLi x){
int flag;
for(int i=5;i<=x;i+=2){
flag=0;
for(int j=1;j<(int)pri.size();j++){
if(i%pri[j]==0){
flag=1;
break;
}
if(x<pri[j]*pri[j]) break;
}
if(flag==0) pri.emplace_back(i);
}
}
//nを素因数分解 root_prime_fac
//i, tmp, bin, lmt, xを使用
map<LLi, int> rpfac(LLi x){
int sqx=ceil(sqrt(x));
//cout<< "sqx=" << sqx SP << sqx*sqx << endl;//
auto bin = lower_bound(all(pri), sqx);
int lmt=bin-pri.begin()+1;
map<LLi, int> tmp;
//cout<< "lmt=" << lmt SP << pri[lmt] SP << pri[lmt]*pri[lmt] <<endl;//
if((int)pri.size()<lmt) cout<< "rpfac: pri size is small" <<endl;
for(int i=1;i<lmt;i++){
while(x%pri[i]==0){
x/=pri[i];
tmp[pri[i]]++;
//cout<< x <<endl;
//tmp[0]++;//0番に何個の積でできてるかをメモれる
}
if(x==1) break;
}
//if(x!=1) cout<< "prime_fac x=" << x <<endl;
return tmp;
}
//階乗を素因数分解
map<LLi, int> facfac(LLi x){
map<LLi, int> tmp;
auto bin = lower_bound(all(pri), x);
int lmt=bin-pri.begin()+1;
//cout<< "lmt=" << lmt SP << pri[lmt] SP << pri[lmt]*pri[lmt] <<endl;//
for(LLi i=1;i<lmt;i++){
int lp=0, di=0;
while(pow(pri[i], lp)<x){
lp++;
di+=x/pow(pri[i], lp);
}
if(di>0) tmp[pri[i]]=di;
}
return tmp;
}
//約数列挙
//i, j, k, tmp, mul, mを使用
vector<LLi> getfac(map<LLi, int> mp){
vector<LLi> tmp={1}, ad;
LLi mul;
for (auto p : mp) {
mul=1;
ad.clear();
LLi key = p.first;
int value = p.second;
rep(j, value){
mul*=key;
rep(k, tmp.size()) ad.push_back(tmp[k]*mul);
}
rep(j, ad.size()) tmp.push_back(ad[j]);
//PV(tmp);//
}
/*for(int i=1;i<v.size();i++){
if(v[i]==0) continue;
mul=1;
ad.clear();
rep(j, v[i]){
mul*=pri[i];
rep(k, tmp.size()){
ad.push_back(tmp[k]*mul);
}
}
//PV(tmp);
rep(j, ad.size()) tmp.push_back(ad[j]);
}*/
sort(all(tmp));
return tmp;
}
//素因数の積を計算
//i, tmp, vを使用
LLi defac(map<LLi, int> mp){
LLi tmp=1;
/*for(int i=1;i<v.size();i++){
if(v[i]!=0) tmp*=pow(pri[i], v[i]);
}*/
for (auto p : mp) {
LLi key = p.first;
int value = p.second;
tmp*=pow(key, value);
}
return tmp;
}
//素因数の積を計算
//i, tmp, vを使用
LLi defac2(map<LLi, int> mp){
LLi tmp=1;
/*for(int i=1;i<v.size();i++){
if(v[i]!=0) tmp*=pow(pri[i], v[i]);
}*/
for (auto p : mp) {
LLi key = p.first;
int value = p.second;
tmp*=pow(key, min(1, value));
}
return tmp;
}
//素数判定 is_prime
//ub, lb, x, iを使用
bool isp(LLi x){
//if(x==1) return false;//1を素数としないなら有効化
auto ub = upper_bound(all(pri), x);
auto lb = lower_bound(all(pri), x);
//xがでかいときは素数リストで割ってみる
if(lb==pri.end()){
for(int i=1;i<(int)pri.size();i++){
if(x%pri[i]==0) return false;
if(x<pri[i]*pri[i]) return true;
}
//cout<< "isp: pri size is small" <<endl;
//priのサイズが足りないときは地道にチェックする
for(LLi i=pri[pri.size()-1]+2;i*i<=x;i+=2){
if(x%i==0) return false;
}
return true;
}
return ub!=lb;
}
int main(){
LLi n, m, nl, ml;
vector<LLi> v;
map<LLi, int> mp, np;
cin>> n >> m;
mplist(100000);//nまでの素数列挙
//PVL(pri);//
np=rpfac(n);//mを√mまで素因数分解
mp=rpfac(m);//mを√mまで素因数分解
nl=defac(np);//mlにmpの総積を代入
ml=defac(mp);//mlにmpの総積を代入
//cout<< "m/ml=" << m/ml SP << isp(m/ml) <<endl;//
if(n!=nl) np[n/nl]++;//mpにm/mlを追加(√m以上の素数が約数の場合に必要な処理)
if(m!=ml) mp[m/ml]++;//mpにm/mlを追加(√m以上の素数が約数の場合に必要な処理)
//mの約数で√m以上の素数は1つしかないのでこれでOK
//mp=facfac(n);
for (auto pm : mp) {
auto mkey = pm.first;
auto mvalue = pm.second;
//cout<< mkey << "^" << mvalue SP;//mpの内訳を表示
}
//cout<< endl;//
for (auto p : np) {
auto key = p.first;
auto value = p.second;
//cout<< key << "^" << value SP;//mpの内訳を表示
mp[key]=max(0, mp[key]-value);
}
cout<< defac2(mp) <<endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
using namespace std;
using lli = long long int;
lli gcd(lli x, lli y) {
return (y == 0 ? x : gcd(y, x % y));
}
lli solve(lli p, lli q) {
const lli NMAX = 100008;
vector<lli> factor(NMAX, 0);
for (lli n = 2; n < NMAX; ++n) {
if (factor[n]) { continue; }
for (lli k = 2; k * n < NMAX; ++k) {
factor[k * n] = n;
}
}
lli ans = 1;
for (lli n = 2; q > 1 && n < NMAX; ++n) {
if (factor[n] || (q % n != 0)) { continue; }
while (q % n == 0) { q /= n; }
ans *= n;
}
ans *= q;
return max<lli>(2, ans);
}
int main() {
lli P, Q; cin >> P >> Q;
const lli G = gcd(P, Q);
P /= G, Q /= G;
cout << solve(P, Q) << endl;
return 0;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
///////////////////////////////////////////////////////////////
int gcd(int x, int y) {
if (x == 0) return y;
return gcd(y % x, x);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int p,q; cin >> p >> q;
int r = q / gcd(p,q);
int s = r;
vector<int> A;
for (int i = 2; i*i <= r; i++) {
if (s % i == 0) {
A.emplace_back(i);
while (s % i == 0) s /= i;
}
}
if (s != 1) A.emplace_back(s);
int ans = 1;
rep(i,A.size()) ans *= A[i];
cout << ans << endl;
}
|
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;
int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
map<int, int> f(int n){
map<int, int> ret;
for(int i = 2; i * i <= n; i++){
while(n % i == 0){
ret[i]++;
n /= i;
}
}
if(n > 1) ret[n]++;
return ret;
}
signed main(){
int p, q; cin >> p >> q;
int g = gcd(p, q);
p /= g;
q /= g;
map<int, int> m = f(q);
int b = 1;
for(auto x : m){
b *= x.first;
}
cout << b << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int x,int y){
if (y==0) return x;
return gcd(y,x%y);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int p,q,r=1; cin >> p >> q;
q/=gcd(p,q);
for (int i=2;i*i<=q;++i){
if (q%i==0){
r*=i;
while(q%i==0) q/=i;
}
}
cout << q*r << '\n';
}
|
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdio>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define pb push_back
#define P pair<int,int>
#define ll __int64
int p,q;
vector<int>primes;
const int MA=30000000;
bool t[MA*2];
void eratosu(){
t[1]=1;
for(int i=2;i<=MA;i++){
if(t[i]==0){
primes.push_back(i);
for(int j=i*2;j<=MA;j+=i)t[j]=1;
}
}
}
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int main(){
eratosu();
cin>>p>>q;
for(int i=0;i<primes.size();i++){
int now=primes[i];
while(q%now==0&&p%now==0)q/=now,p/=now;
}
int gc=gcd(p,q);
int np=p,nq=q;
//p=np/gc;
//q=nq/gc;
int ans=1,qq=q;
int cnt=0;
for(int i=0;i<primes.size();i++){
int now=primes[i];
if(q%now==0){ans*=now,cnt++;}
while(q%now==0)q/=now;
//if(now>sqrt(qq))break;
}
ans*=q;
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll
int gcd(int a, int b) {
return b != 0 ? gcd(b, a%b) : a;
}
signed main(void)
{
int p, q;
cin >> p >> q;
int r = gcd(p, q);
//cout << r << endl;
p /= r; q /= r;
set<int> s;
ll a = 2;
while(q >= a*a) {
if(q % a == 0) {
s.insert(a);
q /= a;
} else {
a++;
}
}
s.insert(q);
ll ret = 1;
for(int i: s) ret *= i;
cout << ret << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int main() {
int p, q;
cin >> p >> q;
p = gcd(p, q);
q /= p;
set<int> s;
for (int i = 2; i <= q;) {
if (i * i > q) {
i = q;
}
if (q % i == 0) {
q /= i;
s.insert(i);
} else {
i++;
}
}
int r = 1;
for (auto it = begin(s); it != end(s); it++) {
r *= *it;
}
cout << r << endl;
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
ll gcd(const ll a, const ll b)
{
if (a == 0 or b == 0) {
return (a == 0) ? b : a;
}
if (a > b) {
return gcd(b, a);
} else {
return gcd(b % a, a);
}
}
constexpr int SQRT = 100000;
bool isprime[SQRT];
int main()
{
ll p, q;
cin >> p >> q;
const ll g = gcd(p, q);
q /= g;
fill(isprime, isprime + SQRT, true);
for (ll i = 2; i < SQRT; i++) {
if (isprime[i]) {
for (ll j = 2; i * j < SQRT; j++) {
isprime[i * j] = false;
}
}
}
vector<ll> prime;
for (ll i = 2; i < SQRT; i++) {
if (isprime[i]) {
prime.push_back(i);
}
}
vector<ll> factor;
for (ll p : prime) {
if (q % p == 0) {
factor.push_back(p);
}
while (q % p == 0) {
q /= p;
}
}
if (q > 1) {
factor.push_back(q);
}
ll prod = 1;
for (const ll f : factor) {
prod *= f;
}
cout << prod << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) for(auto x : v){cout << x << " ";} cout << endl
#define printVS(vs) for(auto x : vs){cout << x << endl;}
#define printVV(vv) for(auto v : vv){for(auto&& x : v){cout << x << " ";}cout << endl;}
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp) for(auto p : vp) printP(p);
typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
map<int, int> primeFactorize(int n) {
map<int, int> res;
for (int i = 2; i * i <= n; ++i) {
while (n % i == 0) {
++res[i];
n /= i;
}
}
if (n > 1) ++res[n];
return res;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int p, q;
cin >> p >> q;
int g = gcd(p, q);
p /= g;
q /= g;
auto mp = primeFactorize(q);
int x = 1;
for (auto p : mp) {
x = lcm(x, p.first);
}
cout << x << endl;
}
|
/* -*- coding: utf-8 -*-
*
* 2706.cc: Let's Solve Geometric Problems
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_P = 32000;
/* typedef */
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
/* global variables */
bool primes[MAX_P + 1];
/* subroutines */
int gen_primes(int maxp, vi &pnums) {
memset(primes, true, sizeof(primes));
primes[0] = primes[1] = false;
int p;
for (p = 2; p * p <= maxp; p++)
if (primes[p]) {
pnums.push_back(p);
for (int q = p * p; q <= maxp; q += p) primes[q] = false;
}
for (; p <= maxp; p++)
if (primes[p]) pnums.push_back(p);
return (int)pnums.size();
}
bool prime_decomp(int n, vi &pnums, vpii& pds) {
pds.clear();
int pn = pnums.size();
for (int i = 0; i < pn; i++) {
int pi = pnums[i];
if (pi * pi > n) {
if (n > 1) pds.push_back(pii(n, 1));
return true;
}
if (n % pi == 0) {
int fi = 0;
while (n % pi == 0) n /= pi, fi++;
pds.push_back(pii(pi, fi));
}
}
return false;
}
int gcd(int m, int n) { // m >= n > 0
while (n > 0) {
int r = m % n;
m = n;
n = r;
}
return m;
}
int powi(int a, int b) {
int p = 1;
while (b) {
if (b & 1) p *= a;
a *= a;
b >>= 1;
}
return p;
}
/* main */
int main() {
vi pnums;
int pn = gen_primes(MAX_P, pnums);
//printf("pn=%d\n", pn);
int p, q;
scanf("%d%d", &p, &q);
int g = gcd(q, p);
//printf("gcd(%d,%d)=%d\n", p, q, g);
int n = q / g;
vpii pds;
prime_decomp(n, pnums, pds);
/*
printf("%d = ", n);
for (int i = 0; i < pds.size(); i++) {
if (i) putchar('*');
printf("%d^%d", pds[i].first, pds[i].second);
}
putchar('\n');
*/
int m = 1;
for (int i = 0; i < pds.size(); i++) m *= pds[i].first;
printf("%d\n", m);
return 0;
}
|
#include<iostream>
using namespace std;
int p,q,ans=1,j;
int so(int n){
for(int i=2;i*i<=n;i++)if(n%i==0)return 1;
return 0;
}
int main(){
cin>>p>>q;
if(so(q)){
for(int i=2;q>=i;i++){
for(j=0;q%i==0;j++){
q/=i;
if(p%i==0)p/=i,j--;
}
if(j==0)continue;
/*if(p%(i*j)==0)p/=(i*j);
else*/ ans*=i;
}
cout<<ans<<endl;
}else cout<<q<<endl;
return 0;
}
|
#include "bits/stdc++.h"
#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[a]; if (a != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9
const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int gcd(int a, int b) {
if (!b)return a;
return gcd(b, a%b);
}
int fact(int n) {
int res = 1, i = 2;
int p = n;
while (i*i<=p) {
if (n % i == 0) {
res *= i;
while (n%i == 0)n /= i;
if(n!=i*i)res *= fact(n);
break;
}
++i;
}
if (res == 1)return n;
else return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int p, q;
cin >> p >> q;
int c = gcd(p, q);
p /= c; q /= c;
int i = 2;
cout << fact(q) << endl;
return 0;
}
|
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
const int MOD = 1000000007;
int gcd(int x, int y) {
if (x < y) {
swap(x, y);
}
if (y == 0) {
return x;
}
return gcd(y, x%y);
}
int main() {
int p, q;
cin >> p >> q;
int m = sqrt(q) + 1;
vi prime(m+1, 1);
prime[0] = prime[1] = 0;
FOR(i, 2, m) {
if (prime[i]) {
for (int j = i*i; j <= m; j += i) {
prime[j] = 0;
}
}
}
int ans = 1;
q /= gcd(p, q);
FOR(i, 2, m) {
if ((prime[i] == 1) && (q%i == 0)) {
ans *= i;
while (q%i == 0) {
q /= i;
}
}
}
if (q > 1) {
ans *= q;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
each(x,vec) is >> x;
return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
rep(i,vec.size()) {
if (i) os << " ";
os << vec[i];
}
return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
rep(i,vec.size()) {
if (i) os << endl;
os << vec[i];
}
return os;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll solve(ll n) {
vector<ll> v;
for (ll i = 2; i*i <= n; ++i) {
if (n % i == 0) {
while (n % i == 0) n /= i;
v.pb(i);
}
}
v.pb(n);
ll res = 1;
each(x, v) res *= x;
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll p, q; cin >> p >> q;
cout << max(2LL, solve(q / gcd(p, q))) << endl;
}
|
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#define REP(i,n) for(long long i=0;i<n;++i)
#define REPR(i,n) for(long long i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v) {for(long long sitr=0;sitr<v.size();++sitr){cin>>v[sitr];}}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
using namespace std;
typedef pair<int,int> pii;
int main(){
llong p, q;
cin >> p >> q;
llong a = q, b = p;
llong r = a % b;
while(r != 0){
a = b;
b = r;
r = a % b;
}
p /= b; q /= b;
map<llong, llong> mp;
for(int i = 2; i * i <= q; ++i){
if(q % i == 0){
mp[i]++;
while(q % i == 0) q /= i;
}
}
if(q != 1){
mp[q]++;
}
llong ans = 1;
REPI(itr, mp){
ans *= itr->first;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;
int solve();
ll gcd(ll p, ll q) {
if (q == 0) { return p; }
return gcd(q, p%q);
}
int main(void) {
while (solve()) {}
return 0;
}
int solve() {
ll p, q, g;
cin >> p >> q;
g = gcd(max(p, q), min(p, q));
// 約分
p = p / g;
q = q / g;
// 答えはqの素因数をすべて掛け合わせたもの
// sqrt{q}までの素数を調べる
const ll M = (1e+9) + 1;
const ll N = sqrt(M) + 1;
ll ans;
//cout << M << " " << N << endl;
// エラトステネスの篩
vector<ll> primes;
primes.reserve(5000);
vector< bool > isPrime(M, true);
isPrime[0] = false;
isPrime[1] = false;
for (ll i = 2; i < N; i++) {
if (isPrime[i] == false) { continue; } // 合成数なら
primes.push_back(i);
for (ll j = i; i * j < N; j++) {
isPrime[i*j] = false;
}
}
ll qc = q;
set<ll> facts;
for (auto p : primes) {
if( qc % p == 0 ) {
while(qc % p == 0)
qc /= p;
if( qc % p > 0 ) {
facts.insert(p);
}
}
}
ans = qc;
for( auto itr = facts.begin(); itr != facts.end(); ++itr ){
ans *= (*itr);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;
template<typename T>
void printv(vector<T> const& s) {
REP(i, SZ(s)) {
cout << s[i] << " ";
}
cout << endl;
}
int gcd(int a, int b) {
if(b == 0) return a;
else return gcd(b, a%b);
}
int main () {
cin.tie(0);
cout << setprecision(10);
int p, q; cin >> p >> q;
int g = gcd(p, q);
p /= g; q /= g;
int ans = 1;
int tmpq = q;
for(int i=2;i*i<=tmpq;i++) {
if(q % i == 0) {
ans *= i;
}
while(q % i == 0) {
q /= i;
}
}
if(q != 1) ans *= q;
cout << ans << endl;
}
|
#include<iostream>
#include<vector>
using namespace std;
int gcd(int a, int b){
if(b==0) return a;
else return gcd(b, a%b);
}
int main(){
int p, q;
cin>> p>> q;
q=q/gcd(p, q);
vector<int> v;
for(int i=2; i*i<=q; i++){
if(q%i==0){
v.push_back(i);
while(q%i==0) q/=i;
}
}
int r=1;
for(int i=0; i<v.size(); i++) r*=v[i];
cout<< r*q<< endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
map<long long, int> prime_factorize(long long n) {
map<long long, int> res;
for (long long p = 2; p * p <= n; ++p) {
while (n % p == 0) { ++res[p]; n /= p; }
}
if (n != 1) res[n] = 1;
return res;
}
int main() {
long long p, q; cin >> p >> q;
long long g = gcd(p, q);
q /= g;
long long ans = 1;
for (auto &p: prime_factorize(q)) ans *= p.first;
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int a,int b){
if(a<b){swap(a,b);}
if(!b){return a;}
return gcd(b,a%b);
}
int main(){
int a=1,p,q,i,gc;
cin>>p>>q;
gc=gcd(p,q);
p/=gc;q/=gc;
for(i=2;i<sqrt(q)+1;i++){
if(!(q%i)){
a*=i;
while(!(q%i)){q/=i;}
}
}
a*=q;
cout<<a<<endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
int COU(int a,int b){
if(a==b||a==0)return 1;
while(true){
if(a<b){int t=a;a=b,b=t;}
if(b==0)return a;
a%=b;
}
}
int main(){
int p,q;
cin>>p>>q;
q/=COU(p,q);
for(int i=2;i*i<=q;i++){
while(!(q%(i*i)))q/=i;
}
cout<<q<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int GCD(int a, int b) {
return b ? GCD(b, a%b) : a;
}
int main()
{
int p, q, n, res = 1;
cin >> p >> q;
n = q / GCD(p, q);
for (int i = 2; i < sqrt(n) + 2; i++) {
if (n % i == 0) {
res *= i;
while (n % i == 0) {
n /= i;
}
}
}
res *= n;
if (res == 1) res = 2;
cout << res << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(a) (a).begin(),(a).end()
using ll = long long;
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
int main(){
int p, q;
cin>>p>>q;
int g = gcd(q, p);
q /= g;
int res = 1;
for (int i = 2; i*i <= q; ++i) {
if (q % i == 0) {
res *= i;
while (q % i == 0) {
q /= i;
}
}
}
if (q > 1) res *= q;
if (res > 1) {
cout << res << endl;
} else {
cout << 2 << endl;
}
return 0;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int p, q, mi, ans, temp;
cin >> p >> q;
mi = (p ,q);
while (p%q == 0) {
if (q == 1) break;
p /= q;
q /= q;
}
for (int i = 2; i * i<= mi; i++) {
while (p%i == 0 && q%i == 0) {
p /= i;
q /= i;
}
while (q%(mi/i) == 0 && p%(mi/i) == 0) {
p /= (mi/i);
q /= (mi/i);
}
}
ans = 1;
for (int i = 2; i * i <= q; i++) {
if (q%i != 0) continue;
int k = 1;
if (i != 2){
for (int j = 3; j * j<= i; j++) {
if (i%j == 0) {
k = 0;
break;
}
}
}
if (k == 1) ans *= i;
while (q%i == 0) {
q /= i;
}
}
ans *= q;
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int gcd(int p,int q){
return q==0 ? p : gcd(q,p%q);
}
int main(){
int p,q;
while(cin>>p>>q,p){
int g=gcd(p,q);
p/=g;
q/=g;
long long int res=1;
for(int i=2;i<1e5;i++){
if(q%i==0){
res*=i;
while(q%i==0) q/=i;
}
}
res*=q;
cout<<res<<endl;
return 0;
}
}
|
#include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
using namespace std;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int main(){
int p,q;
cin>>p>>q;
int r=gcd(q,p);
p/=r;q/=r;
int ans=1;
for(int i=2;i*i<=q;i++){
if(q%i!=0)continue;
ans*=i;
while(q%i==0)q/=i;
}
ans*=q;
cout<<ans<<endl;
}
|
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
using namespace std;
int yaku(int n,int m){
if(n%m==0){
return m;
}
return yaku(m,n%m);
}
int main(){
int n,m;
cin>>n>>m;
m/=yaku(n,m);
int ans=1;
for(int i=2;i*i<=m;i++){
if(m%i==0){
ans*=i;
while(m%i==0){
m/=i;
}
}
}
ans*=m;
cout<<ans<<endl;
return 0;
}
|
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
#define EPS 1e-15
using namespace std;
ll gcd(ll a,ll b){
ll r = a%b;
while(r>0){
a = b;b = r;
r =a%b;
}
return b;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll p,q;cin>>p>>q;
ll g = gcd(p,q);
ll ans = 1;
q/=g;
ll loop = q;
for(ll i = 2;i <= sqrt(loop);i++){
if(q % i == 0){
ans *= i;
while(q%i == 0){
q/=i;
}
}
}
cout<<ans*q<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
using ll = long long;
void bunkai(vector<ll> &a,ll x){
ll temp=x;
for(ll i=2;i*i<=x;i++){
if(temp%i==0){
a.push_back(i);
while(temp%i==0){
temp/=i;
}
}
}
a.push_back(temp);
return;
}
ll gcd(ll x,ll y){
if(x%y==0){
return y;
}else{
return gcd(y,x%y);
}
}
int main(){
ll p,q;
cin>>p>>q;
ll a=gcd(q,p);
p/=a;
q/=a;
//cout<<"p="<<p<<"q="<<q<<endl;
vector<ll> soinsu;
bunkai(soinsu,q);
ll ans=1;
for(ll i=0;i<soinsu.size();i++){
ans*=soinsu[i];
//cout<<"ans="<<ans<<endl;
}
cout<<ans<<endl;
return 0;
}
|
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;
int calc(int x,int y){
if(y == 0){
return x;
}else{
return calc(y,x%y);
}
}
int main(){
int p,q,common,limit;
scanf("%d %d",&p,&q);
common = calc(p,q);
p /= common;
q /= common;
limit = sqrt(q);
int ans = 1;
for(int i = 2; i <= limit; i++){
if(q%i == 0){
ans *= i;
while(q % i == 0)q /= i;
}
}
ans *= q;
printf("%d\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
ll gcd(ll a,ll b){
if(!b) return a;
return gcd(b,a%b);
}
map<int,int> Prime_Factor(int n){
map<int,int> m;
for(int i=2;i*i<=n;i++){
while(n%i==0){
m[i]++;
n/=i;
}
}
if(n!=1) m[n]++;
return m;
}
int main(){
ll n,m;
cin>>n>>m;
m/=gcd(n,m);
auto mp=Prime_Factor(m);
ll t=1;
for(auto p:mp) t*=p.first;
cout<<t<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int p, q;
int gcd(int x, int y){
if(x % y == 0) return y;
return gcd(y, x % y);
}
bool prime(int x){
bool f = true;
for(int i=2; i*i<=x; ++i)
if(x % i == 0)
f = false;
return f;
}
int main(){
// cin.tie(0);
// ios::sync_with_stdio(false);
cin >> p >> q;
int g = gcd(p, q);
q /= g;
// cout << p / g << " " << q << "\n";
vector<int> a;
if(prime(q)){
cout << q << "\n";
return 0;
}
for(int i=2; i<=q; ++i){
if(q % i == 0){
int t = 1;
q /= i;
while(q % i == 0){
++t;
q /= i;
}
a.push_back(i);
}
}
int ans = 1;
for(int i=0; i<a.size(); ++i)
ans *= a[i];
cout << ans << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
#define REP(i,j) for(int i = 0; i < j; i++)
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
int main(){
int p1,q;
cin >> p1 >> q;
int t = gcd(p1,q);
q /= t;
vector<int> pl;
pl.push_back(2);
pl.push_back(3);
for(int i = 5; i*i < 1e9; i+=2){
bool f = true;
for(int j = 3; j*j <= i; j+=2){
if(i % j == 0)f = false;
}
if(f) pl.push_back(i);
}
for(int i = 0; i < pl.size(); i++){
while(q%(pl[i]*pl[i]) == 0){
q /= pl[i];
}
}
cout << q << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int p, q;
int main() {
cin >> p >> q;
vector<int> A = {};
for (int i = 2; i * i <= q; i++) {
if(q % i == 0) {
A.push_back(i);
A.push_back(q / i);
}
}
for (auto i : A) {
while (p % i == 0 and q % i == 0) {
p /= i;
q /= i;
}
}
vector<int> pr ={};
for (int i = 2; i * i <= q; i++) {
if (q % i == 0) pr.push_back(i);
while (q % i == 0) q /= i;
}
for (auto pp : pr) {
q *= pp;
}
cout << q << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
typedef long long LL;
typedef long double LD;
LL gcd(LL x, LL y){
while(x%y){
LL tmp = x%y;
x=y;
y=tmp;
}
return y;
}
int main(){
LL p,q;
cin >> p >> q;
p%=q;
q/=gcd(p,q);
LL ans=1;
repp(i,2,sqrt(q)+1){
if(q%i==0){
ans*=i;
while(q%i==0) q/=i;
}
}
if(q>1) ans*=q;
cout << ans << endl;
return 0;
}
|
#include<iostream>
using namespace std;
int p, q; int gcd(int a, int b) { if (b == 0)return a; return gcd(b, a%b); }
int main() {
cin >> p >> q; int res = 1; int r = gcd(p, q); q /= r;
for (int i = 2; i*i <= q; i++) {
if (q%i == 0) { res *= i; while (q%i == 0)q /= i; }
}
if (q >= 2)res *= q;
cout << res << endl;
return 0;
}
|
#include<iostream>
using namespace std;
int p,q,ans=1,j;
int so(int n){
for(int i=2;i*i<=n;i++)if(n%i==0)return 1;
return 0;
}
int main(){
cin>>p>>q;
if(so(q)){
for(int i=2;q>=i;i++){
for(j=0;q%i==0;j++){
q/=i;
if(p%i==0)p/=i,j--;
}
if(j==0)continue;
ans*=i;
}
cout<<ans<<endl;
}else cout<<q<<endl;
return 0;
}
|
#include<bits/stdc++.h>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
struct Point
{
double x,y;
Point(){x=0;y=0;}
Point(double d_x,double d_y){x=d_x,y=d_y;}
double operator*(Point obj){return obj.x*x+obj.y*y;}
double operator%(Point obj){return obj.y*x-obj.x*y;}
Point operator*(double b){Point tmp;tmp.x=x*b;tmp.y=y*b;return tmp;}
Point operator/(double b){Point tmp;tmp.x=x/b;tmp.y=y/b;return tmp;}
Point operator+(Point obj){Point tmp;tmp.x=x+obj.x;tmp.y=y+obj.y;return tmp;}
Point operator-(){Point tmp;tmp.x=-x;tmp.y=-y;return tmp;}
Point operator-(Point obj){Point tmp;tmp.x=x-obj.x;tmp.y=y-obj.y;return tmp;}
Point operator-=(Point obj){x-=obj.x;y-=obj.y;return *this;}
Point operator+=(Point obj){x+=obj.x;y+=obj.y;return *this;}
Point operator/=(double b){x=x/b;y=y/b;return *this;}
Point operator*=(double b){x=x*b;y=y*b;return *this;}
double size(){return hypot(x,y);}
Point unit(){return Point(x/size(),y/size());}
Point normal(){return Point(y,-x);}
double atan(){return atan2(y,x);}
};
bool operator<(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool operator>(Point a,Point b){return b<a;}
bool operator<=(Point a,Point b){return !(b<a);}
bool operator>=(Point a,Point b){return !(a<b);}
bool operator==(Point a,Point b){return (a-b).size()<eps;}
bool operator!=(Point a,Point b){return !(a==b);}
bool equal(double a,double b){return abs(a-b)<eps;}
double cross(Point a,Point b){return a%b;}
double dot(Point a,Point b){return a*b;}
int ccw(Point a,Point b,Point c)
{
b=b-a;
c=c-a;
if(b%c>0) return +1;
else if(b%c<0)return -1;
else if(b*c<0) return +2;
else if(b.size()<c.size()) return -2;
else return 0;
}
int gcd(int a,int b)
{
if(b>a)
{
swap(a,b);
gcd(a,b);
}
if(b==0) return a;
else if(b==1) return b;
int p=a/b;
a-=p*b;
return gcd(b,a);
}
int main(int argc,char const* argv[])
{
int p,q,k;
cin >>p >> q;
k=gcd(p,q);
p/=k;
q/=k;
int q_=q;
vector<P> ans;
for(int i=2;i<=sqrt(q);i++)
{
if(q_%i==0)
{
P pa=make_pair(i,1);
q_ /=i;
while(q_%i ==0)
{
pa.second++;
q_ /=i;
}
ans.push_back(pa);
}
}
if(q_ > 1) ans.push_back(P(q_,1));
int a=1;
for(int i=0;i<(int) ans.size();i++)
{
a*=ans[i].first;
}
cout << a << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int GCD(int a, int b){
if(b == 0) return a;
else return GCD(b, a % b);
}
int main(){
int p, q;
cin >> p >> q;
q /= GCD(p, q);
map<int, int> mpa;
for(int i = 2; i * i <= q; i++){
while(q % i == 0){
mpa[i]++;
q /= i;
}
}
if(q != 1) mpa[q]++;
map<int, int>::iterator it;
int ans = 1;
for(it = mpa.begin(); it != mpa.end(); it++){
ans *= it->first;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <tuple>
#include <cstring>
#include <map>
#include <iomanip>
#include <ctime>
#include <complex>
#include <cassert>
#include <climits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>
ll gcd(ll a, ll b) {
if (a < b) swap(a, b);
ll r = a % b;
while (r != 0) {
a = b; b = r;
r = a % b;
}
return b;
}
vector<Pll> PF(ll n) {
vector<Pll> fs;
ll base = n;
for (ll i = 2; i * i <= base; i++) {
if (base % i == 0) {
int cnt = 0;
while (base % i == 0) {
base /= i;
cnt++;
}
fs.emplace_back(i, cnt);
}
}
if (base > 1) fs.emplace_back(base, 1);
return fs;
}
int main() {
int p, q;
cin >> p >> q;
int g = gcd(p, q);
vector<Pll> fs = PF(q / g);
int ans = 1;
for (auto &i : fs) ans *= i.first;
cout << ans << endl;
}
|
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int gcd(int a,int b){
return (b==0?a:gcd(b,a%b));
}
int main(){
int a,b;
cin>>a>>b;
b/=gcd(a,b);
int out=1;
for(int t=2;t*t<=b;t++)if(b%t==0){
out*=t;
while(b%t==0)b/=t;
}
cout<<out*b<<endl;
}
|
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll gcd(ll x, ll y)
{
ll r;
if (x < y) {
swap(x, y);
}
while (y > 0) {
r = x % y;
x = y;
y = r;
}
return x;
}
map<ll, ll> prime_factor(ll a) {
map<ll, ll> ret;
for (ll i = 2; i * i <= a; i++) {
while (a % i == 0 && a != 0) {
ret[i]++;
a /= i;
}
}
if (a != 1) {
ret[a]++;
}
return ret;
}
int main()
{
/*
cin.tie(nullptr);
ios::sync_with_stdio(false);
*/
ll p, q; scanf("%lld %lld", &p, &q);
ll g = gcd(p, q);
p /= g;
q /= g;
map<ll, ll> mp1 = prime_factor(p);
map<ll, ll> mp2 = prime_factor(q);
vector<ll> v;
//for (auto p : mp1) v.push_back(p.first);
for (auto p: mp2) v.push_back(p.first);
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
ll res = 1;
for (auto e : v) res *= e;
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
if (b == 0){
return a;
} else {
return gcd(b, a % b);
}
}
int main(){
int p, q;
cin >> p >> q;
int g = gcd(p, q);
p /= g;
q /= g;
int ans = 1;
for (int i = 2; i * i <= q; i++){
if (q % i == 0){
ans *= i;
while (q % i == 0){
q /= i;
}
}
}
if (q != 1){
ans *= q;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int p, q;
cin >> p >> q;
q /= gcd(p, q);
for (int i = 2; i * i <= q; i++) {
while (q % (i * i) == 0) q /= i;
}
cout << q << endl;
return 0;
}
|
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b,a%b);
}
int main()
{
int p, q;
cin >> p >> q;
q /= gcd(p,q);
long long int x = 1;
for(int i = 2; i * i <= q; i++){
if(q % i == 0){
x *= i;
while(q % i == 0) q /= i;
}
}
cout << x * q << endl;
}
|
#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(const T a, const T b){return a>=0 ? a/b : (a+1)/b-1;}
template<class T> T div_ceil(const T a, const T b){return a>=0 ? (a-1)/b+1 : a/b;}
constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 100010;
lint gcd(lint p, lint q){
if(p < q) swap(p, q);
while(q){
lint r = p % q;
p = q;
q = r;
}
return p;
}
int main(){
lint p, q;
scanf("%lld%lld", &p, &q);
lint g = gcd(p, q);
p /= g; q /= g;
lint ans = 1;
for(lint i=2; i*i<=q; ++i){
if(q%i == 0){
ans *= i;
while(q%i == 0) q /= i;
}
}
if(q > 1) ans *= q;
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include <math.h>
#include<algorithm>
// #include <boost/multiprecision/cpp_int.hpp>
#include<functional>
#define int long long
#define inf 1000000007
#define pa pair<int,int>
#define ll long long
#define pal pair<double,pa>
#define ppa pair<pa,int>
#define ppap pair<int,pa>
#define ssa pair<string,int>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
using namespace std;
//priority_queue<int, vector<int>, greater<int> > que;
class Point{
public:
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
Point operator + (Point p) {return Point(x+p.x,y+p.y);}
Point operator - (Point p) {return Point(x-p.x,y-p.y);}
Point operator * (double a) {return Point(x*a,y*a);}
Point operator / (double a) {return Point(x/a,y/a);}
double absv() {return sqrt(norm());}
double norm() {return x*x+y*y;}
bool operator < (const Point &p) const{
return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const Point &p) const{
return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
}
};
typedef Point Vector;
struct Segment{
Point p1,p2;
};
double hen(Vector a){
if(fabs(a.x)<EPS && a.y>0) return acos(0);
else if(fabs(a.x)<EPS && a.y<0) return 3*acos(0);
else if(fabs(a.y)<EPS && a.x<0) return 2*acos(0);
else if(fabs(a.y)<EPS && a.x>0) return 0.0;
else if(a.y>0) return acos(a.x/a.absv());
else return 2*acos(0)+acos(-a.x/a.absv());
}
string itos( int i ) {
ostringstream s ;
s << i ;
return s.str() ;
}
int gcd(int v,int b){
if(v>b) return gcd(b,v);
if(v==b) return b;
if(b%v==0) return v;
return gcd(v,b%v);
}
double dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
double distans(double x1,double y1,double x2,double y2){
double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return sqrt(rr);
}
/*}
int pr[100010];
//int inv[100010];
int beki(int wa,int rr){
if(rr==0) return 1ll;
if(rr==1) return wa;
if(rr%2==1) return (beki(wa,rr-1)*wa)%inf;
int zx=beki(wa,rr/2);
return (zx*zx)%inf;
}
void gya(){
pr[0]=1;
for(int i=1;i<100010;i++){
pr[i]=(pr[i-1]*i)%inf;
}
for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2);
}
*/
//----------------kokomade tenpure------------
bool b[100020]={0};
signed main(){
b[1]=1;
for(int i=2;i<100020;i++){
if(b[i]==1) continue;
int d=2*i;
while(d<100020){
b[d]=1;
d+=i;
}
}
int n,m;
cin>>n>>m;
m=m/gcd(n,m);
vector<int> ve;
for(int i=2;i<100020;i++){
if(b[i]==1) continue;
int ko=0;
while(1){
if(m%i==0){
m/=i;
ko++;
if(ko==1) ve.pb(i);
}
else break;
}
}
if(m>1) ve.pb(m);
int ans=1;
for(int i=0;i<ve.size();i++) ans*=ve[i];
cout<<ans<<endl;
/*
while(1){
cin>>n>>m;
if(n==0) return 0;
}*/
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int gcd(int a, int b){
if(b==0) return a;
return gcd(b,a%b);
}
bool isPrime[100000];
typedef pair<int,int> P;
int main(){
int p,q;
cin>>p>>q;
int d = gcd(p,q);
p/=d; q/=d;
// cout<<p<<' '<<q<<endl;
fill(isPrime,isPrime+100000,true);
isPrime[0]=isPrime[1]=false;
for(int i=0;i<100000;i++){
if(isPrime[i])for(int k=2;k*i<100000;k++) isPrime[i*k]=false;
}
int ans=1;
vector<P> V;
for(int i=0;i<100000;i++){
if(!isPrime[i]) continue;
if(q%i==0){
ans*=i;
while(q%i==0) q/=i;
}
}
ans*=q;
cout<<ans<<endl;
}
|
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
using namespace std;
const long long int MOD = 1000000007;
long long int N, M, K, H, W, L, R;
int gcd(int a, int b) {
if (a < b)swap(a, b);
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
M /= gcd(N, M);
for (int i = 2; i*i <= M; i++) {
long long int box = i;
long long int div = 1;
while (box < M) {
box *= i;
if (M%box == 0)div *= i;
}
M /= div;
}
cout << M<< endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int M = 1000000007;
int gcd(int a, int b) {
while (b > 0) { int t = a % b; a = b; b = t; }
return a;
}
int main() {
int p, q;
cin >> p >> q;
int g = gcd(p, q);
q /= g;
int ans = 1;
for (int i = 2; i * i <= q; ++i) {
if (q % i == 0) {
ans *= i;
while (q % i == 0)
q /= i;
}
}
ans *= q;
cout << max(ans, 2) << "\n";
return 0;
}
|
#include <iostream>
using namespace std;
int gcd(int p,int q){
int r=q%p;
while(r>0){
q=p;
p=r;
r=q%p;
}
return p;
}
int divide(int b,int i){
while(b%(i*i)==0){
b/=i;
}
return b;
}
int main(){
int p,q;
cin>>p>>q;
int b=q/gcd(p,q);
for(int i=2;i*i<=b;i++)
b=divide(b,i);
cout<<b<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
int main(){
int p,q;
method(gcd,int,int a,int b){
while(b){
a %= b;
swap(a,b);
}
return a;
};
method(func,int,int x){
int res = 1;
for(int i=2;i*i<=x;++i){
bool flag = false;
while(x%i==0){
x /= i;
flag = true;
}
if(flag)res *= i;
}
res *= x;
return res;
};
cin >> p >> q;
cout << func(q /gcd(p,q)) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
int ext_gcd(int a, int b, int &x, int &y){
if(b == 0){
x = 1; y = 0; return a;
}
int q = a / b;
int g = ext_gcd(b, a-q*b, x, y);
int z = x - q * y;
x = y; y = z;
return g;
}
map<int,int> pf(int n){
map<int,int> res;
FOR(i,2,sqrt(n)+1){
if(n == 1) break;
while(n%i == 0){
res[i]++;
n /= i;
}
}
if(n != 1) res[n]++;
return res;
}
int main(){
int p, q; cin >> p >> q;
int x, y;
q /= ext_gcd(p,q,x,y);
auto pr = pf(q);
int ans = 1;
for(auto pa : pr) ans *= pa.first;
cout << ans << endl;
}
|
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b, a%b);
}
int main(){
long p, q;
cin >> p >> q;
long tmp = gcd(q, p);
p /= tmp;
q /= tmp;
long ans = 1;
long root = sqrt(q) + 1;
vector<bool> hurui(root, true);
for(int i=2; i<=root; i++){
if(hurui[i] == true){
if(i <= root){
for(int j=2*i; j<=root; j+=i){
hurui[j] = false;
}
}
if(q%i == 0){
ans *= i;
while(q%i == 0)
q /= i;
if(q == 1)
break;
}
}
}
ans *= q;
cout << ans << endl;
return 0;
}
|
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;
int main(){
int p,q,pre_limit,limit;
scanf("%d %d",&p,&q);
pre_limit = sqrt(q);
for(int i = 2; i <= pre_limit; i++){
while(p%i == 0 && q %i == 0){
p /= i;
q /= i;
}
}
if(q % p == 0)q /= p;
limit = sqrt(q);
int ans = 1;
for(int i = 2; i <= limit; i++){
if(q%i == 0){
ans *= i;
while(q % i == 0)q /= i;
}
}
ans *= q;
printf("%d\n",ans);
return 0;
}
|
#include<iostream>
using namespace std;
unsigned gcd(unsigned a, unsigned b) {
if(a < b) return gcd(b, a);
unsigned r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
int main(){
long long p,q,t=1,gc;
cin >> p >> q;
gc = gcd(p,q);
p /= gc;
q /= gc;
for(int i=2;i*i<=q;i++){
if(q%i==0){
t*=i;
while(q%i==0) q/=i;
}
}
t*=q;
cout << t << endl;
}
|
#include<iostream>
using namespace std;
long gcd(long a,long b){return b?gcd(b,a%b):a;}
long a,b;
int main()
{
cin>>a>>b;
b/=gcd(a,b);
for(long i=2;i*i<=b;i++)
{
while(b%(i*i)<1)b/=i;
}
cout<<b<<endl;
}
|
#include <iostream>
#include <vector>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
using namespace std;
typedef long long ll;
int P, Q;
bool p[1000000];
vector<int> v;
int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b, a % b);
}
bool prime(int n) {
REP(i, 0, v.size()) if(n % v[i] == 0) return false;
return true;
}
ll solve() {
ll ans = 1;
REP(i, 0, v.size()) if(Q % v[i] == 0) {
ans *= v[i];
while(Q % v[i] == 0) Q /= v[i];
}
if(prime(Q)) ans *= Q;
return ans;
}
int main(void) {
REP(i, 0, 1000000) p[i] = true;
p[0] = p[1] = false;
REP(i, 0, 1000000) if(p[i]) for(int j = i * 2; j < 1000000; j += i) p[j] = false;
REP(i, 0, 1000000) if(p[i]) v.push_back(i);
cin >> P >> Q;
Q = Q / gcd(P, Q);
cout << solve() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int solve(int n) {
int res = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i) continue;
res *= i;
while (n % i == 0) n /= i;
}
if (n != 1) res *= n;
return res;
}
int main(void) {
int p, q;
cin >> p >> q;
cout << solve(q / gcd(p, q)) << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
//#include <numeric>
using namespace std;
#define FOR(i,a,b) for (size_t i = a; i < b; i++)
#define REP(i,b) FOR(i,0,b)
#define RFOR(i,a,b) for (size_t i = a-1; i >= b; i--)
#define RREP(i,a) RFOR(i,a,0)
#define INF 1e7
long gcd(long a, long b)
{
long r;
do {
r = b % a;
//printf("%ld = %ld * ? + %ld\n", b, a, r);
b = a;
a = r;
} while(r);
return b;
}
long search_root(long a)
{
int ans = 1, tmp = a;
for (int i = 2; i * i <= tmp; i++) {
//cout<<i<<tmp<<endl;
if (tmp % i == 0) {
ans *= i;
while (tmp % i == 0) tmp /= i;
}
}
return ans * tmp;
}
int main()
{
long p, q;
cin>>p>>q;
cout<<search_root(q / gcd(p, q))<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define var auto
using namespace std;
ll gcd(ll a, ll b){
while(true){
if (a == 0) return b;
b %= a;
if (b == 0) return a;
a %= b;
}
}
int main(){
ll p, q;
cin >> p >> q;
var g = gcd(p, q);
p /= g;
q /= g;
ll res = 1;
for (ll i = 2; i * i <= q; i++){
if (q % i != 0) continue;
res *= i;
while (q % i == 0){
q /= i;
}
}
res *= q;
cout << res << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int x, int y) {
if (x<=y) return gcd(y, x);
if (x%y == 0) return y;
else return gcd(y, x%y);
}
int main() {
int p, q; cin >> p >> q;
int r = q/gcd(p, q);
int s = r;
int res = 1;
for (int i = 2; i*i <= r; ++i) {
if (s%i == 0) {
res *= i;
while (s%i == 0) s /= i;
}
}
res *= s;
cout << res << endl;
return 0;
}
|
#include <cstdio>
#include <utility>
int gcd(int m, int n) {
while (n) std::swap(m %= n, n);
return m;
}
int main() {
int p, q;
scanf("%d %d", &p, &q);
int m = q / gcd(p, q);
int res = 1;
for (int i = 2; i*i <= m; ++i) {
if (m % i == 0) {
res *= i;
do {
m /= i;
} while (m % i == 0);
}
}
if (m > 1) res *= m;
printf("%d\n", res);
}
|
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
int gcd(int a, int b) {
if (b == 0)return a;
return gcd(b, a%b);
}
int main() {
int isp[100000] = {};
vector<int> v;
Rep(i, 2, 100000) {
if (!isp[i]) {
v.push_back(i);
for(int j=2*i;j<100000;j+=i){
isp[j] = 1;
}
}
}
int p, q; cin >> p >> q;
int d = gcd(q,p);
q /= d;
int out = 1; int now = 0;
while (q > 1) {
if (q%v[now] == 0) {
out*=v[now];
while (q%v[now] == 0)q /= v[now];
}
else if (v[now] > (int)sqrt(q + 0.5)) {
out *= q; q = 1;
}
else now++;
}
if (out == 1)out = 2;
cout << out << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll power(ll e, ll x) {
if (x == 0)
return 1;
if (x % 2 == 0)
return power(e * e, x / 2);
return e * power(e, x - 1);
}
int main() {
ll a, b;
cin >> a >> b;
a %= b;
map<ll, int> amp, bmp;
for (ll i = 2; i * i <= a; i++) {
if (a % i == 0) {
amp[i]++;
a /= i;
i--;
}
}
for (ll i = 2; i * i <= b; i++) {
if (b % i == 0) {
bmp[i]++;
b /= i;
i--;
}
}
if (a > 1)
amp[a]++;
if (b > 1)
bmp[b]++;
ll ans = 1;
for (auto &v : bmp) {
if (v.second - amp[v.first] >= 1)
ans *= v.first;
}
cout << ans << endl;
return 0;
}
|
#include<iostream>
#include<map>
#include<algorithm>
#include <iomanip>
#include <cmath>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <vector>
#include <bitset>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <list>
constexpr int INF = 1050000000;
constexpr int MOD = 1000000007;
constexpr long long LONGINF = 1005000000000000000;
using namespace std;
using ll = long long;
/* UnionFind
class UnionFind {
private:
std::vector<int> parent;
std::vector<int> height;
std::vector<int> m_size;
public:
UnionFind(int size_) : parent(size_), height(size_, 0), m_size(size_, 1) {
for (int i = 0; i < size_; ++i) parent[i] = i;
}
void init(int size_) {
parent.resize(size_);
height.resize(size_, 0);
m_size.resize(size_, 1);
for (int i = 0; i < size_; ++i) parent[i] = i;
}
int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
int t = size(x) + size(y);
m_size[x] = m_size[y] = t;
if (height[x] < height[y]) parent[x] = y;
else parent[y] = x;
if (height[x] == height[y]) ++height[x];
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
if (parent[x] == x) return m_size[x];
return size(parent[x] = find(parent[x]));
}
};
*/
int GCD(int a, int b) {
while (b) {
int tmp = a % b;
a = b;
b = tmp;
}
return a;
}
int s(int a) {
int n = a;
map<int, int> mp;
for (int i = 2; i*i <= a; i++) {
while (n%i == 0) {
n /= i;
mp[i];
}
}
mp[n];
int check = 1;
for (auto x : mp) {
check *= x.first;
}
return check;
}
int main() {
int p, q; cin >> p >> q; if (p == 0 && q == 0) { return 0; }
int G = GCD(p, q);
p /= G;
q /= G;
cout << s(q) << endl;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD = 1e9 + 7;
//a,b???GCD
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a%b);
}
//?´???°??????
vector<ll> divisor(ll n){
vector<ll> res;
//res.push_back(1);res.push_back(n);
ll tmp=n;
for(int i=2;i*i<=n;i++){
if(tmp%i!=0)continue;
res.push_back(i);
while(tmp%i==0){
tmp/=i;
}
}
if(tmp!=n) res.push_back(tmp);
sort(res.begin(),res.end());
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll p,q;cin>>p>>q;
ll g=gcd(p,q);
p/=g;q/=g;
vector<ll> res=divisor(q);
if(res.size()==0) cout<<q<<endl;
else{
ll ret=1;
for(int i=0;i<(int)res.size();i++) ret*=res[i];
cout<<ret<<endl;
}
}
|
#include<bits/stdc++.h>
using namespace std;
// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()
// code starts
#define MOD 1000000007
int main()
{
ll n,m,k;cin>>n>>m>>k;
vector<int> p(m);
ll i,j,l;
rep(i,m)cin>>p[i];
ll num=1;
ll needs=0;
while(num<=k)
{
num*=2;
needs++;
}
vector<vector<ll>> con(needs+2,vector<ll>(n,0));
rep(i,m)
{
con[0][p[i]%n]++;
}
con[0][0]++;
for(i=1;i<=needs+1;i++)
{
rep(j,n)
{
rep(l,n)
{
con[i][(j+l)%n]+=con[i-1][l]*con[i-1][j];
con[i][(j+l)%n]%=MOD;
}
}
}
ll left=k;
ll nowi=0;
vector<ll> ans(n,0);
ans[0]=1;
vector<ll> nans(n,0);
while(left>0)
{
if(left%2==1)
{
rep(j,n)
{
rep(l,n)
{
nans[(j+l)%n]+=con[nowi][l]*ans[j];
nans[(j+l)%n]%=MOD;
}
}
rep(j,n)
{
ans[j]=nans[j];
nans[j]=0;
}
}
left/=2;
nowi++;
}
rep(j,n)cout<<ans[j]<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[50][1000];
ll ans[1000];
ll M=1e9+7;
int main(){
ll n,m,K,p[10000];
cin>>n>>m>>K;
dp[0][0]++;
for(int i=0;i<m;i++)cin>>p[i],dp[0][p[i]%n]++;
ll i=0;
ans[0]=1;
while((1<<i)<=K){
if((1<<i)&K){
ll tmp[1000]={};
for(int k=0;k<n;k++)
for(int j=0;j<n;j++){
tmp[(k+j)%n]
=(tmp[(k+j)%n]+(ans[k]*dp[i][j]%M))%M;
}
for(int j=0;j<n;j++)ans[j]=tmp[j];
}
for(int k=0;k<n;k++)
for(int j=0;j<n;j++)dp[i+1][(k+j)%n]=(dp[i+1][(k+j)%n]+dp[i][k]*dp[i][j]%M)%M;
i++;
}
for(int i=0;i<n;i++)cout<<ans[i]<<endl;
return 0;
}
|
#include <bits/stdc++.h>
/* S.size() s.substr(l,r=s.size()-l) -> l文字目からr文字分
__gcd() lower_bound(a+l,a+r,x)-a でindex
// 配列 -> min/max({})
// vector-> *min_element(a+l,a+r) *忘れず lとrで[l,r)
//reverse(a+l,a+r) 配列aの[l,r)を逆順に strはreverse(all(s))
//sort(a+l,a+r,greater <int>()) 配列aの[l,r)昇順sort,大きい順はgreater<type>()
//vectorならall(v)
clock()/CLOCKS_PER_SEC で秒数
vector v.push_back(x),v.pop_back()
q.push(x),front()で先頭を返す,pop()で削除, size(),empty()
<deque>(push/pop)_(front/back)/fornt/back/insert/
priority_queue 宣言は priority_queue< Type, vector<Type>, greater<Type>> Q1;
// pq.push(x),top()で参照 pop()で削除 greaterで最小 lessで最大がtopに*/
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repl(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rrep(i,n) for(int (i)=(n)-1;(i)!=-1;(i)--)
#define vep(i,v) for(auto (i)=v.begin();(i)<(v.end());(i)++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
inline long long ceildiv(long long a, long long b) { return (a+b-1)/b; }
const long long inf = 10e17;
//int |x|<=2*10**9 long |x|<= 9*10**18
//printf(".(小数点以下の桁数)lf(double時、float ならf)")
#define ll long long
#define print(i) std::cout << (i) << '\n'
typedef vector<ll> vec;
typedef vector<vec> mat ;
const ll mod =1000000007;
vec mul(vec &A,vec &B){
vec ret(A.size(),0ll);
int n=A.size();
rep(i,n){
rep(j,n){
ret[(i+j)%n]=(ret[(i+j)%n]+A[i]*B[j]%mod)%mod;
}
}
return ret;
}
vec pow(vec A,ll n){
vec ret(A.size(),0ll);
ret[0]=1ll;
while(n>0ll){
if(n&1ll)ret=mul(ret,A);
A=mul(A,A);
n>>=1ll;
}
return ret;
}
int main(){
int n,m;
ll k;
cin>>n>>m>>k;
vec a(n,0ll);
a[0]++;
rep(b,m){
int p;
cin>>p;
p%=n;
a[p]++;
}
vec V=pow(a,k);
rep(i,n){
cout<<V[i]<<endl;
}
}
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;
VL mul(const VL &a, const VL &b) {
int n = a.size();
VL ret(2 * n - 1);
REP(i, 0, n) {
REP(j, 0, n) {
ret[i + j] += a[i] * b[j];
ret[i + j] %= mod;
}
}
for (int i = n - 2; i >= 0; --i) {
ret[i] += ret[i + n];
ret[i] %= mod;
}
return VL(ret.begin(), ret.begin() + n);
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
VL a(n, 0);
VL sum(n, 0);
a[0] = 1;
sum[0] = 1;
REP(et, 0, m) {
int p;
cin >> p;
a[p % n] += 1;
}
VL cur(a);
while (k > 0) {
if (k % 2) {
sum = mul(sum, cur);
}
cur = mul(cur, cur);
k /= 2;
}
REP(i, 0, n) {
cout << sum[i] << "\n";
}
}
|
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
typedef vector<ll> V;
typedef vector<V> MATRIX;
int N,M;
ll table[10000];
MATRIX calc_ans(MATRIX left, MATRIX right){
MATRIX RET(1,V(N));
for(int col = 0; col < N; col++){
RET[0][col] = 0;
for(int row = 0; row < N; row++){
RET[0][col] += left[0][row]*right[row][col];
RET[0][col] %= MOD;
}
}
return RET;
}
MATRIX calc(MATRIX left,MATRIX right){
MATRIX ret(N,V(N));
for(int i = 0; i < N; i++){
for(int k = 0; k < N; k++)ret[i][k] = 0;
}
for(int col = 0; col < N; col++){
ret[0][col] = 0;
for(int row = 0; row < N; row++){
ret[0][col] += left[0][row]*right[row][col];
ret[0][col] %= MOD;
}
}
for(int row = 0; row <= N-2; row++){
for(int col = 0; col <= N-1; col++){
ret[row+1][(col+1)%N] = ret[row][col];
}
}
return ret;
}
MATRIX pow(MATRIX MULT,int count){
MATRIX ret(N,V(N));
for(int row = 0; row < N; row++){
for(int col = 0; col < N; col++){
if(row == col)ret[row][col] = 1;
else{
ret[row][col] = 0;
}
}
}
while(count > 0){
if(count%2 == 1)ret = calc(ret,MULT);
MULT = calc(MULT,MULT);
count /= 2;
}
return ret;
}
int main(){
int K;
scanf("%d %d %d",&N,&M,&K);
for(int i = 0; i < M; i++){
scanf("%lld",&table[i]);
table[i] %= N;
}
ll work[N];
work[0] = 1;
for(int i = 1; i <= N-1; i++)work[i] = 0;
for(int i = 0; i < M; i++){
work[table[i]]++;
}
MATRIX first_state(1,V(N));
first_state[0][0] = 1;
for(int i = 1; i <= N-1; i++)first_state[0][i] = 0;
MATRIX transition(N,V(N));
for(int i = 0; i <= N-1; i++)transition[0][i] = work[i];
for(int row = 0; row <= N-2; row++){
for(int col = 0; col <= N-1; col++){
transition[row+1][(col+1)%N] = transition[row][col];
}
}
if(K > 1){
transition = pow(transition,K);
}
MATRIX ans = calc_ans(first_state,transition);
for(int i = 0; i < N; i++){
printf("%lld\n",ans[0][i]%MOD);
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
const long long p = 1000000007;
void printmat(const vector< int > &a)
{
const int n = a.size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cerr << a[(i+n-j)%n] << " ";
}
cerr << endl;
}
}
// caution: b = a * b
void mulBy(const vector< int > &a, vector< int > &b, int n)
{
vector< int > r(n);
for(int i = 0; i < n; i++) {
r[i] = b[i];
}
for(int j = 0; j < n; j++) {
long long c = 0;
for(int k = 0; k < n; k++) {
c += (a[k] * (long long)r[(j+n-k)%n]) % p; // b'[i][j] += a[i][k] * b[k][j];
}
b[j] = (int)(c % p);
}
}
// b = b * b
void pow2(vector< int > &b, int n)
{
vector< int > r(n);
for(int i = 0; i < n; i++) {
r[i] = b[i];
}
mulBy(r, b, n);
}
// mat = mat^x
void pow(vector< int > &mat, int n, int x)
{
vector< int > r(n);
// eye
for(int j = 0; j < n; j++) {
r[j] = 0 == j ? 1 : 0;
}
while(x > 0) {
if((x & 1) != 0) {
mulBy(mat, r, n);
}
pow2(mat, n);
x >>= 1;
}
for(int j = 0; j < n; j++) {
mat[j] = r[j];
}
}
int main(int argc, char *argv[])
{
for(;;) {
int n, m, k;
cin >> n >> m >> k;
vector<int> ps;
for(int i = 0; i < m; i++) {
int pi;
cin >> pi;
ps.push_back(pi);
}
// hist[i] .. possibilities of being on i
// hist' = hist * mat
// where mat[i][j] is transition possibilities from i to j
vector< int > mat(n);
for(int j = 0; j < n; j++) {
mat[j] = 0;
}
// kachi
for(int j = 0; j < m; j++) {
mat[(int)((ps[j]) % n)]++;
}
mat[0]++; // make
//printmat(mat);
pow(mat, n, k);
for(int i = 0; i < n; i++) {
cout << mat[i] << endl;
}
break;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
vector<ll> solve(vector<ll> a,vector<ll> b,ll m) {
vector<ll> c(m,0);
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
c[(i+j)%m]+=a[i]*b[j];
c[(i+j)%m]%=mod;
}
}
return c;
}
vector<ll> mod_pow(vector<ll> a,ll n,ll m){
if(n==0) {
vector<ll> v(m,0);
v[0]=1;
return v;
}
if(n==1) return a;
vector<ll> b=mod_pow(solve(a,a,m),n/2,m);
if(n&1) b=solve(b,a,m);
return b;
}
int main() {
ll n,m,k;
cin >> n >> m >> k;
vector<ll> a(n,0);
a[0]++;
for(int i=0; i<m; i++) {
int x;
cin >> x;
a[x%n]++;
}
a=mod_pow(a,k,n);
for(int i=0; i<n; i++) cout << a[i] << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
typedef long long ll;
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
int ret = 1; while (b > 0) {
if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
} return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b)
{
int n = b.size(); Vec ret(n, 0);
rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
return ret;
}
Mat mulMatMat(Mat a, Mat b)
{
int n = a.size(); Mat ret(n, Vec(n, 0));
rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
Mat mulMatMatForJunkai(Mat a, Mat b)
{
int n = a.size(); Mat ret(n, Vec(n, 0));
rep(j, 0, n) rep(k, 0, n) ret[0][j] = add(ret[0][j], mul(a[0][k], b[k][j]));
rep(i, 1, n) rep(j, 0, n) ret[i][j] = ret[0][(j - i + n) % n];
return ret;
}
Mat fastpow(Mat x, ll n)
{
Mat ret(x.size(), Vec(x.size(), 0));
rep(i, 0, x.size()) ret[i][i] = 1;
while (0 < n) { if ((n % 2) == 0) { x = mulMatMatForJunkai(x, x); n >>= 1; } else { ret = mulMatMatForJunkai(ret, x); --n; } }
return ret;
}
void printVec(Vec a) { cout << "[\t"; rep(i, 0, a.size()) cout << a[i] << "\t"; cout << "]" << endl; }
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ??? |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/??? ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/
int N, M, K, P[101010];
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N >> M >> K;
rep(i, 0, M) cin >> P[i];
Mat m(N, Vec(N, 0));
Vec v(N, 0);
v[0] = 1;
rep(from, 0, N) rep(i, 0, M) {
int to = (from + P[i]) % N;
m[to][from]++;
}
rep(i, 0, N) m[i][i]++;
//printMat(m);
m = fastpow(m, K);
v = mulMatVec(m, v);
rep(i, 0, N) printf("%d\n", v[i]);
}
|
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef vector<ll> row;
typedef vector<row> mat;
ll N, M, K, P[10000];
row mul(row &A, row &B) {
row C(N);
REP(i, 0, N)
REP(j, 0, N)
C[i] = (C[i] + A[j] * B[(i - j + N) % N]) % MOD;
return C;
}
row pow(row A, ll n) {
row B(N);
B[0] = 1;
while(n > 0) {
if(n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
int main(void) {
cin >> N >> M >> K;
REP(i, 0, M) cin >> P[i];
row A(N);
A[0]++;
REP(i, 0, M) A[(N - P[i] % N) % N]++;
A = pow(A, K);
REP(i, 0, N) cout << A[(N - i) % N] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
using ll = long long;
const ll mod = 1e9+7;
using vl = vector<ll>;
using mat = vector<ll>;
mat mul(const mat &a, const mat &b)
{
int n = a.size();
mat c(n);
rep(i,n)
{
c[i] = 0;
rep(j,n) c[i] = (a[j]*b[(i-j+n)%n] + c[i]) %mod;
}
return c;
}
vl mul_v(const mat &a, const vl &b)
{
int n = a.size();
vl ret(n);
rep(i,n)
{
rep(j,n)
{
ret[i] += (a[(j+i)%n]*b[j])%mod;
ret[i] %= mod;
}
}
return ret;
}
mat pow(const mat &a, int n)
{
int N = a.size();
mat r(N);
// ??????
r[0]=1;
mat p(a);
while(n)
{
if(n&1) r = mul(r,p);
p = mul(p,p);
n>>=1;
}
return r;
}
int main()
{
int n,m,k;
scanf(" %d %d %d", &n, &m, &k);
mat A(n);
A[0] = 1;
rep(i,m)
{
int p;
scanf(" %d", &p);
++A[p%n];
}
mat P = pow(A,k);
vl b(n);
b[0]=1;
vl ans = mul_v(P,b);
rep(i,n) printf("%lld\n", ans[i]);
return 0;
}
|
// need
#include <iostream>
#include <algorithm>
// data structure
#include <bitset>
//#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <array>
//#include <tuple>
//#include <unordered_map>
#include <unordered_set>
#include <complex>
//#include <deque>
#include<valarray>
// stream
//#include <istream>
//#include <sstream>
//#include <ostream>
//#include <fstream>
// etc
#include <cmath>
#include <cassert>
#include <functional>
#include <iomanip>
//#include <typeinfo>
#include <chrono>
#include <random>
#include <numeric>
// input
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; }
template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest&...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); }
#define VEC_ROW(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_VEC_ROW_Init(n, __VA_ARGS__); for(int i=0; i<n; ++i){MACRO_VEC_ROW_Scan(i, __VA_ARGS__);}
template<typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); }
template<typename First, typename...Rest>void MACRO_VEC_ROW_Init(int n, First& first, Rest&...rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); }
template<typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename...Rest>void MACRO_VEC_ROW_Scan(int p, First& first, Rest&...rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); }
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& r:c)for(auto& i:r)std::cin>>i;
// output
#define OUT(d) std::cout<<d;
#define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<d;
#define SOUT(n, c, d) std::cout<<std::setw(n)<<std::setfill(c)<<d;
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define SPBR(i, n) std::cout<<(i + 1 == n ? '\n' : ' ');
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
#define SHOW(d) {std::cerr << #d << "\t:" << d << "\n";}
#define SHOWVECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOWVECTOR2(v) {std::cerr << #v << "\t:\n";for(const auto& xxx : v){for(const auto& yyy : xxx){std::cerr << yyy << " ";}std::cerr << "\n";}}
#define SHOWQUEUE(a) {auto tmp(a);std::cerr << #a << "\t:";while(!tmp.empty()){std::cerr << tmp.front() << " ";tmp.pop();}std::cerr << "\n";}
// utility
#define ALL(a) (a).begin(),(a).end()
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define FORLL(i, a, b) for(ll i=ll(a);i<ll(b);++i)
#define RFORLL(i, a, b) for(ll i=ll(b)-1;i>=ll(a);--i)
#define REPLL(i, n) for(ll i=0;i<ll(n);++i)
#define RREPLL(i, n) for(ll i=ll(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
template<typename T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; }
template<typename T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; }
#define EXCEPTION(msg) throw std::string("Exception : " msg " [ in ") + __func__ + " : " + std::to_string(__LINE__) + " lines ]"
#define TRY(cond, msg) try {if (cond) EXCEPTION(msg);}catch (std::string s) {std::cerr << s << std::endl;}
void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)).count(); std::cerr << "[Time:" << res << "ns (" << res / (1.0e9) << "s)]\n"; }
// test
template<class T> std::vector<std::vector<T>> VV(int n) {
return std::vector<std::vector<T>>(n);
}
template<class T> std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template<typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")"; return os;
}
// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = 1 << 30; // 1.07x10^ 9
constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9
constexpr ll INFLL = 1LL << 60; // 1.15x10^18
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18
constexpr double EPS = 1e-7;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
ll powMod(ll n, ll p, ll mod) {
ll res = 1;
while (p) {
if (p & 1) res *= n; res %= mod;
n *= n; n %= mod;
p >>= 1;
}
return res;
}
std::vector<int> mul(const std::vector<int>& l, const std::vector<int>& r) {
int n = l.size();
std::vector<int> res(n, 0);
REP(i, n) {
REP(j, n) {
(res[i] += l[j] * r[(i - j + n) % n]) %= MOD;
}
}
return res;
}
signed main() {
INIT;
VAR(int, n, m, k);
VEC(int, p, m);
p.emplace_back(0);
REP(i, m) p[i] %= n;
std::vector<int> C(n, 0);
REP(i, m + 1) ++C[p[i]];
std::vector<int> B(n, 0);
B[0] = 1;
while (k) {
if (k & 1) B = mul(B, C);
C = mul(C, C);
k >>= 1;
}
REP(i, n) {
OUT(B[i])BR;
}
return 0;
}
|
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat &A,mat &B){
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();i++){
for(int k=0;k<B.size();k++){
for(int j=0;j<B[0].size();j++){
C[i][j]=(C[i][j]+(A[i][k]*B[k][j]%MOD))%MOD;
}
}
}
return C;
}
mat pow(mat A,ll n){
mat B(A.size(),vec(A.size()));
for(int i=0;i<A.size();i++){
B[i][i]=1;
}
while(n>0){
if(n&1)B=mul(B,A);
A=mul(A,A);
n>>=1;
}
return B;
}
int n,m,K;
ll p[10001];
ll nec[801];
ll res[30][801];
ll ans[2][801];
int main(void){
scanf("%d%d%d",&n,&m,&K);
mat B(n,vec(n));
for(int i=0;i<m;i++){
scanf("%d",&p[i]);
}
nec[0]++;
for(int i=0;i<m;i++){
int mo=p[i]%n;
nec[mo]++;
}
for(int i=0;i<30;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(i>0)res[i][(j+k)%n]+=(res[i-1][j]*res[i-1][k])%MOD;
if(i==0)res[i][j]=nec[j];
if(res[i][(j+k)%n]>=MOD)res[i][(j+k)%n]%=MOD;
}
}
}
int prev=0,now=1;
ans[0][0]=1;
for(int i=0;i<30;i++){
if(K>>i & 1){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
ans[now][(j+k)%n]+=(ans[prev][j]*res[i][k])%MOD;
if(ans[now][(j+k)%n]>=MOD)ans[now][(j+k)%n]%=MOD;
}
}
swap(now,prev);
memset(ans[now],0,sizeof(ans[now]));
}
}
for(int i=0;i<n;i++){
printf("%lld\n",ans[prev][i]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount
const long long M=1e9+7;
typedef vector<long long> vec;
typedef vector<vec> mat;
struct Matrix{
int N;
mat a;
Matrix(int n){
N=n;
a=mat(N,vec(N));
for(int i=0;i<N;i++)a[i][i]=1;
}
Matrix pow(long long b) const {
Matrix res(N);
Matrix d(N);
for(int i=0;i<N;i++)for(int j=0;j<N;j++)d[i][j]=a[i][j];
for(;b>0;b>>=1,d=d*d)if(b&1)res=d*res;
return res;
}
Matrix operator+(const Matrix& r) const {
Matrix s(N);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
s[i][j]=(a[i][j]+r.a[i][j])%M;
}
}
return s;
}
Matrix operator*(const Matrix& r) const {
Matrix s(N);
for(int i=0;i<N;i++)s[i][i]=0;
const long long modl=8*M*M;
//for(int i=0;i<N;i++){
for(int k=0;k<N;k++){
for(int j=0;j<N;j++){
s[0][j]+=a[0][k]*r.a[k][j];
if(s[0][j]>=modl)s[0][j]-=modl;
}
}
for(int j=0;j<N;j++)s[0][j]%=M;
//}
for(int i=0;i<N-1;i++){
for(int j=0;j<N;j++){
s[i+1][(j+1)%N]=s[i][j];
}
}
return s;
}
vector<long long>& operator[](int i){
return a[i];
}
};
ll n,m,k;
ll cnt[888];
int main(){
cin>>n>>m>>k;
rep(i,m){
ll p;
cin>>p;
cnt[p%n]++;
}
Matrix mat(n);
rep(i,n)rep(j,n){
mat[i][(i+j)%n]+=cnt[j];
}
Matrix res=mat.pow(k);
rep(i,n){
cout<<res[0][i]<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
using namespace std;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
struct mat
{
vvl v;
mat(int n){
v.resize(n,vl(n,0));
}
};
ll add(ll x,ll y)
{
return (x + y)%MOD;
}
ll sub(ll x,ll y)
{
return (x+MOD-y)%MOD;
}
ll mul(ll x,ll y)
{
return x*y%MOD;
}
mat mul(const mat& m1,const mat& m2)
{
int MAX_N = len(m1.v);
mat res(MAX_N);
rep(i,MAX_N){
rep(j,MAX_N){
res.v[0][i] = add(res.v[0][i],mul(m1.v[0][j],m2.v[j][i]));
}
}
srep(i,1,MAX_N){
rep(j,MAX_N){
res.v[i][j] = res.v[0][(j+MAX_N-i)%MAX_N];
}
}
return res;
}
mat mod_pow(const mat& m1,ll b)
{
int n = len(m1.v);
mat res(n);
rep(i,n){
res.v[i][i] = 1;
}
mat nw(n);
nw = m1;
while(b){
if(b & 1){
res = mul(res,nw);
}
nw = mul(nw,nw);
b >>= 1;
}
return res;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n,m,K;
cin >> n >> m >> K;
mat a(n);
rep(i,n){
a.v[i][i]++;
}
rep(i,m){
int b;
cin >> b;
rep(j,n){
a.v[j][(j+b)%n]++;
}
}
mat res(n);
res = mod_pow(a,K);
rep(i,n){
cout << res.v[0][i] << "\n";
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
typedef vector<int> vec;
vec calc(vec a,vec b){
int n=a.size();
vec res(n,0);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
res[(i+j)%n]+=a[i]*b[j];
res[(i+j)%n]%=MOD;
}
}
return res;
}
signed main(){
int n,m,k;
cin>>n>>m>>k;
int p[m];
for(int i=0;i<m;i++) cin>>p[i];
vec t(n,0);
t[0]=1;
for(int i=0;i<m;i++) t[p[i]%n]++;
vec ans(n,0);ans[0]=1;
while(k){
if(k&1) ans=calc(ans,t);
t=calc(t,t);
k>>=1;
}
for(int i=0;i<n;i++) cout<<ans[i]<<endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>
#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif
#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::cout.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
/*-*/
constexpr lnt MOD = 1e9+7;
struct mint
{
lnt v;
mint():v(0){}
mint(lnt v):v((v+MOD)%MOD){}
mint operator-()const{ return mint(0) - *this; }
mint& operator+=(const mint& a){ if((v+=a.v)>=MOD) v-=MOD; return *this; }
mint& operator-=(const mint& a){ if((v+=MOD-a.v)>=MOD) v-=MOD; return *this; }
mint& operator*=(const mint& a){ (v*=a.v)%=MOD; return *this; }
mint& operator/=(const mint& a){ (*this) *= a.inv(); return *this; }
mint operator+(const mint& a)const{ return mint(*this) += a; }
mint operator-(const mint& a)const{ return mint(*this) -= a; }
mint operator*(const mint& a)const{ return mint(*this) *= a; }
mint operator/(const mint& a)const{ return mint(*this) /= a; }
bool operator<(const mint& a)const{ return v < a.v; }
bool operator==(const mint& a)const{ return v == a.v; }
mint pow(lnt k)const{ mint r(1),t(v); while(k){ if(k&1) r*=t; t*=t; k>>=1; } return r; }
mint inv()const{ return pow(MOD-2); }
static mint comb(lnt n, lnt k) { if(n-k<k) k=n-k; mint num(1), dom(1); for(int i=0;i<k;i++) { num*=n-i; dom*=i+1; } return num/dom; }
static std::vector<mint> construct_comb(int n) {
std::vector<mint> c(n+1); mint a = 1; c[0] = a; for(int i=1;i<=n;i++) { a = a*mint(n+1-i)/i; c[i] = a; } return c;
}
static std::vector<mint> construct_fact(int n) { std::vector<mint> f(n+1,1); for(int i=2;i<=n;i++) f[i]=f[i-1]*i; return f; }
};
std::istream& operator>>(std::istream&i,mint&a){ lnt t; i>>t; a=mint(t); return i; }
std::ostream& operator<<(std::ostream&o,const mint&a){ o<<a.v; return o; }
std::vector<mint> mul(std::vector<mint>& a, std::vector<mint>&b) {
int n=a.size();
std::vector<mint> c(n,0);
rep(i,n) rep(j,n) c[(i+j)%n]+=a[i]*b[j];
return c;
}
std::vector<mint> pow(std::vector<mint>&a, lnt k) { int n=a.size(); std::vector<mint> r(n,0),t=a;r[0]=1; while(k){ if(k&1) r=mul(r,t); t=mul(t,t); k>>=1; } return r; }
int main() {
int n,m,k;
std::cin >> n >> m >> k;
std::vector<mint> a(n,0);
a[0]=1;
rep(i,m) {
int p;
std::cin >> p;
a[p%n]+=mint(1);
}
debug(a);
auto c=pow(a,k);
debug(c);
rep(i,n) print(c[i]);
}
|
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef vector<ll> row;
typedef vector<row> mat;
ll N, M, K, P[10000];
row mul(row &A, row &B) {
row C(N);
REP(i, 0, N)
REP(j, 0, N)
C[i] = (C[i] + A[j] * B[(i - j + N) % N]) % MOD;
return C;
}
row pow(row A, ll n) {
row B(N);
B[0] = 1;
while(n > 0) {
if(n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
int main(void) {
cin >> N >> M >> K;
REP(i, 0, M) cin >> P[i];
row A(N);
A[0]++;
REP(i, 0, M) A[P[i] % N]++;
A = pow(A, K);
REP(i, 0, N) cout << A[i] << endl;
}
|
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
using usize = std::size_t;
using u64 = std::uint64_t;
static constexpr u64 mod = 1000000007;
int main() {
usize n, m;
std::cin >> n >> m;
u64 k;
std::cin >> k;
const auto mul = [&](std::vector<u64> &a, const std::vector<u64> &b) {
std::vector<u64> c(n * 2, 0);
for (usize i = 0; i != n; ++i) {
for (usize j = 0; j != n; ++j) {
c[i + j] = (c[i + j] + a[i] * b[j]) % mod;
}
}
for (usize i = 0; i != n; ++i) {
a[i] = (c[i] + c[n + i]) % mod;
}
};
std::vector<u64> v(n, 0);
v[0] += 1;
for (usize i = 0; i != m; ++i) {
usize p;
std::cin >> p;
p %= n;
v[p] += 1;
}
std::vector<u64> ans(n, 0);
ans[0] = 1;
while (k != 0) {
if (k % 2 != 0) {
mul(ans, v);
}
mul(v, v);
k /= 2;
}
for (const auto e : ans) {
std::cout << e << "\n";
}
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
constexpr ll INF = 1ll<<30;
constexpr ll MOD = 1000000007;
//---------------------------------//
ll dp[32][800];
ll dp2[32][800];
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<int> P(M);
REP(i, M) {
scanf("%d", &P[i]);
P[i] %= N;
}
++dp[0][0];
REP(i, M) ++dp[0][P[i] % N];
REP(t, 31) REP(i, N) REP(j, N) {
(dp[t + 1][(i + j) % N] += dp[t][i] * dp[t][j] % MOD) %= MOD;
}
dp2[31][0] = 1;
for (int t = 30; t >= 0; --t) {
if (K >> t & 1) {
REP(i, N) {
REP(j, N) {
(dp2[t][(i + j) % N] += dp2[t + 1][i] * dp[t][j] % MOD) %= MOD;
}
}
}
else {
REP(i, N) dp2[t][i] = dp2[t + 1][i];
}
}
REP(i, N) printf("%lld\n", dp2[0][i]);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf = 1e9;
ll mod = 1e9+7;
int main(){
ll n,m,kk;
cin>>n>>m>>kk;
vector<vector<ll> > dp(32, vector<ll>(n,0) );
dp[0][0]++;
for(ll i=0;i<m;i++){
ll tmp;
cin>>tmp;
//tmp--;
dp[0][tmp%n]++;
}
for(ll i=1;i<32;i++){
for(ll j=0;j<n;j++){
for(ll k=0;k<n;k++){
dp[i][(j+k)%n] += (dp[i-1][j]*dp[i-1][k])%mod;
dp[i][(j+k)%n] %= mod;
}
}
}
vector<ll> ans(n,0);
ans[0] = 1;
for(ll i=0;i<32;i++){
if( (kk>>i)&1 ){
vector<ll> tmp(n,0);
for(ll j=0;j<n;j++){
for(ll k=0;k<n;k++){
tmp[(j+k)%n] += (ans[j]*dp[i][k])%mod;
tmp[(j+k)%n] %= mod;
}
}
ans = tmp;
}
}
for(auto i:ans)cout<<i<<endl;
return 0;
}
|
/* -*- coding: utf-8 -*-
*
* 2844.cc: Almost Infinite Glico
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 800;
const int MOD = 1000000007;
/* typedef */
typedef long long ll;
typedef int vec[MAX_N];
/* global variables */
vec av, bv, cv;
vec sv, tv;
/* subroutines */
inline void addmod(int &a, int b) { a = (a + b) % MOD; }
inline void initvec(const int n, vec a) {
memset(a, 0, sizeof(vec));
}
inline void copyvec(const int n, const vec a, vec b) {
memcpy(b, a, sizeof(vec));
}
inline void mulvec(const int n, const vec a, const vec b, vec c) {
initvec(n, c);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
addmod(c[(i + j) % n], (ll)a[i] * b[j] % MOD);
}
/* main */
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
initvec(n, av);
av[0] = 1;
for (int i = 0; i < m; i++) {
int pi;
scanf("%d", &pi);
av[pi % n]++;
}
initvec(n, bv);
bv[0] = 1;
while (k > 0) {
if (k & 1) {
mulvec(n, bv, av, sv);
copyvec(n, sv, bv);
}
mulvec(n, av, av, sv);
copyvec(n, sv, av);
k >>= 1;
}
for (int i = 0; i < n; i++) printf("%d\n", bv[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
const ll LINF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
#define name3(_1,_2,_3,name,...) name
#define rep1(n) for(ll i=0;i<(n);++i)
#define rep2(i,n) for(ll i=0;i<(n);++i)
#define rep3(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(...) name3(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep(i,a,b) for(ll i=(b)-1;i>=(a);i--)
#define each(i,a) for(auto &i : a)
#define sum(a) accumulate(all(a), 0LL)
#define all(i) begin(i), end(i)
template<class T, class U> inline bool chmin(T &a, const U &b){ if(a > b) { a = b; return 1; } else return 0; }
template<class T, class U> inline bool chmax(T &a, const U &b){ if(a < b) { a = b; return 1; } else return 0; }
ll n;
struct T{
ll cnt[800] = {};
T operator*(const T& a) const {
T ans;
rep(n)rep(j,n)ans.cnt[(i+j)%n]+=cnt[i]*a.cnt[j]%MOD;
each(i,ans.cnt)i%=MOD;
return ans;
}
T pow(ll x) const {
T ans, a = *this;
ans.cnt[0] = 1;
while(x){
if(x & 1) ans = ans * a;
a = a * a;
x >>= 1;
}
return ans;
}
};
int main(){
ll m, k;
cin >> n >> m >> k;
T ans;
ans.cnt[0] = 1;
rep(m){
ll p;
cin >> p;
ans.cnt[p % n]++;
}
ans = ans.pow(k);
rep(n) cout << ans.cnt[i] << endl;
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; i++ )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
typedef vector<ll> mat;
int sz;
mat mul(mat A,mat B){
mat C(sz);
rep(i,sz)rep(j,sz)C[(i+j)%sz]+=A[i]*B[j]%mod;
rep(i,sz)C[i]%=mod;
return C;
}
mat pow(mat A,ll k){
mat B(sz);
B[0]=1;
while(k>0){
if(k&1)B=mul(A,B);
A=mul(A,A);
k/=2;
}
return B;
}
int main(){
cin>>sz;
int m,k;
cin>>m>>k;
mat A(sz);
int p[m];
rep(i,m){
cin>>p[i];
A[p[i]%sz]++;
}
A[0]++;
A=pow(A,k);
rep(i,sz)cout<<A[i]<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
typedef vector<int> vec;
vec calc(vec a,vec b){
int n=a.size();
vec res(n,0);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
res[(i+j)%n]+=a[i]*b[j];
res[(i+j)%n]%=MOD;
}
}
return res;
}
signed main(){
int n,m,k;
cin>>n>>m>>k;
int p[m];
for(int i=0;i<m;i++) cin>>p[i];
vec t(n,0);
t[0]=1;
for(int i=0;i<m;i++) t[p[i]%n]++;
vec ans=t;k--;
while(k){
if(k&1) ans=calc(ans,t);
t=calc(t,t);
k>>=1;
}
for(int i=0;i<n;i++) cout<<ans[i]<<endl;
return 0;
}
|
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int cys=998244353;
int n;
ll a[300005],fac[300005],inv[300005],hv[300005];
bool vis[300005];
vector<int> v;
ll qpow(ll x,ll p){
ll ret=1;
for(;p;p>>=1,x=x*x%cys) if(p&1) ret=ret*x%cys;
return ret;
}
int main(){
n=readint();
fac[0]=inv[0]=1;
for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%cys;
inv[n]=qpow(fac[n],cys-2);
for(int i=n-1;i>=1;i--) inv[i]=inv[i+1]*(i+1)%cys;
for(int i=1;i<=n;i++) a[i]=readint();
for(int i=1;i<=n;i++){
if(vis[i]) continue;
int cnt=0;
for(int u=i;!vis[u];u=a[u]) cnt++,vis[u]=1;
v.pb(cnt);
}
sort(v.begin(),v.end());
if(v[0]>2) return printf("0\n"),0;
if(v[0]==1){
ll ans=1;
for(int i=0;i<v.size();){
int num=v[i],cr=0;
while(v[i]==num) cr++,i++;
if(num>1){
ll tmp=0;
for(int j=1;j<=cr;j++)
tmp=(tmp+fac[cr-1]*inv[j-1]%cys*inv[cr-j]%cys*qpow(cr,cr-j)%cys*qpow(hv[num],j)%cys*qpow(num,cr-j))%cys;
ans=ans*tmp%cys;
}
else ans=cr==1?1:qpow(cr,cr-2);
for(int j=num<<1;j<=n;j+=num) hv[j]+=cr*num;
}
printf("%lld\n",ans);
}
else{
ll ans=1;
for(int i=0;i<v.size();){
int num=v[i],cr=0;
while(v[i]==num) cr++,i++;
if(num>2){
ll tmp=0;
for(int j=1;j<=cr;j++)
tmp=(tmp+fac[cr-1]*inv[j-1]%cys*inv[cr-j]%cys*qpow(cr,cr-j)%cys*qpow(hv[num],j)%cys*qpow(num,cr-j))%cys;
ans=ans*tmp%cys;
}
else ans=qpow(cr,cr-1)*qpow(num,cr-1)%cys;
for(int j=num<<1;j<=n;j+=num) hv[j]+=cr*num;
}
printf("%lld\n",ans);
}
return 0;
}
|
#include<bits/stdc++.h>
typedef long long i64;
const int N=3e5+10,mod=998244353;
int n,ans,p[N],cnt[N],vis[N];
int length(int u){vis[u]=1;return vis[p[u]]?1:length(p[u])+1;}
inline int fpow(int a,int b){int s=1;for(;b;b>>=1,a=(i64)a*a%mod)if(b&1)s=(i64)s*a%mod;return s;}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&p[i]);
for(int i=1;i<=n;i++)if(!vis[i])cnt[length(i)]++;
if(!cnt[1]&&!cnt[2]){puts("0"); return 0;}
if(!cnt[1])for(int i=1;i<=n;i+=2)if(cnt[i]){puts("0"); return 0;}
for(int i=1,j,w;i<=n;i++)if(cnt[i]){
if(i==1)ans=cnt[i]==1?1:fpow(cnt[i],cnt[i]-2);
else if(i==2&&!cnt[1])ans=(i64)fpow(cnt[i],cnt[i]-1)*fpow(i,cnt[i]-1)%mod;
else{
for(w=0,j=1;j*j<=i;j++)if(!(i%j))w=(w+(i64)j*cnt[j])%mod;
for(j=2;j*j<i;j++)if(!(i%j))w=(w+(i64)(i/j)*cnt[i/j])%mod;
ans=(i64)ans*w%mod*fpow((w+(i64)i*cnt[i])%mod,cnt[i]-1)%mod;
}
}
printf("%d\n",ans);
}
|
#include <bits/stdc++.h>
#define ll long long
#define maxn 300005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
ll ksm(ll a, ll b) {
if (!b) return 1;
ll ns = ksm(a, b >> 1);
ns = ns * ns % mod;
if (b & 1) ns = ns * a % mod;
return ns;
}
ll tw[maxn];
int p[maxn];
vi cu;
int vis[maxn];
int cnt[maxn];
ll jc[maxn];
ll bjc[maxn];
vi dv[maxn];
ll c(int a, int b) {
if (a < b || b < 0) return 0;
return jc[a] * bjc[a - b] % mod * bjc[b] % mod;
}
int main() {
jc[0] = bjc[0] = 1;
for (int i = 1; i < maxn; i++)
jc[i] = jc[i - 1] * i % mod,
bjc[i] = ksm(jc[i], mod - 2);
for (int i = 1; i < maxn; i++)
for (int j = 1; 1ll * j * i < maxn; j++)
dv[j * i].pb(i);
tw[1] = 1;
for (int i = 2; i < maxn; i++)
tw[i] = ksm(i, i - 2);
int n;
cin >> n;
ll mt = 1;
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
int j = i;
int cnt = 0;
while (1) {
vis[j] = 1;
cnt++;
j = p[j];
if (j == i) break;
}
cu.pb(cnt);
}
sort(cu.begin(), cu.end());
if (cu[0] >= 3) {
cout << 0 << endl;
return 0;
}
else {
for (auto v : cu)
cnt[v]++;
ll ans = 1;
int bg = cu[0];
// for (auto v : cu) cout << v << ' ';
// cout << endl;
for (int i = 1; i < maxn; i++) {
if (!cnt[i]) continue;
else {
if (i == bg) {
if (i == 1) ans = ans * tw[cnt[1]] % mod;//, cout << ans << endl;
else {
ans = tw[cnt[2]] * cnt[2] % mod * ksm(2, cnt[2] - 1) % mod;
}
}
else {
ll tt = ksm(i, cnt[i]);
ll mt = 0;
for (auto q : dv[i]) {
if (q == i) continue;
mt = (mt + cnt[q] * ksm(i / q, mod - 2)) % mod;
}
int tid = cnt[i] + 1;
ll na = 0;
for (int s = 1; s <= cnt[i]; s++) {
ll pw = c(tid - 2, s - 1) * ksm(mt, s) % mod * ksm(tid - 1, tid - 2 - (s - 1)) % mod;
// cout << s << ' ' << pw << endl;
na = (na + pw) % mod;
}
na = na * tt % mod;
ans = ans * na % mod;
// cout << i << ' ' << na << endl;
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <limits>
#include <random>
#include <utility>
#include <vector>
namespace procon {
class UnionFind {
private:
struct nodeinfo {
int par;
int rank;
nodeinfo(int par) : par(par), rank(0) {}
};
std::vector<nodeinfo> node;
public:
UnionFind(int n) : node() {
node.reserve(n);
for (int i = 0; i < n; ++i) {
node.push_back(nodeinfo(i));
}
}
int root(int x) {
if (node[x].par == x) {
return x;
}
return node[x].par = root(node[x].par);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (node[x].rank < node[y].rank) {
node[x].par = y;
} else {
node[y].par = x;
if (node[x].rank == node[y].rank) {
++node[x].rank;
}
}
}
bool is_same_set(int x, int y) { return root(x) == root(y); }
};
}
int main(void) {
int size, n_query;
std::cin.read((char *)&size, sizeof(int));
std::cin.read((char *)&n_query, sizeof(int));
std::vector<uint64_t> query(n_query);
std::cin.read((char *)query.data(), sizeof(uint64_t) * n_query);
const int mask = (1 << 30) - 1;
uint64_t res = 123456789;
procon::UnionFind uf(size);
for (uint64_t q : query) {
const int com = q >> 60;
const int x = (q >> 30) & mask;
const int y = (q >> 0) & mask;
assert(0 <= x && x < size);
assert(0 <= y && y < size);
if (com) {
res = res * 17 + (uf.is_same_set(x, y) ? 1 : 0);
} else {
uf.unite(x, y);
}
}
std::cout << res << std::endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
signed main(){
vector<int> v(10);
for(int i=0;i<10;i++) cin>>v[i];
sort(v.rbegin(),v.rend());
for(int i=0;i<3;i++) cout<<v[i]<<endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.