sicily 1203. The Cubic End
来源:互联网 发布:mac顶部菜单字体怎么改 编辑:程序博客网 时间:2024/05/29 18:37
题目链接在此
转自这位大神
1.乘法求模:
(1)模数不大的时候可利用性质(a*b)%c = (a%c)*(b%c)%c
(2)模数和两乘数都很大的时候,可用以下算法(暂时还没弄清原理和理论来源,就当个模板吧):
long long mulAndMod(long long a, long long b, long long mod) { // a * b % modlong long c;const int base = 2; // base可以取任意大于1的整数for (c = 0; b != 0; b /= base) {c += (b % base) * a;c %= mod;a = (a * base) % mod;}return c;}
2. 关于解法具体算法,仔细揣摩一下就能看懂代码,另外可以参考上述大神的博客中的解释。
</pre><p><span style="white-space:pre"></span><pre name="code" class="cpp">#include <iostream> #include <string>using namespace std;long long mulAndMod(long long a, long long b, long long mod) { // a * b % modlong long c;const int base = 2; // base可以取任意大于1的整数for (c = 0; 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){ // x^3 % y = (x^2 % y) * x % yreturn mulAndMod(mulAndMod(x, x, mod), x, mod);}int main() { int t;long long result; // 答案,从右向左一位位增加long long remain; // 从右向左一位位地和待比较的数比较long long power;long long step;string str; // 待比较的数int length; // 待比较数的长度cin >> t;while (t--){cin >> str;length = str.length();remain = str[length - 1] - '0'; // 初始化,取待比较的数最低位if (remain == 1) result = 1; // 待比较的数最低位若为1,则答案最低位为1else if (remain == 3) result = 7; // 待比较的数最低位若为3,则答案最低位为7else if (remain == 7) result = 3; // 待比较的数最低位若为7,则答案最低位为3else if (remain == 9) result = 9; // 待比较的数最低位若为9,则答案最低位为9power = 10;for (int i = length - 2; i >= 0; --i){ // 从待比较数的倒数第二位开始remain += (str[i] - '0') * power; // 更新remain值step = power;power *= 10; // 若remain有两位,则模数为100;若remain有三位,则模数为1000……// (result^3 % power)与remain比较,直到相等。此时result低位中对应remain的那几位已经确定while (cube(result, power) != remain){// 若remain有两位,则result每次加10即可;若remain有三位,则result每次加100即可……result += step;}}cout << result << '\n';}return 0;}
0 0
- sicily 1203. The Cubic End
- Sicily 1203. The Cubic End(数论乘法)
- 1203. The Cubic End
- 1203. The Cubic End(高精度乘法)
- The End.
- The end
- THE END
- The end
- THE END
- The End
- The end...
- The End
- <Sicily>Catch the thief
- sicily Catch the thief
- capacity is approximately 1880 cubic inches. On the other hand
- In the end
- Fiddler教程 THE END
- persist to the end
- 使用SimpleAdapter创建ListView
- win7中如何解决“该项不适用于在指定状态下使用”
- Tecal RH5885 V3 BIOS的版本升级
- javascript中文正則表達式
- lua 遍历 table
- sicily 1203. The Cubic End
- 恢复weblogic密码
- Category的用法
- CentOS6.3配置yum源
- Interpreter模式
- LR:LR中的迭代次数设置
- java 总结
- 全世界你最美丽
- intent.putExtra()方法参数详解