Gym-101490H Multiplying Digits(进制转化--倒着)
来源:互联网 发布:房贷利息抵扣个税算法 编辑:程序博客网 时间:2024/05/18 16:16
传送门
题目说的是给一个数n,求在B进制下的每一位的乘积等于n的条件下的最小的数
先得到n小于b的因子,然后在因子乘积等于n的约束下组合因子,得到最小的mp[1]
保证最后的解,位数一定最少,而且不严格递增。
//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define s_1(x) scanf("%d",&x)#define s_2(x,y) scanf("%d%d",&x,&y)#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)#define S_1(x) scan_d(x)#define S_2(x,y) scan_d(x),scan_d(y)#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long long#define mp make_pair#define pb push_backtypedef long long LL;typedef pair <int, int> ii;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=4e3+10;const int maxx=4e5+10;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;unsigned long long maxc=1ull<<63;map<LL,LL>Q;map<LL,LL>::reverse_iterator it;LL a[maxx];LL b,n;void solve(){ W(scanf("%I64d%I64d",&b,&n)!=EOF) { if(n==1) { print(1); continue; } LL tmp=n,tot=0; //首先对n进行分解 for(int i=2;i<b;i++) { if(n%i==0) a[tot++]=i; W(tmp%i==0) { tmp/=i; } } if(tmp!=1) { puts("impossible"); continue; } Q.clear(); Q[n]=0; for(it=Q.rbegin();it!=Q.rend();it++) { LL u=it->first,v=it->second; LL mo=v%b; for(int i=tot-1;i>=0;i--) { if(a[i]<mo) break;/*最优解的组成为非严格递增,所以若后一位小于前一位,则一定不是整体最优解*/ if(((long double)v*b+a[i])>maxc) continue;//防止爆LL if(u%a[i]) continue; LL x=u/a[i],y=v*b+a[i]; if(!Q[x]||Q[x]>y) Q[x]=y; } } print(Q[1]); }}int main(){ //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); int t=1; //s_1(t); for(int cas=1;cas<=t;cas++) { //printf("Case %d:\n",cas); solve(); }}
阅读全文
0 0
- Gym-101490H Multiplying Digits(进制转化--倒着)
- Multiplying Digits Gym
- Gym-101490H
- Gym 100531 D Digits 【暴力】
- Multiplying by Rotation——进制循环节
- GYM 100523H
- codeforces GYM 100792H
- Gym 100712H Bridges
- Gym 100947H-Phobia
- Gym - 100735H
- Gym 101164.H
- Gym 101246.H
- Gym 101482.H
- Gym 100825 H
- Gym 101201.H
- Gym 101617H
- Gym 100712I Bahosain and Digits(枚举)
- Gym 100531D Digits(暴力)
- Selenium3+Python3+PhantomJS配置及测试解析
- 2-3 jdk安装
- jsp直接显示数据库表格信息
- TensorFlow 逻辑回归样例代码
- leetcode
- Gym-101490H Multiplying Digits(进制转化--倒着)
- Java 没有无符号数
- bzoj 2957: 楼房重建(分块)
- Andorid:Service启动的两种方式及区别
- Anaconda学习总结
- TensorFlow 模拟线性回归代码样例
- Java内存泄漏总结
- PTA 7-21(排序) PAT排名汇总(25 分)25分代码
- Web前端攻城狮-Day1-HTML基础