题目来源:Light OJ 1278 Sum of Consecutive Integers
题意:N拆分成连续整数和的方案数
思路:奇因数的个数
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; //筛素数 const int maxn = 10000010; bool vis[maxn]; int prime[1000000]; int sieve(int n) { memset(vis, 0, sizeof(vis)); vis[0] = vis[1] = 1; int c = 0; for(int i = 2; i <= n; i++) if(!vis[i]) { prime[c++] = i; for(int j = 2*i; j <= n; j += i) vis[j] = 1; } return c; } int main() { int c = sieve(10000000); int cas = 1; int T; scanf("%d", &T); while(T--) { long long n, ans = 1; scanf("%lld", &n); while(n%2 == 0) n /= 2; for(int i = 0; i < c && (long long)prime[i]*prime[i] <= n; i++) { if(prime[i] > n) break; if(n % prime[i] == 0) { long long sum = 1; while(n % prime[i] == 0) { sum++; n /= prime[i]; } ans *= sum; } } if(n > 1 && (n&1)) ans *= 2; printf("Case %d: %lld\n", cas++, ans-1); } return 0; }
Light OJ 1278 Sum of Consecutive Integers N拆分成连续整数和
时间: 2024-10-09 03:48:07