HDU 2601An Easy problem-Prime number of use, brute force solution

Source: Internet
Author: User
An easy problem
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %i64d &%i64u

Submit Status

Description when Teddy is a child, he is always thinking on some simple math problems, such as "what it ' s 1 cup of W Ater plus 1 pile of dough.., "Yuan buy pig". etc..

One day Teddy met a-a-man-in-his-dream, in-Dream the man whose name is "Rulai" gave Teddy a problem:

Given an N, can-calculate how many ways to write N as I * j + i + j (0 < I <= j)?

Teddy found the answer when N is less than 10...but if n get bigger, he found it is too difficult for him to solve.
Well, are you clever acmers, could do I little Teddy to solve the problem and let him has a good dream?

Input The first line contain a T (T <= 2000). followed by T-lines, each line contain an integer N (0<=n <= 10 10).

Output for each case, output the number of ways on one line.

Sample Input

2 1 3

Sample Output

0 1 for N as I * j + i + j (0 < I <= j)?
can be represented as N=i*j+i+j
So can be turned into n+1= (i+1) * (j+1);
So there are two ways to do, a violent enumeration, from here can be seen i<=sqrt (n+1);
So a loop can be solved, the first code is, but time-consuming only a little over the topic provided 3s, how to do, whether there
Better solution, yes, n+1= (i+1) * (j+1) can know n+1 is a multiple of i+1 and j+1
So you can convert to the number of approximate numbers (what is the approximate, please the reader himself Baidu understand)
N+1=a1^p1*a2^p2*a3^p3....an^pn
Where AI represents prime numbers, this means that any number greater than 1 can be converted to the product of a finite prime factor
So, you can use the permutation combination to seek, the first kind has p1+1 choice (can choose 0...P1) The second kind has the p2+1 choice (can choose 0...P2) .... The nth type has pn+1 choice (can choose 0...PN)
So about several numbers ans= (p1+1) * (p2+1) * (p3+1) * (p4+1) *....* (pn+1) (there are also minus 1 and n because they do not belong to the topic requirement range)
When N+1 is a total square number, then except for 1 and n+1 itself, only the most intermediate approximations are counted only once, and the other numbers are counted two times,
When N+1 is not a total square number, the other numbers are counted twice, except for 1 and n+1 itself.
So you can determine the output separately, you can also directly convert the output as in code, (ans+1)/2-1, when the total square number, we need to add a second to make the correct result
As for minus one, that is, the previous two non-conforming numbers minus 1 and N
When the number of squares is not complete, ans/2-1 is possible, but in order to synthesize a formula, (ans+1)/2-1, it is possible to replace the ans/2-1
Why, because (4+1)/2==4/2, this will not affect the end result.

/*
author:2486
memory:1416 KB		time:2823 MS
language:g++		result:accepted */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace Std;
typedef long long LL;
const int MAXN=1E5;
int t;
LL N;
int main () {
    scanf ("%d", &t);
    while (t--) {
        scanf ("%i64d", &n);
        if (n==0| | n==1) {
            printf ("0\n");
            Continue;
        }
        int cnt=0;
        for (int i=1; i<=sqrt (n); i++) {
            if (n+1)% (i+1) ==0&& (n+1)/(i+1) >=i+1) cnt++;
        }
        printf ("%d\n", CNT);
    }
}

/*
author:2486
memory:1592 KB		time:46 MS
language:g++		result:accepted */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
LL PRIME[MAXN];
BOOL VIS[MAXN];
int t,cnt;
LL N;
void primes () {//Initialize prime number list
    cnt=0;
    for (int i=2; i<maxn; i++) {
        if (vis[i]) continue;
        prime[cnt++]=i;
        for (int j=i*2; j<maxn; j+=i) {
            vis[j]=true;
        }
    }
}
void Solve (ll N) {
    ll ans=1;
    for (int i=0; prime[i]*prime[i]<=n; i++) {
        if (n%prime[i]==0) {
            int s=0;
            while (n%prime[i]==0) n/=prime[i],s++;
            Ans*= (s+1);
        }
        if (n==1) break;
    }
    if (n>1) ans*=2;
    printf ("%i64d\n", (ans+1)/2-1);
}
int main () {
    primes ();
    scanf ("%d", &t);
    while (t--) {
        scanf ("%i64d", &n);
        n++;
        Solve (N);
    }
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.