poj 2696 A Mysterious Function

本文介绍了一个名为Φ的递归定义函数,该函数对于任意整数i返回一个特定的整数值。文章提供了一段C++代码,用于计算Φ(i),并解释了如何避免直接递归导致的无限循环问题。
A Mysterious Function
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3517 Accepted: 2398

Description

For any integers p and q with q > 0, define p mod q to be the integer r with 0 <= r <= q −1 such that p−r is divisible by q. For example, we have
    • 109 mod 10 = 9

    • −7 mod 3 = 2

  • −56 mod 7 = 0

Let Φ be a function defined recursively as follows.

where a, b, c, d, e, f, g, h are integers with 0 < a, b, c, d, e, f, g, h <= 1000. One can easily see that 0 <= Φ(i) <= 1000 holds for any integer i >= 0. Now for any given integers a, b, c, d, e, f, g, h, i with 0 < a, b, c, d, e, f, g, h, i <= 1000, you are asked to write a program to output

Φ(i). (Hint: a direct recursive implementation of the above recurrence

relation is likely to run forever for large i.)

Input

The first line contains the number n of test cases. Each of the following n lines contains the sequence a, b, c, d, e, f, g, h, i of integers.

Output

For each test case, your program has to output the correct value of Φ(i).

Sample Input

3
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
321 322 323 324 325 326 327 328 329

Sample Output

4
0
90
#include<iostream>
using namespace std;

int mod(int a,int b)
{
int i;
for(i=0;i<=b-1;i++)
if((a-i)%b==0)
return i;
}

int main()
{
int n;
int res[1000];
int a,b,c,d,e,f,g,h,i;
int x;
cin>>n;
while(n--)
{
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i;
res[0]=a;
res[1]=b;
res[2]=c;
x=3;
while(1)
{
if(x%2==1)
res[x]=mod((d*res[x-1]+e*res[x-2]-f*res[x-3]),g);
if(x%2==0)
res[x]=mod((f*res[x-1]-d*res[x-2]+e*res[x-3]),h);
if(x>=i)
break;
x++;
}
cout<<res[i]<<endl;
}
return 0;
}

转载于:https://www.cnblogs.com/w0w0/archive/2011/11/22/2258643.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值