标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
思路:先模拟将出队的顺序求出,再将同余方程求出,不互质的中国剩余定理;
例:7
6 7 5 3 1 2 4
出队顺序:5 6 4 7 3 1 2 ;
暴力约瑟夫得到;
5≡k mod (n);
1≡k mod (n-1);
5≡k mod (n-2);
1≡k mod (n-3);
3≡k mod (n-4);
1≡k mod (n-5);
1≡k mod (n-6);
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } ll xu[100010]; ll pos[100010]; ll a[100010]; ll b[100010]; ll flag[110]; ll gcd(ll x,ll y) { if(x%y==0) return y; else return gcd(y,x%y); } void exgcd(ll a, ll b, ll &x, ll &y) { if(b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, x, y); ll tmp = x; x = y; y = tmp - (a / b) * y; } int main() { ll x,y,z,i,t; scanf("%lld",&x); while(x--) { memset(a,0,sizeof(a)); memset(flag,0,sizeof(flag)); scanf("%lld",&y); for(i=0;i<y;i++) { scanf("%lld",&xu[i]); pos[xu[i]]=i+1; } int num=y; for(i=1;i<=y;i++) b[i]=num--; int st=1; for(i=1;i<=y;i++) { while(1) { if(st==pos[i]) break; if(!flag[st])a[i]++; st++; if(st==y+1) st=1; } a[i]++; flag[st]=1; //cout<<i<<" "<<a[i]<<endl; } ll a1=a[1],b1=b[1]; ll jie=1; for(i=2;i<=y;i++) { ll a2=a[i],b2=b[i]; ll xx,yy; ll gys=gcd(b1,b2); if((a2-a1)%gys) { jie=0; break; } exgcd(b1,b2,xx,yy); xx=(xx*(a2-a1))/gys; ll gbs=b1*b2/gys; a1=(((xx*b1+a1)%gbs)+gbs)%gbs; b1=gbs; } if(!jie) printf("Creation August is a SB!\n"); else if(a1!=0) printf("%lld\n",a1); else printf("%lld\n",b1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5418669.html