Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Runtime error K<32都行 32就不行了

Posted by gongshaoqing at 2009-07-17 15:55:19 on Problem 2115
#include<iostream>
#include<cmath>
using namespace std;
__int64 funp(int x)
{
    int i;
    __int64 sum;
    sum=1;
    for(i=0;i<x;i++)
       sum*=2;
       return sum;
}
int gcd(int a,int b)
{int d=2;
 while(d>1)
 {
   d=a%b;
   if(d==1)return 1;
   else if(d==0)return b;
   a=b;b=d;
}
}
int pou(int a,int n,int p)
{
    __int64 d=a,b=1;/////////////////
    while(n>1)
    {
        if (n%2==0)
        {
			d=d*d%p;
			n/=2;
        }
		else
		{
			b=b*d%p;
			n--;
		}
    }
    return (int)(d*b%p);
}
int Eula(int n)
{
	int i,ans=n,n2;
    
	if(n%2==0)
	{
              ans=ans-ans/2;
              while(n%2==0)n/=2;
    }
    n2=pow(n,0.5);
	for (i = 3; i <= n2; i+=2)
	{
		if (n % i == 0)
		{
			ans = ans - ans / i;
			do
			{
				n /= i;
			} while (n % i == 0);  
		}
	}
	if (n != 1)
	{
		ans = ans - ans / n;
	}
    return ans;
} 
int main()
{
    int a,c,b,k;
    int d;
    int m,n;
    __int64 L;
    int t;
    while(cin>>a>>b>>c>>k)
    {
          if(a==0&&b==0&&c==0&&k==0)
			  break;
          L=funp(k);                
	      m=(b-a)%L; if(m<0) m+=L;  
          n=c%L;       
          d=gcd(n,L);
          if (m%d==0)
          {
             n/=d; m/=d; L/=d;
             d=Eula(L)-1;
             t=pou(n,d,L);
             t=(__int64)t*m%L;//////////////
             printf("%d\n",t);
          }
          else
          printf("FOREVER\n");                         
    }
    return 0;
}
/*Sample Input

3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0

Sample Output

0
2
32766
FOREVER

*/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator