题目描述
定义: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
样例输出 Copy
3
1.假设一段长度为n的连续正整数和为s,并且第一个正整数为a,那么s=n*(a+a+n-1)/2=n*(2a+n-1)/2
则2s=2an+nn-n
则a=(2s-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;
}
题目描述
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
【样例2】
99999
【样例3】
314159265358979323846264338327950288419716939937510
样例输出 Copy
【样例1】
1360
【样例2】
111105
【样例3】
349065850398865915384738153697722542688574377708317
提示
样例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;
}