UVA Multiplying by Rotation

 Multiplying by Rotation 

Warning: Not all numbers in this problem are decimal numbers!

Multiplication of natural numbers in general is a cumbersome operation. In some cases however the product can be obtained by moving the last digit to the front.

Example: 179487 * 4 = 717948

Of course this property depends on the numbersystem you use, in the above example we used the decimal representation. In base 9 we have a shorter example:

17 * 4 = 71 (base 9)

as (9 * 1 + 7) * 4 = 7 * 9 + 1

Input

The input for your program is a textfile. Each line consists of three numbers separated by a space: the base of the number system, the least
significant digit of the first factor, and the second factor. This second factor is one digit only hence less than the base. The input file ends with the standard end-of-file marker.

Output

Your program determines for each input line the number of digits of the smallest first factor with the rotamultproperty. The output-file is also
a textfile. Each line contains the answer for the corresponding input line.

Sample Input

10 7 4
9 7 4
17 14 12

Sample Output

6
2
4

Miguel A. Revilla

1998-03-10

题意:给出3个数据a,b,c,b代表一个十进制的数k(题目未给出,不需要求)的个位数字,然后k*c得到的结果在a进制(例如八进制,十进制,十六进制)下的值p的首位数也是b,要求输出符合k要求的最小位数。

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int main()
{
    int a,b,c;
    while(cin >> a >> b >> c)
    {
        int count = 0;
        int sum = 0;
        int pb = b;
        int p = 0;
        while(1)
        {
            count++;
            sum = b * c + p;
            b = sum % a;
            p = sum / a;
            if(sum == pb)
            {
                break;
            }
        }
        cout << count << endl;
    }
    return 0;
}

时间: 2024-12-04 20:08:23

UVA Multiplying by Rotation的相关文章

寒假集训.Multiplying by Rotation

Multiplying by Rotation Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 550 Description Warning: Not all numbers in this problem are decimal numbers! Multiplication of natural numbers in general is a

uva 1379 - Pitcher Rotation(dp)

题目链接:uva 1379 - Pitcher Rotation 题目大意:给出n,表示由n个人组成的战队,接着是m,表示有m个敌人,g表示要比赛的天数,给出g后,比赛的天数起始是g+10,然后是一个m*n的矩阵,g[i][j]表示第j个敌人被i打败后,战队所得到的分数.然后g+10个数,表示每天需要挑战的敌人是谁,0代表说休息:每个人出战的次数没有限制,但是比过一场后要休息4天,问说战队的最高得分. 解题思路:注意题目中的一点,说每个人比完一场赛后要休息4天,那么也就是说对于每个敌人来说,被派

UVA 1379 - Pitcher Rotation(DP + 贪心)

题目链接:1379 - Pitcher Rotation 题意:n个人,m个敌人,去比赛,有得分,n个人可以重复比,但是每次比完要休息4天,问最大得分 思路:dp[i][j][k][l][x] 表示第场比赛,前一天为j,两天为k,三天为l,四天为x,的最大得分,然后由于只有每个人5天就能用一次,所以对于每个人来说,只有得分前5的会被使用上,所以后4维状态只需要5^4,进行状态转移,不用比赛的情况分开考虑,还有这题内存有限,要用滚动数组优化不然会RE 代码: #include <stdio.h>

UVA 550 Multiplying by Rotation (简单递推)

题意:有些数字是可以这样的:abcd*k=dabc,例如179487 * 4 = 717948,仅仅将尾数7移动到前面,其他都不用改变位置及大小.这里会给出3个数字b.d.k,分别代表b进制.尾数.第2个乘数.既然是尾数,必有d<b.求这个abc...d一共有几位数(按b进制算). 思路:举例,进制是十,假设这个数字是6位的,那么abcdef代表了这6位数,f已经给出,是7,且另个乘数是4.因为abcde7*4=7abcde. (1)推e : 4*7=28,那么e一定是为8. (2)推d:abc

UVa 1343 The Rotation Game (状态空间搜索 &amp;&amp; IDA*)

题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使中心8个格子数字一致的最少步骤,要输出具体的操作步骤及最终中心区域的数字.如果有多个解,输出字典序最小的操作步骤. 分析 : 还是状态空间的搜索,对象就是一个数字序列,判断中心位置是否一样,可以看出如果使用BFS,每一层还是爆炸,所以使用IDA*,关键还是模拟操作和h函数,这里的h函数是这样定义的,

UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: 3)采用广度优先搜索结点数越来越多,耗时过大: 经过简单计算,最长大概10次左右的变换就能出解,于是笔者就尝试采用IDA*,迭代加深搜索的好处是: 1)无需存储状态,节约时间和空间: 2)深度优先搜索查找的结点数少: 3)递归方便剪枝: 代码如下: 1 #include <iostream> 2

UVA 1343 The Rotation Game

题意: 给出图,往A-H方向旋转,使中间8个格子数字相同.要求旋转次数最少,操作序列字典序尽量小. 分析: 用一维数组存24个方格.二维数组代表每个方向对应的7个方格.IDA*剪枝是当8-8个方格中重复字母最多的那个字母数量>maxd. 代码: #include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;int a[24];i

UVa 1343 The Rotation Game(IDA*)

主要是设计乐观估计函数来减枝 假设中心区域有6个2,2个3,那肯定是消掉3最好,毕竟就两个. 那么理想情况下,旋转一次就能把一个3变成2,那么最少操作2次. 我们用h()来计算最少还要操作几次,其原理是假设中心区域都放1或2或3,返回至少操作的次数中最小的数 maxd是假设最多能操作的数; d是已经操作的数; 那么就可以得出乐观估计函数   h()+d>maxd 其含义为 : 若  至少还要操作次数  加上  已经操作的次数  大于  最多总共操作的次数就退出 . 其次就是节点的处理了,编个号数

UVA - 1343 The Rotation Game (BFS/IDA*)

题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 typedef long long ll; 5 const int N=24+2,inf=0x3f3f3f3f; 6 const int b[][10]= { 7 {0,2,6,11,15,20,22}, 8 {1,3,8,12,17,21,23}, 9 {10,9,8,7,6,5,4}, 10 {19,18,17,16,1