首页 > 其他分享 >Math Problem

Math Problem

时间:2023-08-18 17:24:56浏览次数:65  
标签:frac into coins 金币 leq integer Problem Math

                                                                                            E-Math Problem

## 题面翻译

**【题目描述】**

给定两个正整数 $n$ 和 $k$,您可以进行以下两种操作任意次(包括零次):

- 选择一个整数 $x$ 满足 $0 \leq x < k$,将 $n$ 变为 $k\cdot n+x$。该操作每次花费 $a$ 枚金币。每次选择的整数 $x$ 可以不同。
- 将 $n$ 变为 $\lfloor \frac{n}{k} \rfloor$。该操作每次花费 $b$ 枚金币。其中 $\lfloor \frac{n}{k} \rfloor$ 表示小于等于 $\frac{n}{k}$ 的最大整数。

给定正整数 $m$,求将 $n$ 变为 $m$ 的倍数最少需要花费几枚金币。请注意:$0$ 是任何正整数的倍数。

**【输入格式】**

有多组测试数据。第一行输入一个整数 $T$($1\leq T\leq 10^5$)表示测试数据组数。对于每组测试数据:

第一行输入五个正整数 $n$,$k$,$m$,$a$,$b$($1\leq n\leq 10^{18}$,$1\leq k, m, a, b\leq 10^9$)。

**【输出格式】**

每组数据输出一行一个整数,代表将 $n$ 变为 $m$ 的倍数最少需要花费几枚金币。如果无法完成该目标,输出 $-1$。

**【样例解释】**

对于第一组样例数据,一开始 $n=101$,最优操作如下:

- 首先进行一次第二种操作,将 $n$ 变为 $\lfloor \frac{n}{4} \rfloor=25$,花费 $5$ 枚金币。
- 接下来进行一次第一种操作,选择 $x = 3$,将 $n$ 变为 $4\cdot n+3=103$,花费 $3$ 枚金币。
- 接下来进行一次第一种操作,选择 $x = 2$,将 $n$ 变为 $4\cdot n+2=414$,花费 $3$ 枚金币。
- 此时 $414=2 \times 207$,满足 $n$ 是 $m$ 的倍数。共花费 $5+3+3=11$ 枚金币。

对于第二组样例数据,进行两次第二种操作将 $n$ 变为 $0$。共花费 $1 + 1 = 2$ 枚金币。

对于第三组样例数据,因为 $n = 114 = 6 \times 19$ 已经是 $m$ 的倍数,因此无需进行任何操作。共花费 $0$ 枚金币。

## 题目描述

Given two positive integers $n$ and $k$, you can perform the following two types of operations any number of times (including zero times):

- Choose an integer $x$ which satisfies $0 \leq x < k$, and change $n$ into $k\cdot n+x$. It will cost you $a$ coins to perform this operation once. The integer $x$ you choose each time can be different.
- Change $n$ into $\lfloor \frac{n}{k} \rfloor$. It will cost you $b$ coins to perform this operation once. Note that $\lfloor \frac{n}{k} \rfloor$ is the largest integer which is less than or equal to $\frac{n}{k}$.

Given a positive integer $m$, calculate the minimum number of coins needed to change $n$ into a multiple of $m$. Please note that $0$ is a multiple of any positive integer.

## 输入格式

There are multiple test cases. The first line of the input contains an integer $T$ ($1\leq T\leq 10^5$) indicating the number of test cases. For each test case:

The first line contains five integers $n$, $k$, $m$, $a$, $b$ ($1\leq n\leq 10^{18}$, $1\leq k, m, a, b\leq 10^9$).

## 输出格式

For each test case output one line containing one integer, indicating the minimum number of coins needed to change $n$ into a multiple of $m$. If this goal cannot be achieved, output $-1$ instead.

## 样例 #1

### 样例输入 #1

```
4
101 4 207 3 5
8 3 16 100 1
114 514 19 19 810
1 1 3 1 1
```

### 样例输出 #1

```
11
2
0
-1
```

## 提示

For the first sample test case, initially $n=101$. The optimal steps are shown as follows:

- Firstly, perform the second type of operation once. Change $n$ into $\lfloor \frac{n}{4} \rfloor=25$. This step costs $5$ coins.
- Then, perform the first type of operation once. Choose $x = 3$ and change $n$ into $4\cdot n+3=103$. This step costs $3$ coins.
- Then, perform the first type of operation once. Choose $x = 2$ and change $n$ into $4\cdot n+2=414$. This step costs $3$ coins.
- As $414=2 \times 207$, $n$ is a multiple of $m$. The total cost is $5+3+3=11$ coins.

