hdu1061-Rightmost Digit(附快速幂简单原理)

杭电acm 1061 #include <iostream> #include <math.h> using namespace std; int main() { int a[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}},m,n,j; while(cin>>j) { while(cin>>n) { m=n%10; if(m==0||m==1||m==5||m==6) cout<<m<<endl; else if(m==4||m==9) cout<<a[m][n%2]<<endl; else if(m==2||m==7||m==8||m==3) cout<<a[m][n%4]<<endl; } } return 0; } 立即下载

http://acm.hdu.edu.cn/showproblem.php?pid=1061

1.快速幂实现a^N
求3^999(a=3,N=999):3 ^ 999 = 3 * 3 * 3 * … * 3,直接乘要做998次乘法。
快速幂方法实质使用了二分法进行时间优化:
 tmp+   =   tmp-*    a-;
3 ^ 1  =    3*    1
3 ^ 2  = (3 ^ 1)* (3 ^ 1)
3 ^ 4  = (3 ^ 2) *(3 ^ 2)
…………
3 ^ 256 = (3 ^ 128) * (3 ^ 128)
3 ^ 512 = (3 ^ 256) * (3 ^ 256)
==>
3 ^ 999 
= (3 ^ 512) * (3 ^ 256) * (3 ^ 128) * (3 ^ 64) * (3 ^ 32) * (3 ^ 4) * (3 ^ 2) * 3 
只做16次乘法。
将999转为2进制数:1111100111
1    1   1   1  1 0 0 1 1 1
512 256 128 64 32 0 0 4 2 1
各个位上的值即为需要进行乘积的标志。

 

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
#include "algorithm"
#include "iostream"

using namespace std;

#define INT __int64
int Pow( INT n )
{
	int temp = n % 10 ;
	int ans = 1 ;
	while( n )
	{
		if( n % 2 == 1 )
		{
			n-- ;
			ans *= temp;
			ans %= 10 ;
		}
		else
		{
			n /= 2 ;
			temp *= temp ;
			temp %= 10 ;
		}
		ans %= 10 ;
	}
	return ans ;
} 
	
int main()
{
	INT n ;
	int Case ;
	cin >> Case ;
	while( Case-- )
	{
		cin >> n ; 
		cout << Pow( n ) << endl ;
	}
	return 0;
}


 

下面是发现没20个数据存在循环,之前没有将num[ 0] 赋值为0 ,总是WA,还怀疑规律是不是错了,但是突然发现20的倍数mod20会为0

#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std; 

int main()
{
	__int64 n ;
	int a , b , num[ 50 ] ;
	int i ;
	num[ 0 ] = 0 ; 
	for(i = 1 ; i <= 20 ; i++ )
	{
		a = i ;
		b = 1 ;
		while ( a-- )
		{
			b = b * i % 10 ;
		}
		num[ i ] = b ;
	}
	int Case ;
	cin >> Case;
	{
	while( Case-- )
	{	
		cin >> n ;
		n = n % 20 ;
	//	cout << n << endl ;
		cout << num[ n ] << endl ;
	}
}
	return 0 ;
}


 

qt项目中添加 jsoncpp 库 qt使用jsoncpp库 阅读详情

相关推荐

Scipy优化踩坑实录:trust-constr和SLSQP约束定义到底差在哪?

本文深入解析了Scipy优化模块中trust-constr和SLSQP两种方法在约束定义上的差异。通过对比面向对象的trust-constr和字典式定义的SLSQP,揭示了它们各自的优势与适用场景,帮助开发者在有约束优化问题中选择合适的算法,提升计算效率与准确性。

weixin_42573113的博客 290

hdu1061(快速幂求模)

Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test ca...

weixin_44013301的博客 349

【智能制造】- APS系列|05 APS 排产的五大底层算法逻辑

很多人误以为APS只是给生产工单排先后顺序,实则整套系统依托成熟算法体系,完成多维度约束下的最优生产决策在制造业数字化排产普及的当下,依旧有不少制造管理者对APS存在认知误区,将其等同于简易工单排序工具。事实上,专业级高级计划与排程系统,早已脱离单纯按时间排序的基础功能,而是依托一整套成熟完善的底层算法逻辑,整合现场各类生产限制条件,在海量变量中自动推演、筛选、输出最贴合企业经营目标的生产方案。

qq_40821260的博客 463

Rightmost Digit(HDU1061) 两种方法 快速幂、打表

Problem Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test

Shanhj的博客 397

【PINN物理信息网络】基于PINN物理信息网络求解圆柱坐标中的热方程(python)

【PINN物理信息网络】基于PINN物理信息网络求解圆柱坐标中的热方程(python)

k8291121的博客 2283

secureCRT 破解方法

3、输入注册信息打开keygen.exe,点击path,选择SecureCRT的安装目录(例如D:\VanDykeSoftware\Clients)的securecrt.exe打开,继续下一步打开LicenseHelper.exe,如下。1、注册SecureCRT9.1打开keygen.exe,出现以下问题(没有关掉安全防护,一双击打开,Windows会自动识别keygen.exe为bingdu并删除)打开桌面SecureCRT,此界面不输入内容,直接点击下一步。2、存储配置文件夹(可以默认安装)

