连续的数(n>2)相加的和等于m,求这个连续的数模式的种数
样例:
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.Each case starts with a line containing an integer N (1 ≤ N ≤ 1014).
Output
For each case, print the case number and the number of ways to express N as sum of consecutive integers.
Sample Input
5
10
15
12
36
828495
Sample Output
Case 1: 1
Case 2: 3
Case 3: 1
Case 4: 2
Case 5: 47
题解:
(a+b)(b-a+1)/2==m
a+b与(b-a+1)必定有一奇一偶
m除2^k的数m2====变成只有两个奇数a,b
再让2^k分配给一对(a,b)中的任意一个。(a,b就是m2的质因子配对)
配对:(质因子数)/2
2^k任意组合到一队中:*2 ===>(结果还是质因子数)
题面要求必须是两个连续的数字:-1(排除1和本身这一对)
ans=m2的因子数-1
求解质因子数:
1e14
打质数表1e7(打到一半因子数k即可)
如果剩余的m不是1:说明还有大于一半的质因子M,于是之前的因子都含有配对的M(1-k)。因此m2的因子数需要k*2。
否则:m2的因子数就是k
唯一分解定理
n=p1^a1*p2^a2.... (p1,p2....为素数)
n的因子个数=(a1+1)*(a2+1)*.....
代码:
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn=1e7+5;
long long n,m;
bool notprime[maxn];
vector<int>prime;
void get_prime(){
notprime[0]=true;
notprime[1]=true;
for(int i=2;i<maxn;i++){
if(!notprime[i])prime.push_back(i);
for(int j=0;j<prime.size()&&i*prime[j]<=maxn;j++){
notprime[i*prime[j]]=true;
if(!(i%prime[j]))break;
}
}
}
int main(){
cin>>n;
get_prime();
for(int k=1;k<=n;k++){
cin>>m;
while(m%2==0)m/=2;
int res=1;
long long q=m;
for(int i=0;i<prime.size()&&1LL*prime[i]*prime[i]<=m;i++){
int p=prime[i];
if(q%p!=0)continue;
int sum=1;
while((q%p)==0){
sum++;
q/=p;
}
res*=sum;//数因子分解定理
if(q==1)break;
}
if(q!=1)res*=2;//唯一的大数质数因子
printf("Case %d: %d\n",k,res-1);//找规律
}
return 0;
}