Sicily 1203. The Cubic End(数论乘法)
来源:互联网 发布:mac pro 2017发布时间 编辑:程序博客网 时间:2024/06/12 00:00
题目大意:立方求尾数。求一个数的立方尾数等于本身。
解题思路:
1.判断尾数相同,不需要将整个数乘完再取尾数。只要从低位开始,一位一位逐个匹配即可。
2.尾数为1,3,7,9时,1立方尾数为1,3立方尾数为7,7立方尾数为3,9立方尾数为9。
所以,最后一位只有相应4种可能。
3.取尾数操作即求模,取最后一位即模10,最后两位模10^2。
4.在立方取模时运用到数论乘法求模。
a^3 % m = (((a * a) % m) * a) % m
因为a<m,所以先进行a*a再求模。利用公式减小数值范围。
5.但在大于等于10位时,long long也会溢出。所以运用到数论中的乘法求模,在乘法过程中不断求模。
相关介绍:点击打开链接
// 1203. The Cubic End(快速乘法) #include <cstdio>#include <cmath>#include <cstring> using namespace std; long long mul(long long a, long long b, long long mod) { long long c = 0; const int base = 2; for (; b != 0; b /= base) { c += (b % base) * a; c %= mod; a = (a * base) % mod; } return c;}long long cube(long long x, long long mod){ return mul(mul(x,x,mod), x, mod); }int main () { //freopen("D:\\input.txt","r",stdin); int t, length; long long result, remain, power, step; char str[11]; scanf("%d",&t); while( t-- ){ scanf("%s",&str); length = strlen(str); result = remain = str[length-1] - '0'; power = 10; if( result == 3 ) result = 7; else if( result == 7 ) result = 3; for( int i = length - 2; i >= 0; --i ){ remain += (str[i] - '0') * power; step = power; power *= 10; while( cube(result,power) != remain ){ result += step; } } printf("%lld\n",result); } return 0; }
0 0
- Sicily 1203. The Cubic End(数论乘法)
- sicily 1203. The Cubic End
- 1203. The Cubic End(高精度乘法)
- 1203. The Cubic End
- Hdu 4596 Yet another end of the world(数论)
- HDU-4596 Yet another end of the world (数论)
- (数论)HDU 4596 Yet another end of the world题解
- Sicily 1302 Magic Square (数论)
- hdu 4596 Yet another end of the world(数论)
- The Balance(数论)
- sicily 1381 高精度乘法。
- Sicily - 1381 大数乘法
- 最少乘法次数-数论
- 【数论】乘法逆元
- The End.
- The end
- THE END
- The end
- Linux中断(interrupt)子系统
- 字符串反转求分析为何输出有误
- ZStack协议栈SimpleApp小解
- Linux Daemon Writing HOWTO
- 线索二叉树 算法
- Sicily 1203. The Cubic End(数论乘法)
- 传输协议:Soup/Http/Https/TCPip
- wince工程未安装SDK报错解决办法
- javascript高级程序设计学习笔记三
- iOS编程技巧总结之语音篇--前言
- android gridview保留点击后的背景黄色
- Js获取当前日期时间及其它操作
- gdb debug tutorial
- 一分钟内算出21位数的水仙花数