逆水行舟 1万+

hdu1061 快速幂取模

** Rightmost Digit ** Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 82115 Accepted Submissi...

fyh2944163240的博客 206

SRS流媒体服务器在Windows下的安装

在之前的流媒体开发中(视频会议),我基本上使用nginx加模块的方式进行学习、测试,正式开发的时候改为服务器端用EasyDarwin,客户端用rtsp-simple-server(这些流媒体服务器的用法见我以前的博文),这样可以充分利用客户端的资源,降低服务器端的压力。在流媒体协议方案,我选择相对较快的rtsp。这个协议优点是支持UDP协议,优化好的环境下可以实现3秒以下的延迟,基本满足甲方要求,缺点就是不支持网页播放。

dgnankai的专栏 2041

HDU 1061 找规律

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1061 题目求最左边的一位。这就明显是找个规律打表了。因为只和最后一位有关。他又是只有n去乘。#include<bits/stdc++.h> #define INF 1e18 #define inf 1e9 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1

我不吃海鲜的博客 310

huggingface下载提示huggingface_hub.errors.EntryNotFoundError:404 Client Error: (Request 1D: Root...如何解决?

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1248

【机器学习系列】一文搞懂 零知识机器学习(zkML)

位串行乘法器:实现了Booth编码优化,将乘法分解为移位-加法序列,减少部分积数量50%:模拟脉动阵列的2D网格计算,支持分块(tiling)策略优化内存层次卷积映射:提供直接卷积(行缓冲+滑动窗口)和im2col+GEMM两种硬件映射策略。

VectorShift的博客 170

hdu1061

使用快速幂的思想 /** * 快速幂算法 * @param a 底数 * @param b 指数 * @param c 模 * @return */ public static int pow(int a, int b, int c) { int ans = 1; a %= c; // 降低了 a 的规模 while (b > 0) { if ((b & 1...

weixin_42634193的博客 583

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

hdu1061-Rightmost Digit(附20循环的规律解法和附快速幂简单原理)

http://acm.hdu.edu.cn/showproblem.php?pid=1061 1.快速幂实现a^N 求3^999(a=3,N=999):3 ^ 999 = 3 * 3 * 3 * … * 3,直接乘要做998次乘法。 快速幂方法实质使用了二分法进行时间优化:  tmp+   =   tmp-*    a-; 3 ^ 1  =    3*    1 3 ^ 2  = (3

bo-jwolf的专栏 2645

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

HDU1059——Dividing,HDU1060——Leftmost Digit,HDU1061——Rightmost Digit

用于计算快速幂取模。

u014114223的博客 669

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Rightmost Digit(HDU 1061 快速幂取模)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1061 题面: 翻译: 问题描述 给定一个正整数N,你应该输出N^N中最右边的数字。 输入 输入包含几个测试用例。输入的第一行是一个整数T,它是测试用例的数量。接下来是T测试用例。 每个测试用例包含一个正整数N(1<=N<= 1000,000,000)。 输出 对于每个测试用例,您应该输...

kunyuwan的博客 1952

ACM--快速幂--HDOJ 1061--Rightmost Digit

HDOJ地址:http://acm.hdu.edu.cn/showproblem.php?pid=1061 快速幂算法讲解:http://blog.csdn.net/qq_26891045/article/details/51334101 Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)

戎码人生 2687

HDU-1061 Rightmost Digit(快速幂)

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1061 测试样例 Sample Input 2 3 4 Sample Output 7 6 Hint In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6. 题意: 求NNN^NNN最右边的数

pursuit的博客 519

快速幂入门题之Rightmost Digit

HDU 1061 Problem Description Given a positive integer N, you should output the most right digit of N^N Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Ea

kitalekita的博客 234

hdu1061 rightmost digit(C语言)

Problem Description Given a positive integer N, you should output the most right digit of N^N.   Input The input contains several test cases. The first line of the input is a single in

sinat_39591298的博客 1161

Rightmost Digit HDU - 1061(快速幂)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 73702 Accepted Submission(s): 27150 Problem Description Given a positive integer N, you shou...

RouTine 151

hdu1061 Rightmost Digit 标准快速幂

Problem Description Given a positive integer N, you should output the most right digit of N^N.   Input The input contains several test cases. The first line of the input is a single in

小川先生的博客 637

hdu 1061 Rightmost Digit(快速幂取余)

一般的求幂再对10取余会超时,用快速幂 #include #include using namespace std; int mod_exp(int a, int b, int c) //快速幂取余a^b%c { int res, t; res = 1 % c; t = a % c; while (b) {

Pegasus (http://ningning.today) 1068

HDU 1061-Rightmost Digit

题目 Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 67183Accepted Submission(s): 25198 Problem Desc...

187

HDU 1061 Rightmost Digit( 快速幂水 )

链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /************************************************************************* > File Name: hdu1061.cpp > Author: WArobot > Blog: http://...

dijingzhou4259的博客 179
上一篇: hdu1081-To The Max
下一篇: hdu2060-Snooker
luckycoding
博客等级 码龄15年 108粉丝 113原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址