Devu likes to play with a lock of N dials. Each dial rotates from numbers 0 to 9 clockwise (i.e. 0 to 1, 2 to 3 and 8 to 9). You can not rotate from 9 to 0.
Initially all the dials of the lock are set to 0. From the current lock, Devu can move any dial to at most 10 different positions (i.e. 0 to 9), so there are total 10N different locks possible.
Let us say for a lock we define cost of converting the initial lock (all zeros) to this lock. For this, we have to rotate each dial to corresponding position in the lock. Cost of moving a single dial to value x takes xseconds.
eg. cost of moving to 123 is 1 + 2 + 3 = 6 and cost of moving to 99 is 9 + 9 is 18.
Now Devu has to hurry up to meet his girlfriend, So he has at most M seconds to spend, he wonders how many possible locks he can create such that when a lock is represented as a decimal number, it should be divisible by P. As answer could be large, print answer modulo 998244353.
Only line of input will contain three integers N, P, MM respectively. Use of MM is defined in the output section.
Print a single line containing MM + 1 integers, ith (0 based indexing) of them should represent the
answer for the problem with given N, P and M = i.
Input: 8 0 BB 0 BG 0 BBGG 1 BGG 1 BGGB 1 BBBGG 2 BBGG 2 BGB Output: -1 0 1 1 1 3 1 0
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBGwhich is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j − i|, that is, the absolute value of the difference betweeni and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGwhich is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGBwhich is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will beBGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j − i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBGwhich is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0.
http://www.codechef.com/problems/DEVCLASS
#include<iostream> #include<algorithm> #include<string> #include<map> #include<set> #include<cmath> #include<string.h> #include<stdlib.h> #include<cstdio> #define ll long long using namespace std; int main(){ string s; int i,j,ans,type,l,g,b,e,t; scanf("%d",&t); while(t--){ scanf("%d",&type); cin>>s; l=s.length(); g=0;b=0;ans=0;j=0;e=0; for(i=0;i<l;i++){ if(s[i]=='G') g++; else b++; } if(type==0){ //0次方(不管abs(j-i)等于什么都为1) if(g==b+1){ for(i=0;i<l;i=i+2) //分类 if(s[i]=='B') ans++; } else if(b==g+1){ for(i=0;i<l;i=i+2) //分类 if(s[i]=='G') ans++; } else if(b==g){ e=0; for(i=0;i<l;i=i+2){ //分类 if(s[i]=='B') ans++; else e++; } ans=min(ans,e); } else ans=-1; } else{ //当type>0时都是一样处理(相邻交换,其实题目的type次方是没用的) if(g==b+1){ for(i=0;i<l;i=i+2){ while(s[j]!='G') j++; ans+=abs(i-j); j++; } } else if(b==g+1){ for(i=0;i<l;i=i+2){ while(s[j]!='B') j++; ans+=abs(i-j); j++; } } else if(b==g){ for(i=0;i<l;i=i+2){ while(s[j]!='B') j++; e+=abs(i-j); j++; } j=0; for(i=0;i<l;i=i+2){ while(s[j]!='G') j++; ans+=abs(i-j); j++; } ans=min(ans,e); } else ans=-1; } printf("%d\n",ans); } return 0; } /* 1 6 BBBGGG */