Title Address: Click to open the link
Test instructions: There are N balls, number 0--n-1
Two sequence of the first I ball Xi = (i mod a)
Section I yi= (i mod B)
Seek Sigma (| Xi-yi |)
It is known that there is always an equal difference between Xi and Yi.
Speed can be accelerated under simulation.
#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostream > #include <algorithm> #include <sstream> #include <cmath>using namespace std; #include <queue > #include <stack> #include <set> #include <vector> #include <deque> #include <map># Define Cler (arr, Val) memset (arr, Val, sizeof (arr)) #pragma comment (linker, "/stack:102400000,102400000") typedef long LO ng Ll;const int maxn = 100+6;const int MAXM = 140000;const int INF = 0x3f3f3f3f;const int mod = 1000000007; ll GCD (ll A,ll b) {if (b==0) return A; else return gcd (b,a%b);} Inline LL ABSs (ll a,ll b) {if (a>b) return a B; else return b-a;} ll ans (ll n,ll A,ll b) {ll len=0,x=0,y=0,p=0,out=0; while (len<n) {LL m=min (a-x,b-y); if (len+m>=n) M=n-len; OUT+=ABSS (x, y) *m; x= (x+m)%a; Y= (y+m)%b; Len+=m; } return out;} int main () {#ifndef Online_judge Freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); #endif LL t,n,a,b; cin>>t; while (t--) {cin>>n>>a>>b; LL LCM=A*B/GCD (A, b); cout<<lcm<<endl; The values in each LCM are the same Cout<<n/lcm*ans (lcm,a,b) +ans (n%lcm,a,b) <<endl; } return 0;}
"Blind" HDU 4710 Balls rearrangement