【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)
生活随笔
收集整理的這篇文章主要介紹了
【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題干:
小希希望你構(gòu)造一個(gè)最小的正整數(shù),使得其有n個(gè)因子。
輸入描述:
第一行一個(gè)整數(shù)T表示數(shù)據(jù)組數(shù)每組數(shù)據(jù)第一行輸入一個(gè)正整數(shù)n,表示其因子數(shù)。n≤1,000,000n≤1,000,000T≤1,000,000T≤1,000,000輸出描述:
輸出一行一個(gè)整數(shù),表示你構(gòu)造出的這個(gè)數(shù)。注意:你需要保證你構(gòu)造的數(shù)≤1,000,000≤1,000,000,如果在這個(gè)范圍里面無(wú)法構(gòu)造出一個(gè)正整數(shù)滿(mǎn)足條件,請(qǐng)輸出-1。示例1
輸入
復(fù)制
2 4 5輸出
復(fù)制
6 16解題報(bào)告:
? ?這題一眼反素?cái)?shù),,每次都查詢(xún)會(huì)超時(shí),打表后發(fā)現(xiàn)240以上都輸出-1就行了。標(biāo)程是篩了一下素?cái)?shù)然后維護(hù)答案的,也可以。(用時(shí)分別是180ms和360ms)
這也告訴我們,1e6以?xún)?nèi)的數(shù),因子個(gè)數(shù)最多240個(gè)左右,還可以證明,1e9以?xún)?nèi)的數(shù),因子個(gè)數(shù)1e5以?xún)?nèi),當(dāng)個(gè)小結(jié)論、、
AC代碼1:(反素?cái)?shù))
#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,,不能打表打大了,,不然不滿(mǎn)足構(gòu)造的數(shù)小于1e6這個(gè)條件、、)
#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; }?
總結(jié)
以上是生活随笔為你收集整理的【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【CF - 699C】 Vacation
- 下一篇: 【CCFCSP- 201312-4】有趣