日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVALive 4225 Prime Bases 贪心

發布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVALive 4225 Prime Bases 贪心 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Prime Bases

題目連接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2226

Descriptionww.co

Given any integer base b >= 2, it is well known that every positive integer n can be uniquely represented in base b. That is, we can write
n = a0 + a1* b + a2* b* b + a3* b* b* b + ...
where the coefficients a0, a1, a2, a3, ... are between 0 and b-1 (inclusive).
What is less well known is that if p0, p1, p2, ... are the first primes (starting from 2, 3, 5, ...), every positive integer n can be represented uniquely in the "mixed" bases as:
n = a0 + a1* p0 + a2* p0* p1 + a3* p0* p1* p2 + ...
where each coefficient ai is between 0 and pi-1 (inclusive). Notice that, for example, a3 is between 0 and p3-1, even though p3 may not be needed explicitly to represent the integer n.
Given a positive integer n, you are asked to write n in the representation above. Do not use more primes than it is needed to represent n, and omit all terms in which the coefficient is 0.

Input

Each line of input consists of a single positive 32-bit signed integer. The end of input is indicated by a line containing the integer 0.

Output

For each integer, print the integer, followed by a space, an equal sign, and a space, followed by the mixed base representation of the integer in the format shown below. The terms should be separated by a space, a plus sign, and a space. The output for each integer should appear on its own line.

Sample Input

123
456
123456
0

Sample Output

123 = 1 + 12 + 4235
456 = 123 + 1235 + 22357
123456 = 123 + 6235 + 42357 + 1235711 + 4235711*13

Hint

題意

給你一個數,你需要拆成素數因子的形式

比如123 = 1 + 1*2+4*2*3*5

拆成n = a0 + a1* p0 + a2* p0* p1 + a3* p0* p1* p2 + ...

的形式

給你一個數,問你怎么拆

題解:

貪心去拆就好了讓,素數乘積從大到小不斷考慮

如果超過就減去就好了

然后不斷貪

代碼

#include<bits/stdc++.h> using namespace std;const int MAXN = 30; bool flag[MAXN]; vector<int> P; void GetPrime_1() {int i, j;memset(flag, false, sizeof(flag));for (i = 2; i < MAXN; i++)if (!flag[i]){P.push_back(i);for (j = i; j < MAXN; j += i)flag[j] = true;} } string get(int p) {string k;while(p){k+=(char)(p%10+'0');p/=10;}reverse(k.begin(),k.end());return k; } int main() {GetPrime_1();long long n;while(cin>>n){if(n==0)break;long long ans = 1;for(int i=0;i<P.size();i++)ans*=P[i];long long pre = n;stack<int> S;for(int i=P.size()-1;i>=0;i--){S.push(n/ans);n=n%ans;ans/=P[i];}printf("%lld =",pre);int first = 1;if(n){printf(" 1");first = 0;}for(int i=0;i<P.size();i++){if(S.top()==0){S.pop();continue;}if(!first)printf(" +");first=0;printf(" %d",S.top());S.pop();for(int j=0;j<=i;j++){printf("*%d",P[j]);}}printf("\n");}}

轉載于:https://www.cnblogs.com/qscqesze/p/5152412.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的UVALive 4225 Prime Bases 贪心的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。