[洛谷]P1591 阶乘数码 (#高精度 -1.2)

2023-05-16

题目描述

求n!中某个数码出现的次数。

输入输出格式

输入格式:

第一行为t(≤10),表示数据组数。接下来t行,每行一个正整数n(≤1000)和数码a。

输出格式:

对于每组数据,输出一个整数,表示n!中a出现的次数。

输入输出样例

输入样例#1


2
5 2
7 0  

输出样例#1


1
2  

思路

重载:0分TLE

#include<string>
#include<iostream>
#include<iosfwd>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
#define MAX_L 10005 //最大长度,可以修改
using namespace std;
class bign//大整数类模版,用重载写的(因为我懒得打高精度了(逃)) 
{
public:
	int len, s[MAX_L];//数的长度,记录数组
//构造函数
	bign();
	bign(const char*);
	bign(int);
	bool sign;//符号 1正数 0负数
	string toStr() const;//转化为字符串,主要是便于输出
	friend istream& operator>>(istream &,bign &);//重载输入流
	friend ostream& operator<<(ostream &,bign &);//重载输出流
//重载复制
	bign operator=(const char*);
	bign operator=(int);
	bign operator=(const string);
//重载各种比较
	bool operator>(const bign &) const;
	bool operator>=(const bign &) const;
	bool operator<(const bign &) const;
	bool operator<=(const bign &) const;
	bool operator==(const bign &) const;
	bool operator!=(const bign &) const;
//重载四则运算
	bign operator+(const bign &) const;
	bign operator++();
	bign operator++(int);
	bign operator+=(const bign&);
	bign operator-(const bign &) const;
	bign operator--();
	bign operator--(int);
	bign operator-=(const bign&);
	bign operator*(const bign &)const;
	bign operator*(const int num)const;
	bign operator*=(const bign&);
	bign operator/(const bign&)const;
	bign operator/=(const bign&);
//四则运算的衍生运算
	bign operator%(const bign&)const;//取模(余数)
	bign factorial()const;//阶乘
	bign Sqrt()const;//整数开根(向下取整)
	bign pow(const bign&)const;//次方
//一些乱乱的函数
	void clean();
	~bign();
};
#define max(a,b) a>b ? a : b
#define min(a,b) a<b ? a : b
bign::bign()
{
	memset(s, 0, sizeof(s));
	len = 1;
	sign = 1;
}
bign::bign(const char *num)
{ 
	*this = num;
}
bign::bign(int num)
{
	*this = num;
}
string bign::toStr() const
{
	string res;
	res = "";
	for (int i = 0; i < len; i++)
		res = (char)(s[i] + '0') + res;
	if (res == "")
		res = "0";
	if (!sign&&res != "0")
		res = "-" + res;
	return res;
}
istream &operator>>(istream &in, bign &num)
{
	string str;
	in>>str;
	num=str;
	return in;
}
ostream &operator<<(ostream &out, bign &num)
{
	out<<num.toStr();
	return out;
}
bign bign::operator=(const char *num)
{
	memset(s, 0, sizeof(s));
	char a[MAX_L] = "";
	if (num[0] != '-')
		strcpy(a, num);
	else
		for (int i = 1; i < strlen(num); i++)
			a[i - 1] = num[i];
	sign = !(num[0] == '-');
	len = strlen(a);
	for (int i = 0; i < strlen(a); i++)
		s[i] = a[len - i - 1] - 48;
	return *this;
}
bign bign::operator=(int num)
{
	char temp[MAX_L];
	sprintf(temp, "%d", num);
	*this = temp;
	return *this;
}
bign bign::operator=(const string num)
{
	const char *tmp;
	tmp = num.c_str();
	*this = tmp;
	return *this;
}
bool bign::operator<(const bign &num) const
{
	if (sign^num.sign)
		return num.sign;
	if (len != num.len)
		return len < num.len;
	for (int i = len - 1; i >= 0; i--)
		if (s[i] != num.s[i])
			return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
	return !sign;
}
bool bign::operator>(const bign&num)const
{
	return num < *this;
}
bool bign::operator<=(const bign&num)const
{
	return !(*this>num);
}
bool bign::operator>=(const bign&num)const
{
	return !(*this<num);
}
bool bign::operator!=(const bign&num)const
{
	return *this > num || *this < num;
}
bool bign::operator==(const bign&num)const
{
	return !(num != *this);
}
bign bign::operator+(const bign &num) const
{
	if (sign^num.sign)
	{
		bign tmp = sign ? num : *this;
		tmp.sign = 1;
		return sign ? *this - tmp : num - tmp;
	}
	bign result;
	result.len = 0;
	int temp = 0;
	for (int i = 0; temp || i < (max(len, num.len)); i++)
	{
		int t = s[i] + num.s[i] + temp;
		result.s[result.len++] = t % 10;
		temp = t / 10;
	}
	result.sign = sign;
	return result;
}
bign bign::operator++()
{ 
	*this = *this + 1;
	return *this;
}
bign bign::operator++(int)
{
	bign old = *this;
	++(*this);
	return old;
}
bign bign::operator+=(const bign &num)
{
	*this = *this + num;		
	return *this;
}
bign bign::operator-(const bign &num) const
{
	bign b=num,a=*this;
	if (!num.sign && !sign)
	{
		b.sign=1;
		a.sign=1;
		return b-a;
	}
	if (!b.sign)
	{
		b.sign=1;
		return a+b;
	}
	if (!a.sign)
	{
		a.sign=1;			
		b=bign(0)-(a+b);
		return b;
	}
	if (a<b)
	{
		bign c=(b-a);
		c.sign=false;
		return c;
	}
	bign result;
	result.len = 0;
	for (int i = 0, g = 0; i < a.len; i++)
	{
		int x = a.s[i] - g;
		if (i < b.len) x -= b.s[i];
		if (x >= 0) g = 0;
		else
		{
			g = 1;
			x += 10;
		}
		result.s[result.len++] = x;
	}
	result.clean();
	return result;
}
bign bign::operator * (const bign &num)const
{
	bign result;
	result.len = len + num.len;
	for (int i = 0; i < len; i++)
    	for (int j = 0; j < num.len; j++)
			result.s[i + j] += s[i] * num.s[j];
	for (int i = 0; i < result.len; i++)
	{
		result.s[i + 1] += result.s[i] / 10;
		result.s[i] %= 10;
	}
	result.clean();
	result.sign = !(sign^num.sign);
	return result;
}
bign bign::operator*(const int num)const
{
	bign x = num;
	bign z = *this;
	return x*z;
}
bign bign::operator*=(const bign&num)
{
	*this = *this * num;
	return *this;
}
bign bign::operator /(const bign&num)const
{
	bign ans;
	ans.len = len - num.len + 1;
	if (ans.len < 0)
	{
		ans.len = 1;
		return ans;
	}
	bign divisor = *this, divid = num;
	divisor.sign = divid.sign = 1;
	int k = ans.len - 1;
	int j = len - 1;
	while (k >= 0)
	{
		while (divisor.s[j] == 0)
			j--;
		if (k > j) k = j;
		char z[MAX_L];
		memset(z, 0, sizeof(z));
		for(int i = j; i >= k; i--)
			z[j - i] = divisor.s[i] + '0';
		bign dividend = z;
		if (dividend < divid)
		{
			k--;
			continue;
		}
		int key = 0;
		while (divid*key<=dividend) key++;
		key--;
		ans.s[k] = key;
		bign temp = divid*key;
		for (int i = 0; i < k; i++)
			temp = temp * 10;
		divisor = divisor - temp;
		k--;
	}
	ans.clean();
	ans.sign = !(sign^num.sign);
	return ans;
}
bign bign::operator/=(const bign&num)
{
	*this = *this / num;
	return *this;
}
bign bign::operator%(const bign& num)const
{
	bign a = *this, b = num;
	a.sign = b.sign = 1;
	bign result, temp = a / b*b;
	result = a - temp;
	result.sign = sign;
	return result;
}
bign bign::pow(const bign& num)const
{
	bign result = 1;
	for (bign i = 0; i < num; i++)
		result = result*(*this);
	return result;
}
bign bign::factorial()const
{
	bign result = 1;
	for(bign i=1;i<=*this;i++)
		result *= i;
	return result;
}
void bign::clean()
{
	if (len == 0) len++;
	while (len > 1 && s[len - 1] == '\0')
		len--;
}
bign bign::Sqrt()const
{
	if(*this<0)return -1;
	if(*this<=1)return *this;
	bign l=0,r=*this,mid;
	while(r-l>1)
	{
		mid=(l+r)/2;
		if(mid*mid>*this)
			r=mid;
		else
			l=mid;
	}
	return l;
}
bign::~bign()
{
}
bign n,m,t,i,j,k,s(1),res,ans;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>t;
	for(k=1;k<=t;k++)
	{
		cin>>n>>m;
		for(i=1;i<=n;i++)
		{
			s=s*i;
		}
		while(s!=0)
		{
			res=s%10;
			if(res==m)
			{
				ans++;
			}
			s=s/10;
		}
		cout<<ans<<endl;
		s=1;
		ans=0;
	}
	return 0;
}

