1 solutions
-
0
C++ :
#include <iostream> #include<cstdio> #include<cmath> using namespace std; int cnt=0,a[1000005],f[1000005]; //预处理,缩小搜索范围,可以大大节约时间 void chuli(int x) { for(int i=2;i<=sqrt(x);i++) //枚举出每个因子(包括完全平方数) { if(x%i==0) { a[++cnt]=i; } } int j=cnt; cnt*=2; if(a[j]*a[j]==x) cnt--; //处理完全平方数 for(int i=1;i<=j;i++)//打印下一半因子 { a[cnt-i+1]=x/a[i]; } a[++cnt]=x; } int dfs(int k) { if(f[k]>0) //边界 { return f[k]; } int t; for(int i=k;i>=1;i--) { if(a[k]%a[i]==0) //搜索因子的因子的数量并累加 { if(a[k]==a[i]) { f[k]++; } else { t=a[k]/a[i];//如果没搜完,就继续搜 int j=k-1; while(a[j]!=t) { j--; } f[k]+=dfs(j); } } } return f[k]; } int main() { int n; scanf("%d",&n); chuli(n);//先预处理,然后算出拆分的总方法数 int ans=dfs(cnt); printf("%d",ans); return 0; }Pascal :
program p0013; var i,j,s,n,total:longint; f:array[-5000..5000] of longint; procedure solve(k:longint); var i,j,t:longint; begin if k=0 then inc(total) else for i:=k downto 1 do if f[k] mod f[i]=0 then if f[k]=f[i] then inc(total) else begin t:=f[k] div f[i]; j:=k-1; while t<>f[j] do dec(j); solve(j); end; end; begin read(n); s:=0; i:=2; while i*i<=n do begin if n mod i=0 then begin inc(s); f[s]:=i; f[-s]:=n div i; end; i:=i+1; end; if (i-1)*(i-1)<>n then j:=-s else j:=-s+1; for i:=j to -1 do begin inc(s); f[s]:=f[i]; end; inc(s); f[s]:=n; total:=0; solve(s); writeln(total); end.Java :
import java.util.Scanner; public class Main { static int cnt = 0; static int[] a=new int[1000005]; static int[] f=new int[1000005]; static void chuli(int x) { for (int i = 2; i <= Math.sqrt(x); i++) //枚举出每个因子(包括完全平方数) { if (x % i == 0) { a[++cnt] = i; } } int j = cnt; cnt *= 2; if (a[j] * a[j] == x) cnt--; //处理完全平方数 for (int i = 1; i <= j; i++)//打印下一半因子 { a[cnt - i + 1] = x / a[i]; } a[++cnt] = x; } static int dfs(int k) { if (f[k] > 0) //边界 { return f[k]; } int t; for (int i = k; i >= 1; i--) { if (a[k] % a[i] == 0) //搜索因子的因子的数量并累加 { if (a[k] == a[i]) { f[k]++; } else { t = a[k] / a[i];//如果没搜完,就继续搜 int j = k - 1; while (a[j] != t) { j--; } f[k] += dfs(j); } } } return f[k]; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n=in.nextInt(); chuli(n);//先预处理,然后算出拆分的总方法数 int ans = dfs(cnt); System.out.println(ans); } }
- 1
Information
- ID
- 17213
- Time
- 4000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By