当前位置: 首页 > news >正文

2019 ICPC Greater New York Region I - RationalBase(思维+进制转换)

题目链接


题目大意

给出两个质数 p 、 q p、q pq,将一个十进制数写成 a 0 + a 1 ∗ ( p q ) + a 2 ∗ ( p q ) 2 + . . . + a m ∗ ( p q ) m a_0+a_1*(\frac{p}{q})+a_2*(\frac{p}{q})^2+...+a_m*(\frac{p}{q})^m a0+a1(qp)+a2(qp)2+...+am(qp)m,实际上就是求十进制数的 ( p q ) (\frac{p}{q}) (qp)进制表示

解题思路

第一眼以为需要浮点数取模 f m o d ( ) fmod() fmod(),后来发现把 p q \frac{p}{q} qp当成一个浮点数是行不通的。还是自己思维太死板:此题解法和一般的进制转换问题大致相同,只不过每次总数不仅要除以 p p p还要乘上 q q q,才能逐位取出系数

  • n = a 0 + ( p q ) ∗ ( a 1 + a 2 ∗ ( p q ) + . . . ) n = a_0 + (\frac{p}{q})*(a_1 + a_2*(\frac{p}{q}) + ...) n=a0+(qp)(a1+a2(qp)+...)
  • a 0 = n % p a_0 = n \% p a0=n%p n n n p p p取模就可以取出每一位系数,剩下 a 1 + a 2 ∗ ( p q ) + . . . = ( n − a 0 ) ∗ ( p q ) a_1 + a_2*(\frac{p}{q}) + ... = (n - a_0)*(\frac{p}{q}) a1+a2(qp)+...=(na0)(qp)
  • 然后把总数乘 q q q再除以 p p p就可以取下一位
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;
unordered_map<int,char> mp;

void init(){
    for(int i=0;i<=9;i++){
        mp[i]=i+'0';
    }
    for(int i=0;i<=25;i++){
        mp[i+10]='A'+i;
    }
    for(int i=0;i<=25;i++){
        mp[i+36]='a'+i;
    }
    /*for(int i=0;i<=61;i++)
        cout<<mp[i]<<endl;*/
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll p,q,n;
    cin>>p>>q>>n;
    string ans="";
    init();
    while(n){
        int x=n%p;
        ans+=mp[x];
        n/=p;
        n*=q;
    }
    for(int i=ans.size()-1;i>=0;i--)
        cout<<ans[i];
    cout<<endl;
    return 0;
}

相关文章:

  • 广告营销的创新
  • Educational Codeforces Round 86 C - Yet Another Counting Problem(规律+前缀和)
  • SQL Server2005重装Performance Monitor Counter Requirement错误解决
  • UVA1616 Caravan Robbers(二分答案+小数化分数)
  • UVA1619 Feel Good(链表+前缀和)
  • Codeforces Round #637 (Div. 2) D. Nastya and Scoreboard(位运算dfs/记忆化搜索)
  • Cocoa应用程序基本运行过程
  • 我们的内心都有伤痕
  • Codeforces Round #638 (Div. 2) B. Phoenix and Beauty (构造)
  • Codeforces Round #638 (Div. 2) C Phoenix and Distribution(思维/构造)
  • Mac Application GDB Usage
  • “Shopee杯“e起来编程暨武汉大学2020年大学生程序设计大赛决赛
  • iPhone开发秘籍一书中的翻译错误
  • 2020WHU校赛 I - Interesting Matrix Problem(规律+整除分块)
  • UVA1025 A Spy in the Metro (dp)
  • MaxCompute访问TableStore(OTS) 数据
  • mysql_config not found
  • nginx 配置多 域名 + 多 https
  • React组件设计模式(一)
  • ubuntu 下nginx安装 并支持https协议
  • Yii源码解读-服务定位器(Service Locator)
  • 高程读书笔记 第六章 面向对象程序设计
  • 简析gRPC client 连接管理
  • 问题之ssh中Host key verification failed的解决
  • 我与Jetbrains的这些年
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 通过调用文摘列表API获取文摘
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • #1015 : KMP算法
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • #pragma data_seg 共享数据区(转)
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (1)(1.9) MSP (version 4.2)
  • (20050108)又读《平凡的世界》
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (31)对象的克隆
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (十六)Flask之蓝图
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (转)LINQ之路
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .net core使用ef 6
  • .NET 读取 JSON格式的数据
  • .net 中viewstate的原理和使用
  • .net/c# memcached 获取所有缓存键(keys)
  • .net对接阿里云CSB服务
  • .net中调用windows performance记录性能信息
  • @Autowired 与@Resource的区别
  • @NoArgsConstructor和@AllArgsConstructor,@Builder
  • @SpringBootApplication 包含的三个注解及其含义
  • [ C++ ] STL_list 使用及其模拟实现