我目前正在Codeforces中尝试出现问题。每当我在计算机上运行程序时,它就会完美运行。但是,当我提交以下代码时,第一个测试用例出现错误。我尝试将变量的类型从“ long long”更改为“ signed int”,但是,我一直遇到相同的错误。我该如何解决这个问题?
测试用例
100000 2
错误消息
Diagnostics detected issues [cpp.clang++-diagnose]: p71.cpp:20:16: runtime error: signed integer overflow: 46349 * 46349 cannot be represented in type 'long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior p71.cpp:20:16 in
代码
#include <iostream>
#include <vector>
#include <bitset>
#define ll long long
using namespace std;
bitset<110001> b;
vector<ll> primes;
vector<ll> factors;
void sieve() {
b.set();
for(long i=2;i<110001;i++) {
if(b[i]) {
for(long j=i*i;j<110001;j+=i) {
b[j] = 0;
}
primes.push_back(i);
}
}
}
void prime_factors(ll n) {
sieve();
vector<ll>::iterator it = primes.begin();
ll pf = (*it);
ll num = n;
ll divisor_count = 1;
ll power;
while(pf <= n) {
while(num%pf == 0) {
num = num/pf;
factors.push_back(pf);
}
it++;
pf = (*it);
}
}
int main() {
ll n,k;
cin>>n;
cin>>k;
prime_factors(n);
if(factors.size() < k) {
cout<<-1;
}
else {
ll i;
for(i=0;i<k-1;i++) {
cout<<factors.at(i)<<" ";
}
ll last = 1;
for(ll j=i;j<factors.size();j++) {
last = last*factors.at(j);
}
cout<<last;
}
return 0;
}