Created
April 7, 2018 19:39
-
-
Save anikseu/1e85897b6a8903981671f7f360ce7056 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Finding Divisor of a number (efficient) | |
| #include "bits/stdc++.h" | |
| using namespace std; | |
| #define MAX 10000000 | |
| #define LL long long | |
| bool Mark[MAX+10]; | |
| vector<LL>prime; | |
| void sieve(){ | |
| memset(Mark,0,sizeof(Mark)); | |
| Mark[1]=Mark[0]=1; | |
| for(LL i=2; i<=MAX; i++){ | |
| if(Mark[i]==0){ | |
| prime.push_back(i); | |
| for(LL j=i*i; j<=MAX; j+=i){ | |
| Mark[j]=1; | |
| } | |
| } | |
| } | |
| } | |
| vector<LL> Factor(LL num){ | |
| int ind=0; | |
| vector<LL>res; | |
| res.push_back(1); | |
| while(prime[ind]*prime[ind]<=num){ | |
| while(num%prime[ind]==0){ | |
| res.push_back(prime[ind]); | |
| num/=prime[ind]; | |
| } | |
| ind++; | |
| } | |
| if(num>1)res.push_back(num); | |
| return res; | |
| } | |
| int main(){ | |
| sieve(); | |
| LL num; | |
| cin>>num; | |
| vector<LL>PF=Factor(num); | |
| vector<LL>div; | |
| //for(int i=0; i<PF.size(); i++)cout<<PF[i]<<endl; | |
| cout<<endl; | |
| for(int i=0; i<PF.size(); i++){ | |
| if(i==0)div.push_back(1); | |
| else{ | |
| if(PF[i]!=PF[i-1]){ | |
| int sz=div.size(); | |
| for(int j=0; j<sz; j++){ | |
| div.push_back(PF[i]*div[j]); | |
| } | |
| } | |
| else{ | |
| int ind=div.size()-1; | |
| div.push_back(PF[i]*div[ind]); | |
| } | |
| } | |
| } | |
| //cout<<div.size()<<endl; | |
| sort(div.begin(),div.end()); | |
| for(int i=0; i<div.size(); i++){ | |
| cout<<div[i]<<endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment