當前位置:
首頁 >
【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)
發布時間:2023/12/10
44
豆豆
生活随笔
收集整理的這篇文章主要介紹了
【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:
小希希望你構造一個最小的正整數,使得其有n個因子。
輸入描述:
第一行一個整數T表示數據組數每組數據第一行輸入一個正整數n,表示其因子數。n≤1,000,000n≤1,000,000T≤1,000,000T≤1,000,000輸出描述:
輸出一行一個整數,表示你構造出的這個數。注意:你需要保證你構造的數≤1,000,000≤1,000,000,如果在這個范圍里面無法構造出一個正整數滿足條件,請輸出-1。示例1
輸入
復制
2 4 5輸出
復制
6 16解題報告:
? ?這題一眼反素數,,每次都查詢會超時,打表后發現240以上都輸出-1就行了。標程是篩了一下素數然后維護答案的,也可以。(用時分別是180ms和360ms)
這也告訴我們,1e6以內的數,因子個數最多240個左右,還可以證明,1e9以內的數,因子個數1e5以內,當個小結論、、
AC代碼1:(反素數)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e6 + 5; ll biao[55] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; ll ans[MAX]; ll minn = 1e18+5; ll n; void dfs(ll dep,ll tmp,ll cur) {if(cur > n) return ;if(cur==n) minn = min(tmp,minn);for(ll i = 1; i<=63; i++) {if(minn < tmp * biao[dep]) break;tmp = tmp * biao[dep];dfs(dep+1,tmp,cur*(i+1));} } int main() {int t;cin>>t;memset(ans,-1,sizeof ans);for(n = 1; n<=250; n++) {minn = 1e18+5;dfs(0,1,1),ans[n] = minn;}while(t--) {scanf("%lld",&n);minn = 1e18+5;if(n > 241) printf("-1\n");else {if(ans[n]>1000000) printf("-1\n");else printf("%lld\n",ans[n]); } }return 0 ;}AC代碼2:(注意這里的MAX一定是1e6+1,,不能打表打大了,,不然不滿足構造的數小于1e6這個條件、、)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 1e6 + 1; int cnt[MAX], ans[MAX],n; int main() {memset(ans, -1, sizeof(ans));for(int i = 1; i < MAX; i++)for(int j = i; j < MAX; j += i)cnt[j]++;for(int i = 1; i < MAX; i++)if(ans[cnt[i]] == -1) ans[cnt[i]] = i; int t;cin>>t;while(t--) {scanf("%d",&n);printf("%d\n",ans[n]);}return 0; }?
總結
以上是生活随笔為你收集整理的【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【CF - 699C】 Vacation
- 下一篇: 【CCFCSP- 201312-4】有趣