For the second sample test case, perform the second type of operation twice will change $n$ into $0$. The total cost is $1 + 1 = 2$ coins.

For the third sample test case, as $n = 114 = 6 \times 19$ is already a multiple of $m$, no operation is needed. The total cost is $0$ coins.

 

很显然要先除,然后再乘,枚举除的次数,一直到0为止

设一个l,r表示区间,如果说当前的值在这个区间内得到的除数都相同,那就可以继续乘,否则结束,得到结果

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
string s;
int n,t,a,b,res,ans,m,k;
bool vis[N];
void solve()
{
    cin>>n>>k>>m>>a>>b;
    if(k==1){
        if(n%m) cout<<-1<<endl;
        else cout<<0<<endl;
        return;
    }
    res=2e18;
    for(int i=0;~i;n/=k,i++){
        if(n==0){
            res=min(res,i*b);
            break;
        }
        __int128_t l=n,r=n;
        int ans=i*b;
        while(l/m==r/m&&l%m){
            l*=k,r=r*k+k-1;
            ans+=a;
        }
        res=min(res,ans);
    }
    cout<<res<<endl;
    return;
}
signed main()
{
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

 

标签:frac,into,coins,金币,leq,integer,Problem,Math
From: https://www.cnblogs.com/o-Sakurajimamai-o/p/17641071.html

相关文章

  • 文件解压 //problem/2928 or /contest/1709/problem/3
    字符串套递归#include<bits/stdc++.h>usingnamespacestd;chars[1005];intn,i;stringwork(){stringp;intt=0;while(++i<=n){if(s[i]>='0'&&s[i]<='9'){t=s[i]-'0......
  • CF1858C Yet Another Permutation Problem 题解
    思路这个题是一个简单的构造题。竟然比T2简单,也是少见我们可以首先从\(1\)开始不断乘以\(2\),像这样:\(1,2,4,8,16\cdots,2^x\),直到什么时候超过\(n\)就停止。这样相邻两个数字就可以凑出\(1,2,4,6,\cdots,2^{x-1}\),保证两两不同。然后我们可以从\(3\)开始不......
  • CF1858C Yet Another Permutation Problem 题解
    杂言赛时想到做法,结果调code把自己心态调炸了,所以来写一篇题解(恼)。另:此题与P9345夕阳西下几时回几乎相同,可以此练手。另另:本题多测,多测不清空,爆零两行泪。题意翻译\(a_1,a_2,\dots,a_n\)是\(1\)至\(n\)的一个排列,记\(d_i=\gcd(a_i,a_{i\bmodn+1})\)。构造一个......
  • CF776D The Door Problem
    题目大意给定门和钥匙的数量,每把钥匙控制\(k_i\)扇门,每扇门被两把钥匙控制。给定初始时每扇门的状态,求是否存在一种方法使得所有的门都打开。思路扩展域并查集。考虑分类讨论:对于开着的门,要么两把钥匙都用,要么两把钥匙都不用;对于关着的门,两把钥匙只能用一把。那么我们......
  • 胡萝卜问题 Carrot Problems
    Refhttps://www.atvbt.com/the-carrot-problem/......
  • 【机器学习|数学基础】Mathematics for Machine Learning系列之矩阵理论(12):相似形理论
    目录前言往期文章3.3线性变换的最简矩阵表示-相似形理论3.3.1一般数域上矩阵相似最简形定义3.9定理3.3.1前言Hello!小伙伴!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ 自我介绍ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序猿|C++选手|学生简介:因C语言结识编程,随后转入计算......
  • 【机器学习|数学基础】Mathematics for Machine Learning系列之矩阵理论(13):Hamliton-Cay
    目录前言Hello!小伙伴!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ 自我介绍ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序猿|C++选手|学生简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python学习经验:扎实基础+多做笔记+......
  • 【机器学习|数学基础】Mathematics for Machine Learning系列之矩阵理论(15):矩阵的范数
    前言Hello!小伙伴!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ 自我介绍ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序猿|C++选手|学生简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python学习经验:扎实基础+多做笔记+多......
  • 【机器学习|数学基础】Mathematics for Machine Learning系列之矩阵理论(16):向量和矩阵的
    前言Hello!小伙伴!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ 自我介绍ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序猿|C++选手|学生简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python学习经验:扎实基础+多做笔记+多......
  • 【机器学习|数学基础】Mathematics for Machine Learning系列之矩阵理论(20):方阵函数
    前言Hello!小伙伴!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ 自我介绍ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序猿|C++选手|学生简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python学习经验:扎实基础+多做笔记+多......