csu 1416 Practical Number(数论,结论题)

来源:互联网 发布:别墅网络覆盖方案 编辑:程序博客网 时间:2024/06/13 16:17

Practical Number

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 182  Solved: 48
[Submit][Status][Web Board]

Description

In number theory, a practical number is a positive integer n such that all smaller positive integers can be represented as sums of distinct divisors of n.
For example, 12 is a practical number because all the numbers from 1 to 11 can be expressed as sums of its divisors 1, 2, 3, 4, and 6: as well as these divisors themselves, we have 5 = 3 + 2, 7 = 6 + 1, 8 = 6 + 2, 9 = 6 + 3, 10 = 6 + 3 + 1, and 11 = 6 + 3 + 2. The first few practical numbers are: 1, 2, 4, 6, 8, 12, 16, 18, 20, 24, 28, 30, 32, 36, 40, 42, 48, 54, 56, 60.
Given a positive integer ntest if it is a practical number.

Input

The first line contains the number of test cases T (1  T  200).
For each test case, there is only one line with an integer n (1 ≤ n ≤ 1018) as defined above.

Output

For each test case, output “Yes (without quotation marks) in one line if n is a practical number. Otherwise, output No (without quotation marks) instead.

Sample Input

1012345678910

Sample Output

YesYesNoYesNoYesNoYesNoNo
题意:给你一个n,问`1~n-1能否由n的约数相加得到

思路:想了好久,最后百度了,挺无语的,居然是个结论题。

这现场赛出这种题目让人怎么解得出来啊~

关于结论可以看http://planetmath.org/PracticalNumber

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 100008int prime[N],cnt,vis[N];void init(){    cnt=0;    memset(vis,0,sizeof(vis));    for(int i=2;i<N;i++)    {        if(vis[i]) continue;        prime[cnt++]=i;        for(int j=i+i;j<N;j+=i)            vis[j]=1;    }}int solve(long long n){    if(n==1) return 1;    if(n&1) return 0;    long long now=1;    for(int i=0;i<cnt;i++)    {        if(n%prime[i]==0)        {            if(prime[i]>now+1) return 0;            long long temp=1,sum=1;            while(n%prime[i]==0)            {                n/=prime[i];                temp*=prime[i];                sum+=temp;            }            now*=sum;        }    }    if(n>now+1) return 0;    return 1;}int main(){    int T;    long long n;    init();    scanf("%d",&T);    while(T--)    {        scanf("%lld",&n);        if(solve(n)) printf("Yes\n");        else printf("No\n");    }    return 0;}





0 0
原创粉丝点击