• 数学 (一个正整数分解成多个连续正整数的和) + 整数每次减少一位加和



    一.数学 (一个正整数分解成多个连续正整数的和)

    题目描述
    定义:f(x)为x分解为连续正整数(大于一个)的和的方案数。
    例如:
    ·6=1+2+3,所以f(6)=1 。
    ·15=1+2+3+4+5=4+5+6=7+8,所以f(15)=3 。
    现在输入一个正整数n,请求出f(n)。
    输入
    一行一个正整数,表示n。n<=10e12
    输出
    一行一个数,表示f(n)。
    样例输入 Copy

    15
    
    • 1

    样例输出 Copy

    3
    
    • 1

    1.假设一段长度为n的连续正整数和为s,并且第一个正整数为a,那么s=n*(a+a+n-1)/2=n*(2a+n-1)/2
    则2
    s=2an+nn-n
    则a=(2
    s-nn+n)/(2n)
    于是本题已知和s,就可以枚举长度n,如果存在合法的正整数a,那么就是一种答案
    2.从1到n的和为n*(n-1)/2,因此和为s时,连续正整数位数不会超过sqrt(s)*2

    #include
    #include
    using namespace std;
    typedef long long ll;
    
    int main()
    {
    	ll s;cin>>s;
    	
    	int ans=0;
    	for(ll n=2;n<=sqrt((long double)s)*2;n++) //枚举连续正整数的位数 
    	{
    		ll sum=2*s-n*n+n;
    		if(sum%(2*n)==0)
    		{
    			ll k=sum/(2*n);
    			if(k>0) ans++;
    		}
    	}
    	cout<<ans;
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    二. Σ[k=0…10100]floor(X/10k)

    题目描述
    Find

    Notes
    ⌊A⌋ denotes the value of A truncated to an integer.

    Constraints
    X is an integer.
    1≤X<500000
    输入
    Input is given from Standard Input in the following format:

    X
    输出
    Print the answer as an integer.
    Here, the answer must be precisely printed as an integer, even if it is large. It is not allowed to use exponential notation, such as 2.33e+21, or print unnecessary leading zeros, as in 0523.
    样例输入 Copy
    【样例1】

    1225
    
    • 1

    【样例2】

    99999
    
    • 1

    【样例3】

    314159265358979323846264338327950288419716939937510
    
    • 1

    样例输出 Copy
    【样例1】

    1360
    
    • 1

    【样例2】

    111105
    
    • 1

    【样例3】

    349065850398865915384738153697722542688574377708317
    
    • 1

    提示
    样例1解释
    The value we seek is 1225+122+12+1+0+0+⋯+0=1360.
    样例2解释
    Beware of carries.
    样例3解释
    The values in input and output can both be enormous.

    题目大意:给定一个很大的正整数x,每次减少x的最低位,求所有数的加和。
    找规律发现,假设x有n位数字:
    个位是由x前n位数字之和决定的
    十位是由前n-1位数字之和 以及 进位决定的
    1.求答案的每一位数字上的和(可能大于10,大于10就需要进位)
    2.将最终结果表示出来


    #include
    #include
    #include
    #include
    using namespace std;
    const int N=5e5+10;
    char a[N];
    int s[N];
    
    int main()
    {
    	scanf("%s",a+1);
    	int n=strlen(a+1);
    	
    	for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i]-'0';//前缀和求答案的每一位上的数 
    	
    	reverse(s+1,s+1+n); //从个位开始加 
    	vector<int> ans;
    	int sum=0;
    	for(int i=1;i<=n||sum;i++)
    	{
    		if(i<=n)sum+=s[i];
    		ans.push_back(sum%10);
    		sum/=10; 
    	}
    	
    	for(int i=ans.size() -1;i>=0;i--) printf("%d",ans[i]);
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    基于Qt 多线程(继承 QObject 的线程)
    如何成为一名优秀的老师?
    LeetCode 59. 螺旋矩阵 II
    webpack实践:解决组件库的静态资源在项目上加载不了的问题!
    Ubuntu18.04 在Anaconda中配置YOLOv3-Pytorch-GPU环境配置
    如何系统地去学python
    【RocketMq 系列】RocketMq 消息重试机制、死信队列
    NJUPT网络管理理论与实践期末考试2021.11.10
    云原生时代顶流消息中间件Apache Pulsar部署实操之轻量级计算框架
    查准率(precision,也叫精确率)和查全率(recall,也叫召回率)
  • 原文地址:https://blog.csdn.net/m0_63363129/article/details/126056333