普通做法:AC

#include <stdio.h>
#include <iostream>
#include <string.h>
int a[20001],n,m,t,s;//n!,m是数码,t是有t位,s存答案,a数组是一串高精度数 
using namespace std;
inline int multe(int n)
{
	register int i,j;
	for(i=1;i<=t;i++)//先让a数组的每一位都能乘以n 
	{
		a[i]*=n;
	}
	for(i=1;i<=t;i++)//从1到位数进行循环 
	{
		if(a[i]>9)//如果需要进位 
		{
			a[i+1]+=a[i]/10;//进位 
			a[i]%=10;
			if(i+1>t)//如果现在是最高位 
			{
				t++;//位+1 
			}
		}
	}
	return 0;
}
inline void lxydl()//lxy大佬太强啦! 
{
	for(register int i=t;i>=1;i--)//这个倒或正可能无所谓吧,但是高精度后出来的数字都是倒序的,那我们也倒叙 
	{
		if(a[i]==m)//如果当前的a[i]等于数 
		{
			s++;//算符+1 
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	register int i,j,l;//l是l组数据 
	cin>>l;
	while(l--)
	{
		cin>>n>>m;
		s=0;//答案初始化 
		memset(a,0,sizeof(a));//清除缓存 
		a[1]=1;//假设第一位数为1,否则0乘任何数都是0 
		t=1;//至少有1位 
		for(i=1;i<=n;i++)//求n! 
		{
			multe(i);//a数组乘以i 
		}
		lxydl();//找n!里有多少m这个数 
		cout<<s<<endl;//输出答案 
	}
	return 0;
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[洛谷]P1591 阶乘数码 (#高精度 -1.2) 的相关文章

  • 插入内核模块失败提示"Invalid module format"

    产品需要编译自己的定制内核 43 内核模块 xff0c 下载内核源码定制修改后rpmbuild方式 点击打开链接 编译升级内核 xff0c 如下方式编译内核模块 make C kernel source SUBDIRS 61 96 pwd
  • microsoft visual c++ build tools

    因为visual studio的安装包太大 xff0c 所以在不需要开发的情况下 xff0c 可以选择使用microsoft visual c 43 43 build tools安装c 43 43 编译器 xff0c 这个工具会小很多 安装
  • C++ 应用程序 内存结构 --- BSS段,数据段,代码段,堆内存和栈

    转自 xff1a http hi baidu com C6 BF D6 D0 B5 C4 C5 AE CE D7 blog item 5043d08e741075f3503d922c html ld 时把所有的目标文件的代码段组合成一个代码
  • 4.1 简单题 - B 恭喜你

    当别人告诉你自己考了 x 分的时候 xff0c 你要回答说 xff1a 恭喜你考了 x 分 xff01 比如小明告诉你他考了90分 xff0c 你就用汉语拼音打出来 gong xi ni kao le 90 fen 输入格式 xff1a 输
  • <script>在页面代码上没有显示

    记录一下 导入js文件 xff0c 自己路径都没有问题 xff0c 为什么在浏览器查看页面代码没有自己写的那行js导入文件的代码呢 xff0c 原来 xff0c 是之前看着不舒服 xff0c 点了exclude xff0c exclude是
  • 利用Rust构建一个REST API服务

    利用Rust构建一个REST API服务 关注公众号 xff1a 香菜粉丝 了解更多精彩内容 Rust 是一个拥有很多忠实粉丝的编程语言 xff0c 还是很难找到一些用它构建的项目 xff0c 而且掌握起来甚至有点难度 想要开始学习一门编程
  • 安装cmake3.22

    升级cmake版本 脚本 span class token assign left variable file name span span class token operator 61 span cmake 3 22 0 yum era
  • stdout stderr 重定向到文件

    1 stdout stderr 重定向 1 stdout stderr 重定向 1 1 dup dup2 重定向到已打开文件 或 新文件1 2 freopen 重定向到新文件1 3 命令行重定向1 4 参考资料 1 1 dup dup2 重
  • 逆向基础-Windows驱动开发(一)

    Windows内核开发 第一个驱动程序 环境配置 xff1a 安装WDK xff1a WDK版本与SDK保持一致 然后记得把Spectre Mitigation给Disabled掉 xff0c 就不用去下载漏洞补丁了 然后在内核层 xff0
  • json-c 理解记录

    1 json c 理解记录 1 json c 理解记录 1 1 编译及说明1 2 特色1 3 使用 1 3 1 创建 xff0c 读写文件1 3 2 拷贝1 3 3 增改 1 3 3 1 字典增加元素1 3 3 2 数组增加修改元素 1 3
  • valgrind 简介(内存检查工具)

    1 valgrind 简介 1 valgrind 简介 1 1 概图1 2 特点1 3 使用示例1 4 参数说明 1 4 1 常用参数1 4 2 展示1 4 3 子进程 动态加载库及记录时机1 4 4 查错内存优化1 4 5 其他不常用1

随机推荐

  • GObject学习教程---第一章:GObject是有用并且简单的

    索引 xff1a https blog csdn net knowledgebao article details 84633798 本文是学习学习他人的博客的心得 xff08 具体详见 楼主见解 xff09 xff0c 如果源网站可访问的
  • GObject学习教程---第二章:模拟类的数据封装形式

    索引 xff1a https blog csdn net knowledgebao article details 84633798 本文是学习学习他人的博客的心得 xff08 具体详见 楼主见解 xff09 xff0c 如果源网站可访问的
  • 音视频中的PTS和DTS及同步

    相关索引 xff1a https blog csdn net knowledgebao article details 84776869 视频的播放过程可以简单理解为一帧一帧的画面按照时间顺序呈现出来的过程 xff0c 就像在一个本子的每一
  • h264和h265的区别

    相关索引 xff1a https blog csdn net knowledgebao article details 84776869 目录 1 H 264与H 265的主要差异 2 xff0c 压缩性能比较 3 各模块技术差异汇总 4
  • StreamEye使用说明

    编译相关索引 xff1a https blog csdn net knowledgebao article details 84973055 官网 xff1a https www elecard com products video ana
  • libc.so库简介

    相关链接 xff1a https blog csdn net knowledgebao article details 84315842 问题一 xff1a 比如不小心把软连接libc so 6删除了 xff0c 只要执行ldconfig
  • CentOS 7.6安装OpenStack Stein版本

    文章目录 一 前提1 1设置四节点1 2网络平台架构1 3准备环境 所有节点 1 3 1设置hosts1 3 2设置主机名1 3 3关闭 firewalld1 3 4关闭SELinux1 3 5设置静态IP1 3 6自定义yum源1 3 7
  • SQLite使用

    1 创建表 xff0c 以及更新表结构 public class MySQLiteOpenHelper extends SQLiteOpenHelper public MySQLiteOpenHelper Context context s
  • 数据结构实验之栈与队列八:栈的基本操作

    Problem Description 堆栈是一种基本的数据结构 堆栈具有两种基本操作方式 xff0c push 和 pop push一个值会将其压入栈顶 xff0c 而 pop 则会将栈顶的值弹出 现在我们就来验证一下堆栈的使用 Inpu
  • 解决SpringSecurity阻止ajax的POST和PUT请求,导致403Forbidden的问题

    解决SpringSecurity阻止ajax的POST和PUT请求 xff0c 导致403Forbidden的问题 参考文章 xff1a xff08 1 xff09 解决SpringSecurity阻止ajax的POST和PUT请求 xff
  • Input.GetTouch 获取触摸

    Input GetTouch 获取触摸 static function GetTouch index int Touch Description 描述 Returns object representing status of a spec
  • Python-Pandas(1)数据读取与显示,数据样本行列选取

    span class hljs keyword import span pandas food info 61 pandas read csv span class hljs string 34 food info csv 34 span
  • 回归模型-线性回归算法

    线性回归算法 问题分为有监督问题和无监督问题两类 当用到标签来划分的时候就是有监督问题 xff0c 当没有用标签值的时候就是无监督问题 线性回归求解的结果是值 比如 xff1a 根据工资和年龄来预测出一个具体的值 xff0c 根据工资和年龄
  • 时间序列(三)滑动窗口

    滑动窗口就是能够根据指定的单位长度来框住时间序列 xff0c 从而计算框内的统计指标 相当于一个长度指定的滑块在刻度尺上面滑动 xff0c 每滑动一个单位即可反馈滑块内的数据 span class hljs import span clas
  • 时间序列(四)ARIMA模型与差分

    ARIMA模型 平稳性 xff1a 平稳性就是要求经由样本时间序列所得到的拟合曲线 在未来的一段期间内仍能顺着现有的形态 惯性 地延续下去 平稳性要求序列的均值和方差不发生明显变化 严平稳与弱平稳 xff1a 严平稳 xff1a 严平稳表示
  • 时间序列(五)股票分析

    首先导入相关模块 span class hljs keyword import span pandas span class hljs keyword as span pd span class hljs keyword import sp
  • Windows下Node多版本管理

    Windows下Node多版本管理 相关指令背景介绍GNVM 相关指令 查看node版本 node v 查看npm版本 span class token function npm span v 查看gnvm版本 gnvm version g
  • Notepad++与NodeJS

    文章目录 背景介绍理论分析实践操作结果展示 背景介绍 前段时间 xff0c 弄了张流量卡 xff0c 感觉线上查询太费劲了 不妙 xff0c 干脆通过NodeJS写个小软件来 xff0c 实时查询 程序员 xff0c 就是要方便自己嘛 哎
  • 傻瓜式Linpack安装(Mpich+Openblas+Hpl)

    Linpack安装 安装信息安装Mpich安装Openblas安装Hpl 参考资料 Linpack安装 安装信息 安装平台是Ubuntu16 04使用的是Mpich 43 Openblas 43 HplCPU架构为 Intel Nehale
  • [洛谷]P1591 阶乘数码 (#高精度 -1.2)

    题目描述 求n 中某个数码出现的次数 输入输出格式 输入格式 xff1a 第一行为t 10 xff0c 表示数据组数 接下来t行 xff0c 每行一个正整数n 1000 和数码a 输出格式 xff1a 对于每组数据 xff0c 输出一个整数