pku1309 数学优化+枚举

题目
Coconuts, Revisited
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 1832
Accepted: 737

Description

The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening Post on October 9, 1926. The story tells about five men and a monkey who were shipwrecked on an island. They spent the first night gathering coconuts. During the night, one man woke up and decided to take his share of the coconuts. He divided them into five piles. One coconut was left over so he gave it to the monkey, then hid his share and went back to sheep.

Soon a second man woke up and did the same thing. After dividing the coconuts into five piles, one coconut was left over which he gave to the monkey. He then hid his share and went back to bed. The third, fourth, and fifth man followed exactly the same procedure. The next morning, after they all woke up, they divided the remaining coconuts into five equal shares. This time no coconuts were left over.

An obvious question is "how many coconuts did they originally gather?" There are an infinite number of answers, but the lowest of these is 3,121. But that's not our problem here.

Suppose we turn the problem around. If we know the number of coconuts that were gathered, what is the maximum number of persons (and one monkey) that could have been shipwrecked if the same procedure could occur?

Input

The input will consist of a sequence of integers, each representing the number of coconuts gathered by a group of persons (and a monkey) that were shipwrecked. The sequence will be followed by a negative number.

Output

For each number of coconuts, determine the largest number of persons who could have participated in the procedure described above. Display the results similar to the manner shown below, in the Expected Output. There may be no solution for some of the input cases; if so, state that observation.

Sample Input

25 30 3121 -1

Sample Output

25 coconuts, 3 people and 1 monkey 30 coconuts, no solution 3121 coconuts, 5 people and 1 monkey

Source


解法:
首先写出递推公式
f(0)=A  A=nk
f(i)=f(i-1)/(n-1)*n+1

随便什么方法写出闭形式
f(n)=[(n^n)*(A+n-1)]/[(n-1)^n]-(n-1)
题目中告诉f(n)的值,求n最大值
首先观察下前面那个分式,由于n和n-1互质,所以n^n和(n-1)^n也互质,分式结果要为一个整数,f(n)+n-1中必须含有因子n^n;换句话说,f(n)+n-1>n^n,题目中给的f(n)可以用32位整数表示,那么n必然小于12!
下面不用说什么了,暴力吧,肯定0MS了~不过为了完美,n^n我用了二进制快速幂~具体看代码吧

代码:
 1 Source Code
 2 Problem: 1309        User: yzhw
 3 Memory: 392K        Time: 0MS
 4 Language: G++        Result: Accepted
 5 
 6     Source Code
 7 
 8     # include <cstdio>
 9     using namespace std;
10     long long pow(int a,int b)
11     {
12         long long ans=1,t=a;
13         while(b)
14         {
15             if(b&1) ans*=t;
16             t*=t;
17             b>>=1;
18         }
19         return ans;
20     }
21     int main()
22     {
23         //freopen("input.txt","r",stdin);
24         int n;
25         while(scanf("%d",&n)!=EOF&&n>=0)
26         {
27             int ans=-1,i;
28             for(i=2;i<=12;i++)
29             {
30                 long long t=n;
31                 t+=i-1;
32                 long long t1=pow(i,i),t2=pow(i-1,i);
33                 if(t%t1==0)
34                 {
35                     t=t/t1*t2-i+1;
36                     if(t>=0&&t%i==0) ans=i;
37                 }
38             }
39             if(ans==-1) printf("%d coconuts, no solution\n",n);
40             else printf("%d coconuts, %d people and 1 monkey\n",n,ans);
41         }
42         return 0;
43     }
44 
45 

posted on 2011-07-19 00:10 yzhw 阅读(208) 评论(0)  编辑 收藏 引用 所属分类: numberic


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年7月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