QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#189291 | #4920. 挑战分解质因数 | hos_lyric# | 0 | 184ms | 3868kb | C++14 | 9.6kb | 2023-09-27 08:51:43 | 2024-07-04 02:10:03 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
#ifndef __x86_64__
#error Only x86-64 targets are supported
#endif
#include<cstdint>
#include<vector>
#include<string>
#include<iosfwd>
#define __builtin_ia32_adc(x,y,flag) __asm__("addb %3, %0\n\t" "adcq %2, %1\n\t" "setc %0":"+r"(flag),"+r"(x):"r"(y),"i"(-1):"cc")
struct bigint{// made by dengyaotriangle!
typedef unsigned long long u64;
typedef unsigned __int128 u128;
typedef std::size_t st;
std::vector<u64> data;
bigint(){}
bigint(u64 x):data(x?std::vector<u64>{x}:std::vector<u64>{}){}
bigint(const std::string &s){
st pos=s.length();
int cnt=0;
u64 val=0;
while(pos){
pos--;
if(cnt==64){
data.push_back(val);
val=0;cnt=0;
}
val|=(u64)(s[pos]=='1')<<cnt;
++cnt;
}
if(cnt&&val)data.push_back(val);
}
explicit operator std::string()const{
if(data.empty())return "0";
bool t=0;
std::string ret;
for(int i=63;i>=0;i--){
t|=(data.back()>>i)&1;
if(t)ret+='0'|((data.back()>>i)&1);
}
st i=data.size()-1;
while(i){
i--;
for(int j=63;j>=0;j--)ret+='0'|((data[i]>>j)&1);
}
return ret;
}
explicit operator bool()const{return !data.empty();}
explicit operator u64()const{return data.empty()?0:data[0];}
st digit()const{
if(data.empty())return 0;
return (data.size()<<6)-__builtin_clzll(data.back());
}
bool operator==(const bigint &a)const{return a.data==data;}
bool operator!=(const bigint &a)const{return a.data!=data;}
bool operator<(const bigint &a)const{
if(data.size()!=a.data.size())return data.size()<a.data.size();
for(st i=data.size();i;){
i--;
if(data[i]!=a.data[i])return data[i]<a.data[i];
}
return 0;
}
bool operator>(const bigint &a)const{return a<(*this);}
bool operator<=(const bigint &a)const{return !(*this>a);}
bool operator>=(const bigint &a)const{return !(*this<a);}
bigint &operator<<=(st n){
if(data.empty())return *this;
int w=n&63;st z=n>>6;
st i=data.size();
bool flg=0;
if(w&&(data.back()>>(64-w)))data.push_back(0),flg=1;
data.resize(data.size()+z);
while(i){
i--;
if(flg)data[i+z+1]|=data[i]>>(64-w);
data[i+z]=data[i]<<w;
flg|=bool(w);
}
for(st i=0;i<z;i++)data[i]=0;
return *this;
}
bigint &operator>>=(st n){
int w=n&63;st z=n>>6,i=0;
for(;i+z<data.size();i++){
if(w&&i)data[i-1]|=data[i+z]<<(64-w);
data[i]=data[i+z]>>w;
}
while(data.size()>i)data.pop_back();
while(!data.empty()&&data.back()==0)data.pop_back();
return *this;
}
bigint operator<<(st n)const{return bigint(*this)<<=n;}
bigint operator>>(st n)const{return bigint(*this)>>=n;}
bigint &operator+=(const bigint &a){
data.resize(std::max(data.size(),a.data.size()));
bool carry=0;
for(st i=0;i<data.size();i++){
u64 rg=0;
if(i<a.data.size())rg=a.data[i];
__builtin_ia32_adc(data[i],rg,carry);
}
if(carry)data.push_back(1);
return *this;
}
bigint &operator-=(const bigint &a){
bool carry=1;
for(st i=0;i<data.size();i++){
u64 rg=-1;
if(i<a.data.size())rg=~a.data[i];
__builtin_ia32_adc(data[i],rg,carry);
}
while(!data.empty()&&data.back()==0)data.pop_back();
return *this;
}
bigint &operator++(){return *this+=bigint(1);}
bigint &operator--(){return *this-=bigint(1);}
bigint operator++(int){bigint tmp=*this;++*this;return tmp;}
bigint operator--(int){bigint tmp=*this;--*this;return tmp;}
bigint &operator*=(const bigint &a){
std::vector<u64> ret(data.size()+a.data.size());
for(st i=0;i<data.size();i++){
u64 carry=0;bool wcarry=0;
st k=i;
for(st j=0;j<a.data.size();j++,k++){
u128 r=data[i]*(u128)a.data[j]+carry;
u64 cur=r;
carry=r>>64;
__builtin_ia32_adc(ret[k],cur,wcarry);
}
while(carry||wcarry){
__builtin_ia32_adc(ret[k],carry,wcarry);
carry=0;k++;
}
}
while(!ret.empty()&&ret.back()==0)ret.pop_back();
data=ret;
return *this;
}
bigint &operator/=(const bigint &a){
if(a.digit()>digit()){
data.clear();
return *this;
}
st z=digit()-a.digit();
std::vector<u64> ret;
while(1){
bigint tmp=a<<z;
if(tmp<=*this){
*this-=tmp;
st v1=z>>6;
if(ret.size()<=v1)ret.resize(v1+1);
ret[v1]|=(u64)(1)<<(z&63);
}
if(!z)break;
z--;
}
data=ret;
return *this;
}
bigint &operator%=(const bigint &a){
if(a.digit()>digit())return *this;
st z=digit()-a.digit();
while(1){
bigint tmp=a<<z;
if(tmp<=*this)*this-=tmp;
if(!z)break;
z--;
}
return *this;
}
bigint operator+(const bigint &a)const{return bigint(*this)+=a;}
bigint operator-(const bigint &a)const{return bigint(*this)-=a;}
bigint operator*(const bigint &a)const{return bigint(*this)*=a;}
bigint operator/(const bigint &a)const{return bigint(*this)/=a;}
bigint operator%(const bigint &a)const{return bigint(*this)%=a;}
};
std::istream &operator>>(std::istream &st,bigint &a){
std::string s;st>>s;a=bigint(s);return st;
}
std::ostream &operator<<(std::ostream &st,const bigint &a){
return st<<(std::string)(a);
}
////////////////////////////////////////////////////////////////////////////////
void swap(bigint &a, bigint &b) {
a.data.swap(b.data);
}
int bsf(const bigint &a) {
for (int i = 0; i < (int)a.data.size(); ++i) if (a.data[i]) {
return i << 6 | __builtin_ctzll(a.data[i]);
}
assert(false);
}
bigint gcd(bigint a, bigint b) {
if (!a) return b;
if (!b) return a;
const int s = min(bsf(a), bsf(b));
a >>= bsf(a);
do {
b >>= bsf(b);
if (a > b) swap(a, b);
b -= a;
} while (b);
return a << s;
}
bigint power(const bigint &a, const bigint &e, const bigint &m) {
bigint b = 1;
for (int i = (int)e.digit(); --i >= 0; ) {
b = (b * b) % m;
if (e.data[i >> 6] >> (i & 63) & 1) b = (b * a) % m;
}
return b;
}
bool isOne(const bigint &a) {
return (a.digit() == 1 && (unsigned long long)a == 1);
}
#include <random>
mt19937_64 rng(58);
bigint N, M;
vector<bigint> ans;
int E;
bigint L;
void solve(const bigint &n) {
if (isOne(n)) {
return;
}
constexpr int NUM_ITERS = 10;
for (int iter = 0; iter < NUM_ITERS; ++iter) {
bigint a;
a.data.resize(n.data.size());
for (int i = 0; i < (int)n.data.size(); ++i) {
a.data[i] = rng();
}
if (isOne(gcd(a, n))) {
a = power(a, L, n);
if (!isOne(a)) {
for (; ; ) {
bigint b = (a * a) % n;
if (isOne(b)) {
const bigint g = gcd(a - 1, n);
if (!isOne(g) && g != n) {
solve(g);
solve(n / g);
return;
}
break;
}
a = b;
}
}
}
}
ans.push_back(n);
}
int main() {
for (; cin >> N >> M; ) {
ans.clear();
E = bsf(M);
L = M >> E;
bigint n = N;
{
const int s = bsf(n);
for (int i = 0; i < s; ++i) {
ans.push_back(2);
}
n >>= s;
}
for (; ; ) {
const bigint g = gcd(n, M);
if (isOne(g)) {
solve(n);
break;
}
solve(g);
n /= g;
}
sort(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (const bigint &p : ans) {
cout << p << '\n';
}
cout << flush;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 0ms
memory: 3868kb
input:
1 1
output:
0
result:
ok "0"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3816kb
input:
1000001010000010011000000000001 1000001010000000000000000000000
output:
2 100100000000001 1110100000000001
result:
ok 3 tokens
Test #3:
score: 0
Accepted
time: 3ms
memory: 3564kb
input:
100011100111101010100110101011 100011100111011001010101000000
output:
2 10011001001011 1110111000100001
result:
ok 3 tokens
Test #4:
score: 0
Accepted
time: 3ms
memory: 3632kb
input:
10001001101010010000101100001111 10001001101001111001001011011000
output:
2 1011000000011011 1100100000011101
result:
ok 3 tokens
Test #5:
score: -5
Wrong Answer
time: 4ms
memory: 3868kb
input:
11010101110001001000101110100011 10001110011111110101111001100000
output:
3 1001 100100101110011 101001011001001
result:
wrong answer 1st words differ - expected: '4', found: '3'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #31:
score: 10
Accepted
time: 66ms
memory: 3628kb
input:
100001100011000011110101000011010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010011101101001101100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 10000...
output:
2 1111000011101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 10001110100110000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
result:
ok 3 tokens
Test #32:
score: 0
Accepted
time: 72ms
memory: 3568kb
input:
100011010001011001101110110101100000111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100010000101101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001...
output:
2 100100111000011101110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 111101001101001010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
result:
ok 3 tokens
Test #33:
score: 0
Accepted
time: 181ms
memory: 3860kb
input:
11111111110101100011000111001011110101010110011000101010110011100101111010010100000001011010101111111001000000000001001110101110000001011111111011000010101011101100111110100000110001110010101101101111110111101001101010010011011100110001101100111111001000100011100000000000110101111101000010011011001 ...
output:
2 100101010001111000101000010010001010011111000110010110101010111101011111001101001100001111011001100001101011001110100011101110100010011111011111100101 110110111001101100000101011101000101110111110101110100011000000001100100100010010001010010111000100110100110110000011011010010011011100110000111100...
result:
ok 3 tokens
Test #34:
score: 0
Accepted
time: 93ms
memory: 3636kb
input:
1011010101010100110010100100011010010000000101001101011001110111011110000101110000000000000000000000000000000000000000000000000000000000000000000001001000011000011001101101110010110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1...
output:
2 1100000000111111011110010110100110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 111100010111011010001111011100001101000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
result:
ok 3 tokens
Test #35:
score: 0
Accepted
time: 150ms
memory: 3620kb
input:
1010100011010100011011010000010001100001101110110010001111110100100001111110001010101100000100001101000110010101011110000111100100001111011101001001110000011011000011010001110001001110001111000001000000101000010100111010101010010111101101010101110011010010101111110000000000000000000000000000001 1010...
output:
2 101101100011000010000111110100011100101101011001110110011100100001101000001101000100100000111000000111010111011100011000000000000000000000000000001 1110110100111010010011110001010110100111010001101011110111100101101000010011010100101000100011101101011111011010011011000000000000000000000000000001
result:
ok 3 tokens
Test #36:
score: 0
Accepted
time: 184ms
memory: 3568kb
input:
100011010100100001010111011101110001010010111011001001111100001001001100011110111110001101101111101111000101110000000110110000000011101001101001110101101000000111100010111110110100111011101001100101010110101010100110000110001000010111110111000000011101101010101001111011000010010100100010111010111011...
output:
2 101111000010110010010101110010001011010100100001001000101011001000111000011111011011011010110010001001000100100001100111001100011111100001101111111101 110000000011010011101100110100000010110111111110010101001101111110010101101101001011000100101001111110111110000000011000100110111011001100011100010...
result:
ok 3 tokens
Test #37:
score: -10
Runtime Error
input:
11010100011100001010111010011110000000001010100110010101100110100100110100100011101010010001111101111101010101001001010101010100000101000011001000100010001111110100111100011111100101010010001111110100011100101011001001000111011111100011010011000101111010011001011011100110110110100010010011011 110101...
output:
result:
Subtask #4:
score: 0
Runtime Error
Test #46:
score: 0
Runtime Error
input:
11011110000000000010100100101011111000100001100010011001110111010100101110110011111101111100001110110100010000000001111000110000100110111011000111000111001010000001010111010001111111000111100110011010111001011011010001100010101001001111111101000110011110011101011100000001101110111011000101010101101 ...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #6:
0%