打表可以简单理解为把需要算的结果先都算出来存进数组里
多次需要调用结果就可以直接从表里找
Problem G:
Count beans will be crazy
Time Limit: 1 Sec Memory Limit: 128 MB
Description
Mother bought a basket of beans before winter came. Cleaning the beans, she
fou
nd a bean left when she counted them two by two; there're two beans left when
she counted them three by three; there're three beans left when she counted them
four by four; there're four beans left when she counted them five by five...and
there're n
-
1 bean
s left when she counted them n by n. Now do you know how many
beans there are in the basket at least?
Input
Input n (2<=n<=40 ).
Process to end of file.
Output
Output how many beans there are in the basket at least?
Sample Input
6
Sample Output
59
#include<stdio.h>
#include<iostream>
using namespace std;
//long long func1(long long a,long long b);
int main()
{
long long ans[45]={0,0,2,6,12,60,60,420,840,2520,2520,27720,27720,360360,360360,360360,720720,12252240,12252240,232792560,232792560,232792560,232792560,5354228880,5354228880,26771144400,26771144400,80313433200,80313433200,2329089562800,2329089562800,72201776446800,144403552893600,144403552893600,144403552893600,144403552893600,144403552893600,5342931457063200,5342931457063200,5342931457063200,5342931457063200};
/*ans[2]=2;
for(int i=3;i<=40;i++)
{
ans[i]=ans[i-1]*(i/func1(i,ans[i-1]));
cout<<ans[i]<<',';
}*/
int n;
cin>>n;
cout<<ans[n]-1<<endl;
//printf("%lld",ans[n]-1);
return 0;
}
/*long long func1(long long a,long long b)
{
while(1)
{
if(a<b)
{
long long t=a;
a=b;
b=t;
}
if(a%b==0)
return b;
a=a%b;
}
}*/
题意:n个n个拿豆子 最后剩n-1个 求最少有多少豆子
找最大公约数然后求最小公倍数在-1
注意多组输入
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
long long ans[45]={0,0,2,6,12,60,60,420,840,2520,2520,27720,27720,360360,360360,360360,720720,12252240,12252240,232792560,232792560,232792560,232792560,5354228880,5354228880,26771144400,26771144400,80313433200,80313433200,2329089562800,2329089562800,72201776446800,144403552893600,144403552893600,144403552893600,144403552893600,144403552893600,5342931457063200,5342931457063200,5342931457063200,5342931457063200};
int n;
while(~scanf("%d",&n))
printf("%I64d",ans[n]-1);
return 0;
}