Happy 2004&&http://acm.hdu.edu.cn/showproblem.php?pid=1452

本文介绍了一种计算特定整数的因子之和并求模的方法。通过两种不同的算法实现,探讨了如何有效处理大规模数值计算问题,特别是在面对有限制的数据类型时的解决方案。
Total Submission(s): 609 Accepted Submission(s): 431


Problem Description
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).

Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29 is equal to 6.

Input
The input consists of several test cases. Each test case contains a line with the integer X (1 <= X <= 10000000).

A test case of X = 0 indicates the end of input, and should not be processed.

Output
For each test case, in a separate line, please output the result of S modulo 29.

Sample Input
1 10000 0

Sample Output
6 10
题意:关于求一个数所有因子之和的问题,一种是等比数列求和,一种是乘法逆元。对于这一题我只能说我SB了,明知道hdu不支持long long类型(如果使用就会超时),却视而不见,导致对自己的算法产生了怀疑,疯狂调试了近两个小时,还是木有找出错误,最后蓦然发现我却用了long long 类型,着实伤不起啊!!有木有,,,,
法一:
#include<iostream>
#include<string.h>
#include<string>
#define N  29
#include<cstdio>
using namespace std;
int a[][3]={2,3,167,2,1,1};
typedef __int64 L;
L pow(L a,L b)
{
    L ans=1;
    while(b)
    {
        if(1&b) ans=(ans*a)%N;
            a=(a%N*a%N)%N;
            b=b>>1;
    }
    return ans;
}
L _pow(L a,L b)
{
    if(a==0) return  0;
    else if(a==1||b==0) return 1;
    else {
         if(1&b) return (_pow(a,b/2)%N)*((1+pow(a,b/2+1))%N);
         else   return ((_pow(a,b/2-1)%N)*((1+pow(a,b/2+1))%N)+pow(a,b/2))%N;
    }
}
int main()
{
    L x;
    while(~scanf("%I64d",&x),x)
    {
        L ans=1;
        for(int i=0;i!=3;++i)
            ans=(ans%N*(_pow(a[0][i],a[1][i]*x)%N))%N;
        printf("%I64d\n",ans);
    }return 0;
}

法二:
*#include<cstdio>
#include<string.h>
#include<iostream>
#define N 29
using namespace std;
typedef __int64  L;
L _pow(L a,L b)
{
	L ans=1;
	while(b)
	{
		if(1&b) ans=(ans*a)%N;
		    a=(a%N*a%N)%N;
			b=b>>1;
	}
	return ans;
}
int main()
{
	L x;
	while(~scanf("%I64d",&x),x)
	{
		L a=_pow(2,(x*2+1));
		L b=_pow(3,(x+1));
		L c=_pow(22,(x+1));
		L d=((a-1)*(b-1)*15*(c-1)*18)%N;
		printf("%I64d\n",d);
	}return 0;
(a+k*29)^29%29=a%29;


HDU 6259 是一道与回文子串相关的编程题目,要求统计特定条件下回文子串的数量。题目通常涉及字符串操作、动态规划或 Manacher 算法等技术。 ### 解题思路 题目核心在于识别并统计满足特定条件的回文子串。通常的解题方法包括: - **暴力枚举**:适用于小规模输入,时间复杂度为 $ O(n^2) $。 - **动态规划**:使用二维数组 `dp[i][j]` 表示从索引 `i` 到 `j` 的子串是否为回文。 - **Manacher 算法**:线性时间复杂度 $ O(n) $ 的高效算法,适用于大规模输入。 ### 示例代码 以下是一个使用动态规划方法统计所有回文子串的示例代码: ```cpp #include <iostream> #include <vector> #include <string> using namespace std; int countPalindromicSubstrings(string s) { int n = s.size(); vector<vector<bool>> dp(n, vector<bool>(n, false)); int count = 0; // 单个字符的回文 for (int i = 0; i < n; ++i) { dp[i][i] = true; ++count; } // 两个字符的回文 for (int i = 0; i < n - 1; ++i) { if (s[i] == s[i + 1]) { dp[i][i + 1] = true; ++count; } } // 更长的回文子串 for (int length = 3; length <= n; ++length) { for (int i = 0; i <= n - length; ++i) { int j = i + length - 1; if (s[i] == s[j] && dp[i + 1][j - 1]) { dp[i][j] = true; ++count; } } } return count; } int main() { string s; cin >> s; cout << countPalindromicSubstrings(s) << endl; return 0; } ``` ### 时间与空间复杂度分析 - **动态规划**:时间复杂度为 $ O(n^2) $,空间复杂度为 $ O(n^2) $。 - **Manacher 算法**:时间复杂度为 $ O(n) $,空间复杂度为 $ O(n) $。 ### 优化建议 对于大规模字符串(如长度超过 $ 10^5 $),应优先使用 Manacher 算法以提升效率。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值